Fixed the bug that MemCpy does not work as expected

This commit is contained in:
2025-12-07 00:04:37 +09:00
parent f3b0f295a8
commit 0438fce10e
4 changed files with 20 additions and 20 deletions

View File

@@ -247,20 +247,20 @@ public sealed unsafe class JobScheduler : IDisposable
public JobHandle Schedule<T>(ref T job, int threadIndex, JobHandle dependency) public JobHandle Schedule<T>(ref T job, int threadIndex, JobHandle dependency)
where T : unmanaged, IJob where T : unmanaged, IJob
{ {
var jobData = _jobDataAllocator.Allocate(MemoryUtility.SizeOf<T>(), MemoryUtility.AlignOf<T>()); var pJobData = _jobDataAllocator.Allocate(MemoryUtility.SizeOf<T>(), MemoryUtility.AlignOf<T>());
if (jobData == null) if (pJobData == null)
{ {
return JobHandle.Invalid; return JobHandle.Invalid;
} }
fixed (T* pJob = &job) fixed (T* pJob = &job)
{ {
MemoryUtility.MemCpy(pJob, jobData, MemoryUtility.SizeOf<T>()); MemoryUtility.MemCpy(pJobData, pJob, MemoryUtility.SizeOf<T>());
} }
var jobInfo = new JobInfo var jobInfo = new JobInfo
{ {
pJobData = jobData, pJobData = pJobData,
pExecutionFunc = &JobExecutor.Execute<T>, pExecutionFunc = &JobExecutor.Execute<T>,
remainingBatches = 1, remainingBatches = 1,
@@ -323,15 +323,15 @@ public sealed unsafe class JobScheduler : IDisposable
public JobHandle ScheduleParallel<T>(ref T job, int totalIteration, int batchSize, int threadIndex, JobHandle dependency) public JobHandle ScheduleParallel<T>(ref T job, int totalIteration, int batchSize, int threadIndex, JobHandle dependency)
where T : unmanaged, IJobParallelFor where T : unmanaged, IJobParallelFor
{ {
var jobData = _jobDataAllocator.Allocate(MemoryUtility.SizeOf<T>(), MemoryUtility.AlignOf<T>()); var pJobData = _jobDataAllocator.Allocate(MemoryUtility.SizeOf<T>(), MemoryUtility.AlignOf<T>());
if (jobData == null) if (pJobData == null)
{ {
return JobHandle.Invalid; return JobHandle.Invalid;
} }
fixed (T* pJob = &job) fixed (T* pJob = &job)
{ {
MemoryUtility.MemCpy(pJob, jobData, MemoryUtility.SizeOf<T>()); MemoryUtility.MemCpy(pJobData, pJob, MemoryUtility.SizeOf<T>());
} }
var optimalBatchSize = Math.Max(1, batchSize); var optimalBatchSize = Math.Max(1, batchSize);
@@ -339,7 +339,7 @@ public sealed unsafe class JobScheduler : IDisposable
var jobInfo = new JobInfo var jobInfo = new JobInfo
{ {
pJobData = jobData, pJobData = pJobData,
pExecutionFunc = &JobExecutor.ExecuteParallel<T>, pExecutionFunc = &JobExecutor.ExecuteParallel<T>,
remainingBatches = totalBatches, remainingBatches = totalBatches,

View File

@@ -66,7 +66,7 @@ public unsafe struct TempJobAllocator : IAllocator, IDisposable
return null; return null;
} }
MemoryUtility.MemCpy(newPtr, ptr, Math.Min(oldSize, newSize)); MemoryUtility.MemCpy(ptr, newPtr,Math.Min(oldSize, newSize));
return newPtr; return newPtr;
} }

View File

@@ -123,7 +123,7 @@ public unsafe struct UnTypedArray : IUnTypedCollection
fixed (T* pDest = destination) fixed (T* pDest = destination)
{ {
MemCpy(_buffer, pDest, _size); MemCpy(pDest, _buffer, _size);
} }
} }
@@ -147,7 +147,7 @@ public unsafe struct UnTypedArray : IUnTypedCollection
fixed (T* pDest = destination) fixed (T* pDest = destination)
{ {
MemCpy((byte*)_buffer + sourceOffset, pDest + destinationIndex, length * sizeOfElement); MemCpy(pDest + destinationIndex, (byte*)_buffer + sourceOffset, length * sizeOfElement);
} }
} }
@@ -169,7 +169,7 @@ public unsafe struct UnTypedArray : IUnTypedCollection
fixed (T* pSrc = source) fixed (T* pSrc = source)
{ {
MemCpy(pSrc, _buffer, sourceSize); MemCpy(_buffer, pSrc, sourceSize);
} }
} }
@@ -193,7 +193,7 @@ public unsafe struct UnTypedArray : IUnTypedCollection
fixed (T* pSrc = source) fixed (T* pSrc = source)
{ {
MemCpy(pSrc + sourceIndex, (byte*)_buffer + destinationOffset, length * sizeOfElement); MemCpy((byte*)_buffer + destinationOffset, pSrc + sourceIndex, length * sizeOfElement);
} }
} }

