"L. Spiro Engine"
Classes | Typedefs | Functions | Variables

lsa Namespace Reference

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

__pad0__
CCriticalSection m_csCrit
CStdAllocatorHeapm_psahHead
CSmallAllocatorm_psaSmallHead
LSA_SIZE m_sSize
LSBOOL m_bGrowable
LSA_SIZE m_sLow
LSA_SIZE m_sHi

Detailed Description

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.


Function Documentation

LSBOOL LSE_CALL lsa::AddBlock ( LSA_SIZE  _sSize) [protected]

Add a new block.

Parameters:
_sSizeSize of the block to add.
Returns:
Returns true if a block was added with the given size.
LSBOOL LSE_CALL lsa::AddSmallAllocator ( ) [protected]

Add a new small allocator.

Returns:
Returns true if successful.
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.

Parameters:
_sSizeAmount of RAM to allocate.
_ui32AlignAlignment of the RAM to allocate. Can be 16 or 32.
Returns:
Returns the allocated RAM or NULL if there is not enough RAM available to make the allocation.
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.

Parameters:
_sSizeSize of the allocation.
_ui32AlignAlignment of the allocation.
Returns:
Returns the address of the allocation or NULL if the allocation failed.
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.

Parameters:
_sSizeThe size to try to allocate.
_ui32AlignThe alignment of the address to return
Returns:
Returns the allocated address or NULL if none could be allocated.
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.

Parameters:
_sSizeAmount of RAM to allocate.
_ui32AlignAlignment of the RAM to allocate. Can be 16 or 32.
Returns:
Returns the allocated RAM or NULL if there is not enough RAM available to make the allocation.
LSBOOL LSE_CALL lsa::Clear ( )

Clear everything. Returns false if any of the blocks were not fully empty.

Returns:
Returns true if all blocks were cleared.
LSE_INLINE LSBOOL LSE_CALL lsa::Free ( LSVOID *  _pvAddr)

Free RAM.

Parameters:
_pvAddrThe RAM to free.
Returns:
Returns true if the given pointer was previously returned by a call to Alloc().
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.

Returns:
Returns the total amount of RAM allocated by this heap.
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.

Parameters:
_ui32CountAllocations with an allocation number above or equal to this value will be printed. Use 0UL to print all allocations.
_ui32EndEnding allocation number up to which to print allocations.
Returns:
Returns the combined size of the allocations printed.
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.

Parameters:
_pvAddrAddress to reallocate.
_sSizeNew size of the allocation.
Returns:
Returns a pointer to the reallocated data or NULL if there is not enough RAM available to make 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.

Parameters:
_lpahBlockBlock.
_pvAddrAddress to reallocate.
_sSizeNew allocation size.
Returns:
Returns a pointer to the reallocated data or NULL if there is not enough RAM available to make the allocation.
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.

Parameters:
_sSizeSize of the heap.
_bGrowableWhether the heap is growable or not.
Returns:
Returns true if the heap was sized correctly. A return of false always indicates a lack of memory.
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.

 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator