Skip to content

Commit

Permalink
#6 Use persistent allocator for non-job usages
Browse files Browse the repository at this point in the history
  • Loading branch information
cathei committed Jan 26, 2023
1 parent b4ff5a0 commit b074cad
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// LinqGen, Maxwell Keonwoo Kang <[email protected]>, 2022

using System;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs.LowLevel.Unsafe;

namespace Cathei.LinqGen.Hidden
{
Expand All @@ -24,13 +22,15 @@ public static ref TTo As<TFrom, TTo>(ref TFrom source)
public static T* ArrayAlloc<T>(int size)
where T : unmanaged
{
return (T*)UnsafeUtility.Malloc(size * sizeof(T), UnsafeUtility.AlignOf<T>(), Allocator.Temp);
var allocator = JobsUtility.IsExecutingJob ? Allocator.Temp : Allocator.Persistent;
return (T*)UnsafeUtility.Malloc(size * sizeof(T), UnsafeUtility.AlignOf<T>(), allocator);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ArrayFree(void* array)
{
UnsafeUtility.Free(array, Allocator.Temp);
var allocator = JobsUtility.IsExecutingJob ? Allocator.Temp : Allocator.Persistent;
UnsafeUtility.Free(array, allocator);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ Install from NuGet, both [LinqGen](https://www.nuget.org/packages/LinqGen) as li

```xml
<ItemGroup>
<PackageReference Include="LinqGen" Version="0.2.2" />
<PackageReference Include="LinqGen.Generator" Version="0.2.2" />
<PackageReference Include="LinqGen" Version="0.2.3" />
<PackageReference Include="LinqGen.Generator" Version="0.2.3" />
</ItemGroup>
```

For Unity, you can install as git package from Unity Package Manager.
```
https://github.com/cathei/LinqGen.git?path=LinqGen.Unity/Packages/com.cathei.linqgen#v0.2.2
https://github.com/cathei/LinqGen.git?path=LinqGen.Unity/Packages/com.cathei.linqgen#v0.2.3
```
Or install via OpenUPM.
```
Expand Down

0 comments on commit b074cad

Please sign in to comment.