View File

@@ -29,7 +29,7 @@ public static unsafe class UnsafeCollectionExtensions
fixed (T* pDest = destination) fixed (T* pDest = destination)
{ {
MemCpy(source.GetUnsafePtr(), pDest, (uint)(source.Count * sizeof(T))); MemCpy(pDest, source.GetUnsafePtr(), (uint)(source.Count * sizeof(T)));
} }
} }
@@ -54,7 +54,7 @@ public static unsafe class UnsafeCollectionExtensions
fixed (T* pDest = destination) fixed (T* pDest = destination)
{ {
MemCpy((byte*)source.GetUnsafePtr() + sourceIndex * sizeof(T), pDest + destinationIndex, (uint)(length * sizeof(T))); MemCpy(pDest + destinationIndex, (byte*)source.GetUnsafePtr() + sourceIndex * sizeof(T), (uint)(length * sizeof(T)));
} }
} }
@@ -76,7 +76,7 @@ public static unsafe class UnsafeCollectionExtensions
fixed (T* pSrc = source) fixed (T* pSrc = source)
{ {
MemCpy(pSrc, destination.GetUnsafePtr(), (uint)(source.Length * sizeof(T))); MemCpy(destination.GetUnsafePtr(), pSrc, (uint)(source.Length * sizeof(T)));
} }
} }
@@ -101,7 +101,7 @@ public static unsafe class UnsafeCollectionExtensions
fixed (T* pSrc = source) fixed (T* pSrc = source)
{ {
MemCpy(pSrc + sourceIndex, (byte*)destination.GetUnsafePtr() + destinationIndex * sizeof(T), (uint)(length * sizeof(T))); MemCpy((byte*)destination.GetUnsafePtr() + destinationIndex * sizeof(T), pSrc + sourceIndex, (uint)(length * sizeof(T)));
} }
} }
@@ -118,7 +118,7 @@ public static unsafe class UnsafeCollectionExtensions
var array = new UnsafeArray<T>(source.Length, allocator); var array = new UnsafeArray<T>(source.Length, allocator);
fixed (T* pSrc = source) fixed (T* pSrc = source)
{ {
MemCpy(pSrc, array.GetUnsafePtr(), (uint)(source.Length * sizeof(T))); MemCpy(array.GetUnsafePtr(), pSrc, (uint)(source.Length * sizeof(T)));
} }
return array; return array;
@@ -137,7 +137,7 @@ public static unsafe class UnsafeCollectionExtensions
var list = new UnsafeList<T>(source.Count, allocator); var list = new UnsafeList<T>(source.Count, allocator);
fixed (T* pSrc = CollectionsMarshal.AsSpan(source)) fixed (T* pSrc = CollectionsMarshal.AsSpan(source))
{ {
MemCpy(pSrc, list.GetUnsafePtr(), (uint)(source.Count * sizeof(T))); MemCpy(list.GetUnsafePtr(), pSrc, (uint)(source.Count * sizeof(T)));
} }
return list; return list;