"L. Spiro Engine"
|
Classes | |
class | CSmallAllocator |
Provide a more efficient way of handling allocations of small sizes. More... | |
class | CStackAllocator |
Stack allocators allocate from the top of a stack and from no where else. More... | |
class | CStdAllocatorHeap |
class | CMemLib |
class | COsHeap |
Typedefs | |
typedef LSUINTPTR | LSA_SIZE |
Functions | |
LSE_CALL | CStdAllocator () |
LSVOID *LSE_CALL | Alloc (LSA_SIZE _sSize, LSUINT32 _ui32Align=16UL) |
LSVOID *LSE_CALL | CAlloc (LSA_SIZE _sSize, LSUINT32 _ui32Align=16UL) |
LSE_INLINE LSBOOL LSE_CALL | Free (LSVOID *_pvAddr) |
LSVOID *LSE_CALL | ReAlloc (LSVOID *_pvAddr, LSA_SIZE _sSize) |
LSBOOL LSE_CALL | Clear () |
LSE_INLINE LSVOID LSE_CALL | Trash () |
LSVOID LSE_CALL | ReleaseEmptyHeaps () |
LSA_SIZE LSE_CALL | PrintAllocations (LSUINT32 _ui32Count=0UL, LSUINT32 _ui32End=0xFFFFFFFFUL) const |
LSA_SIZE LSE_CALL | GetTotalAllocatedSize () |
LSBOOL LSE_CALL | SetSize (LSA_SIZE _sSize, LSBOOL _bGrowable) |
LSBOOL LSE_CALL | AddBlock (LSA_SIZE _sSize) |
LSBOOL LSE_CALL | AddSmallAllocator () |
LSE_INLINE LSVOID *LSE_CALL | AllocInternal (LSA_SIZE _sSize, LSUINT32 _ui32Align) |
LSE_INLINE LSVOID *LSE_CALL | AllocInternalSmall (LSA_SIZE _sSize, LSUINT32 _ui32Align) |
LSVOID *LSE_CALL | ReAllocInternal (CStdAllocatorHeap::LPLSA_ALLOCATIONHEADER &_lpahBlock, LSVOID *_pvAddr, LSA_SIZE _sSize) |
Variables | |
F | __pad0__ |
CCriticalSection | m_csCrit |
CStdAllocatorHeap * | m_psahHead |
CSmallAllocator * | m_psaSmallHead |
LSA_SIZE | m_sSize |
LSBOOL | m_bGrowable |
LSA_SIZE | m_sLow |
LSA_SIZE | m_sHi |
Copyright L. Spiro 2012 All rights reserved.
Written by: Shawn (L. Spiro) Wilcoxen
This code may not be sold or traded for any personal gain without express written consent. You may use this code in your own projects and modify it to suit your needs as long as this disclaimer remains intact. You may not take credit for having written this code.
Description: Stack allocators allocate from the top of a stack and from no where else. They are designed to provide instant allocation but do not offer any way to free individual addresses. This facilitates systems that never allocate more than they need and free all of their allocations at the end of their lives. Allocation time is reduced dramatically and release time is entirely eliminated, and there is no book-keeping for each allocation allowing for an extreme gain in efficiency. With proper organization, this type of allocator can be the most useful of all allocators.
Copyright L. Spiro 2011 All rights reserved.
Written by: Shawn (L. Spiro) Wilcoxen
This code may not be sold or traded for any personal gain without express written consent. You may use this code in your own projects and modify it to suit your needs as long as this disclaimer remains intact. You may not take credit for having written this code.
Description: By default it manages a single CStdAllocatorHeap object, which represents one contiguous block of RAM from which you can allocate, reallocate, and free. If the heap is growable, it can add more of these objects in a singly-linked list to manage more blocks of memory. By using CStdAllocatorHeap objects, there is no restriction on the sizes of allocations, order of allocations, or order of freeing.
Copyright L. Spiro 2011 All rights reserved.
Written by: Shawn (L. Spiro) Wilcoxen
This code may not be sold or traded for any personal gain without express written consent. You may use this code in your own projects and modify it to suit your needs as long as this disclaimer remains intact. You may not take credit for having written this code.
Description: This low-level allocator represents a single contiguous block of RAM. It uses 2 hash tables: one for speedy loop-up by size (free space) and one for speedy loop-up by address (allocated blocks). Each free block is doubly linked forward and backwards linearly in RAM, such that the previous pointer always points to a lower address in RAM and the next pointer always points to a higher address or NULL. These pointers point to the previous and next free blocks respectively. The same blocks are singly-- linked in the hash table organized by size. Finally, each free block also contains a member specifying the actual size of the block. Contiguous free blocks are always merged, so it is always possible to deduce that either an allocated block or the end of the heap immediately follows a free block. Each allocated block is singly-linked linearly, pointing to the next allocated block or NULL. Each allocated block also has a pointer to the previous free block to make removal from the list faster. They also have a linked list of pointers used by the address hash table. The address table size depends on the LSA_BUCKETS macro. The buckets in the table are spaced to cover the whole range of given addresses, so the maximum number of entries in a single bucket depends on both the LSA_BUCKETS macro and the size of the heap covered by this class. There is no pointer to the first allocated block because it can easily be derived. The first allocated block can only be in one of 2 places: at the start of the heap or, if there is free space at the start of the heap, immediately following the first free block.
LSBOOL LSE_CALL lsa::AddBlock | ( | LSA_SIZE | _sSize | ) | [protected] |
Add a new block.
_sSize | Size of the block to add. |
LSBOOL LSE_CALL lsa::AddSmallAllocator | ( | ) | [protected] |
Add a new small allocator.
LSVOID* LSE_CALL lsa::Alloc | ( | LSA_SIZE | _sSize, |
LSUINT32 | _ui32Align = 16UL |
||
) |
Allocate RAM. If there is not enough and the heap can grow, it tries to grow the heap.
_sSize | Amount of RAM to allocate. |
_ui32Align | Alignment of the RAM to allocate. Can be 16 or 32. |
LSE_INLINE LSVOID *LSE_CALL lsa::CStdAllocator::AllocInternal | ( | LSA_SIZE | _sSize, |
LSUINT32 | _ui32Align | ||
) | [protected] |
Allocate interally. Does not try to add heaps etc. Does not lock.
_sSize | Size of the allocation. |
_ui32Align | Alignment of the allocation. |
LSE_INLINE LSVOID *LSE_CALL lsa::CStdAllocator::AllocInternalSmall | ( | LSA_SIZE | _sSize, |
LSUINT32 | _ui32Align | ||
) | [protected] |
Allocate from a small heap. Does not try to add heaps etc. Does not lock.
_sSize | The size to try to allocate. |
_ui32Align | The alignment of the address to return |
LSVOID* LSE_CALL lsa::CAlloc | ( | LSA_SIZE | _sSize, |
LSUINT32 | _ui32Align = 16UL |
||
) |
Exactly the same as Alloc(), but the allocated RAM, if any, is filled with 0's.
_sSize | Amount of RAM to allocate. |
_ui32Align | Alignment of the RAM to allocate. Can be 16 or 32. |
LSBOOL LSE_CALL lsa::Clear | ( | ) |
Clear everything. Returns false if any of the blocks were not fully empty.
LSE_INLINE LSBOOL LSE_CALL lsa::Free | ( | LSVOID * | _pvAddr | ) |
Free RAM.
_pvAddr | The RAM to free. |
LSA_SIZE LSE_CALL lsa::GetTotalAllocatedSize | ( | ) |
Get the total amount of RAM allocated from this heap. This is the amount of RAM that this heap has allocated from the system for its own use, and includes the RAM that it gives to the application upon allocation requests.
LSA_SIZE LSE_CALL lsa::PrintAllocations | ( | LSUINT32 | _ui32Count = 0UL , |
LSUINT32 | _ui32End = 0xFFFFFFFFUL |
||
) | const |
Print allocations with an allocation counter above or equal to the given number. If LSA_DEBUG is not defined, this does nothing.
_ui32Count | Allocations with an allocation number above or equal to this value will be printed. Use 0UL to print all allocations. |
_ui32End | Ending allocation number up to which to print allocations. |
LSVOID* LSE_CALL lsa::ReAlloc | ( | LSVOID * | _pvAddr, |
LSA_SIZE | _sSize | ||
) |
Reallocate RAM. If there is not enough and the heap can grow, it tries to grow the heap.
_pvAddr | Address to reallocate. |
_sSize | New size of the allocation. |
LSVOID* LSE_CALL lsa::ReAllocInternal | ( | CStdAllocatorHeap::LPLSA_ALLOCATIONHEADER & | _lpahBlock, |
LSVOID * | _pvAddr, | ||
LSA_SIZE | _sSize | ||
) | [protected] |
Reallocate internally. Does not try to add heaps etc. Does not lock. On failure, it returns the block that could not be moved (if any) because we need to know its size and alignment to move it manually.
_lpahBlock | Block. |
_pvAddr | Address to reallocate. |
_sSize | New allocation size. |
LSVOID LSE_CALL lsa::ReleaseEmptyHeaps | ( | ) |
Release/remove empty blocks. Does not remove the initial block. Does not clear blocks in which there are still allocations. Can be called at any time.
LSBOOL LSE_CALL lsa::SetSize | ( | LSA_SIZE | _sSize, |
LSBOOL | _bGrowable | ||
) | [protected] |
Set the size of the heap. Can only be done once.
_sSize | Size of the heap. |
_bGrowable | Whether the heap is growable or not. |
LSE_INLINE LSVOID LSE_CALL lsa::Trash | ( | ) |
Trash the whole block of memory. Assumes the user is aware that there may be allocated data in the block but does not care. Does not throw warnings. Sets the block to its default state and allocation can resume as normal.