/// <summary> /// Returns the number of free bytes in the given arena. /// </summary> /// <param name="inArena"></param> /// <returns></returns> static public int ArenaFreeBytes(ArenaHandle inArena) { ArenaHeader *header = (ArenaHeader *)inArena.HeaderStart; if (header != null) { return((int)header->SizeRemaining); } return(0); }
/// <summary> /// Resets the given allocation arena. /// </summary> static public void ResetArena(ArenaHandle inArena) { ArenaHeader *header = (ArenaHeader *)inArena.HeaderStart; if (header != null) { header->SizeRemaining = header->Size; header->CurrentPtr = header->StartPtr; } }
/// <summary> /// Returns the size of the given arena. /// </summary> static public int ArenaSize(ArenaHandle inArena) { ArenaHeader *header = (ArenaHeader *)inArena.HeaderStart; if (header != null) { return((int)header->Size); } return(0); }
/// <summary> /// Attempts to free the given allocation arena. /// </summary> static public bool TryFreeArena(ref ArenaHandle ioArena) { if (ioArena.HeaderStart != null) { Free(ioArena.HeaderStart); ioArena.HeaderStart = null; return(true); } return(false); }
/// <summary> /// Allocates from the given arena. /// </summary> static public void *Alloc(ArenaHandle inArena, int inLength) { ArenaHeader *header = (ArenaHeader *)inArena.HeaderStart; if (header == null) { return(null); } if (header->SizeRemaining < inLength) { Log.Warn("[Unsafe] Unable to allocate region of size {0} in arena {1} (size remaining {2})", inLength, header->Name, header->SizeRemaining); return(null); } void *addr = header->CurrentPtr; header->CurrentPtr += inLength; header->SizeRemaining -= (uint)inLength; return(addr); }
/// <summary> /// Allocates from the given arena with the given alignment. /// </summary> static public void *AllocAligned(ArenaHandle inArena, int inLength, uint inAlignment) { ArenaHeader *header = (ArenaHeader *)inArena.HeaderStart; if (header == null) { return(null); } byte *aligned = (byte *)AlignUpN((ulong)header->CurrentPtr, inAlignment); uint padding = (uint)(aligned - header->CurrentPtr); if (header->SizeRemaining < padding + inLength) { Log.Warn("[Unsafe] Unable to allocate region of size {0} and alignment {1} in arena {2} (size remaining {3})", inLength, inAlignment, header->Name, header->SizeRemaining); return(null); } header->CurrentPtr += inLength + padding; header->SizeRemaining -= (uint)inLength + padding; return(aligned); }
/// <summary> /// Frees the given allocation arena. /// </summary> static public void FreeArena(ArenaHandle inArena) { Free(inArena.HeaderStart); }
static public void *AllocArray <T>(ArenaHandle inArena, int inLength) where T : struct { return((void *)AllocAligned(inArena, inLength * SizeOf <T>(), AlignOf <T>())); }
static public T *AllocArray <T>(ArenaHandle inArena, int inLength) where T : unmanaged { return((T *)AllocAligned(inArena, inLength * sizeof(T), AlignOf <T>())); }