Пример #1
0
        public NativeMemAllocationMediator(int nativeBlockAllocSize)
        {
            _NativeBlockAllocSize = nativeBlockAllocSize;
            _FullCompleteBlocks   = new List <native_mem_block_t>();

            _ActiveBlock = AllocBlock(_NativeBlockAllocSize);
        }
Пример #2
0
        [M(O.AggressiveInlining)] public IntPtr Alloc(int size)
        {
            Debug.Assert(size <= _NativeBlockAllocSize);

            if (_ActiveBlock.FreeSize() < size)
            {
                _FullCompleteBlocks.Add(_ActiveBlock);
                _ActiveBlock = AllocBlock(_NativeBlockAllocSize);
            }
            var ptr = _ActiveBlock.Alloc(size);

            return(ptr);
        }
Пример #3
0
 private void DisposeNativeResources()
 {
     if (_FullCompleteBlocks != null)
     {
         foreach (var t in _FullCompleteBlocks)
         {
             Marshal.FreeHGlobal(t.GetBaseAddr());
         }
         _FullCompleteBlocks = null;
     }
     if (_ActiveBlock.GetBaseAddr() != IntPtr.Zero)
     {
         Marshal.FreeHGlobal(_ActiveBlock.GetBaseAddr());
         _ActiveBlock = default;
     }
 }
Пример #4
0
        [M(O.AggressiveInlining)] public IntPtr AllocAndCopy(char *source, int sourceLength)
        {
            var copyLenInBytes   = sourceLength * sizeof(char);
            var allocSizeInBytes = copyLenInBytes + sizeof(char);

            Debug.Assert(allocSizeInBytes <= _NativeBlockAllocSize);

            if (_ActiveBlock.FreeSize() < allocSizeInBytes)
            {
                _FullCompleteBlocks.Add(_ActiveBlock);
                _ActiveBlock = AllocBlock(_NativeBlockAllocSize);
            }
            var dest = _ActiveBlock.Alloc(allocSizeInBytes);

            Buffer.MemoryCopy(source, (void *)dest, copyLenInBytes, copyLenInBytes);
            ((char *)dest)[sourceLength] = '\0';
            return(dest);
        }