Skip to content

Commit

Permalink
Merge pull request #112 from AnnulusGames/fix-possibility-of-double-r…
Browse files Browse the repository at this point in the history
…elease

Fix: possibility of double release
  • Loading branch information
AnnulusGames authored Mar 18, 2024
2 parents 61239d3 + 99ceedb commit a07eb60
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 63 deletions.
15 changes: 10 additions & 5 deletions src/LitMotion/Assets/LitMotion/Runtime/Internal/MotionStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,11 @@ internal sealed class MotionStorage<TValue, TOptions, TAdapter> : IMotionStorage
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
{
readonly struct AnimationCurveAllocatorKey { }

public MotionStorage(int id) => StorageId = id;
public MotionStorage(int id)
{
StorageId = id;
AllocatorHelper = RewindableAllocatorFactory.CreateAllocator();
}

// Entry
readonly StorageEntryList entries = new(InitialCapacity);
Expand All @@ -133,6 +135,9 @@ readonly struct AnimationCurveAllocatorKey { }
public MotionData<TValue, TOptions>[] dataArray = new MotionData<TValue, TOptions>[InitialCapacity];
public MotionCallbackData[] callbacksArray = new MotionCallbackData[InitialCapacity];

// Allocator
AllocatorHelper<RewindableAllocator> AllocatorHelper;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<MotionData<TValue, TOptions>> GetDataSpan() => dataArray.AsSpan(0, tail);

Expand Down Expand Up @@ -171,7 +176,7 @@ readonly struct AnimationCurveAllocatorKey { }
{
if (!prevAnimationCurve.IsCreated)
{
prevAnimationCurve = new NativeAnimationCurve(SharedRewindableAllocator<AnimationCurveAllocatorKey>.Allocator.Handle);
prevAnimationCurve = new NativeAnimationCurve(AllocatorHelper.Allocator.Handle);
}

prevAnimationCurve.CopyFrom(data.AnimationCurve);
Expand Down Expand Up @@ -403,7 +408,7 @@ public void Reset()
callbacksArray.AsSpan().Clear();
tail = 0;

SharedRewindableAllocator<AnimationCurveAllocatorKey>.Allocator.Rewind();
AllocatorHelper.Allocator.Rewind();
}

public float GetPlaybackSpeed(MotionHandle handle)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.Collections;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace LitMotion
{
internal static class RewindableAllocatorFactory
{
const int InitialSize = 128 * 1024;
static bool isInitialized;
static readonly Stack<AllocatorHelper<RewindableAllocator>> allocators = new();

public static AllocatorHelper<RewindableAllocator> CreateAllocator()
{
Initialize();

var allocatorHelper = new AllocatorHelper<RewindableAllocator>(Allocator.Persistent);
allocatorHelper.Allocator.Initialize(InitialSize, true);
allocators.Push(allocatorHelper);

return allocatorHelper;
}

static void Initialize()
{
if (!isInitialized)
{
#if UNITY_EDITOR
AssemblyReloadEvents.beforeAssemblyReload += Dispose;
#else
UnityEngine.Application.quitting += Dispose;
#endif
isInitialized = true;
}
}

static void Dispose()
{
while (allocators.TryPop(out var allocatorHelper))
{
allocatorHelper.Allocator.Dispose();
allocatorHelper.Dispose();
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ public void Test_Dispose()
public void Test_Dispose_RewindableAllocator()
{
var curve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
var native = new NativeAnimationCurve(curve, SharedRewindableAllocator<NativeAnimationCurveTest>.Allocator.Handle);
var allocator = RewindableAllocatorFactory.CreateAllocator();
var native = new NativeAnimationCurve(curve, allocator.Allocator.Handle);

SharedRewindableAllocator<NativeAnimationCurveTest>.Allocator.Rewind();
allocator.Allocator.Rewind();

try
{
Expand Down

0 comments on commit a07eb60

Please sign in to comment.