Data Structures | Typedefs | Functions
tommyalloc.h File Reference

Allocator of fixed-size blocks. More...

Go to the source code of this file.

Data Structures

struct  tommy_allocator_struct
 Allocator of fixed-size blocks. More...
 

Typedefs

typedef struct tommy_allocator_struct tommy_allocator
 Allocator of fixed-size blocks. More...
 

Functions

TOMMY_API void tommy_allocator_init (tommy_allocator *alloc, tommy_size_t block_size, tommy_size_t align_size)
 Initializes the allocator. More...
 
TOMMY_API void tommy_allocator_done (tommy_allocator *alloc)
 Deinitializes the allocator. More...
 
TOMMY_API void tommy_allocator_clear (tommy_allocator *alloc)
 Releases all the allocated memory to the heap, including blocks still in use. More...
 
TOMMY_API void * tommy_allocator_alloc (tommy_allocator *alloc)
 Allocates a block. More...
 
TOMMY_API void tommy_allocator_free (tommy_allocator *alloc, void *ptr)
 Deallocates a block. More...
 
TOMMY_API tommy_size_t tommy_allocator_memory_usage (const tommy_allocator *alloc)
 Gets the total size of allocated blocks currently in use. More...
 

Detailed Description

Allocator of fixed-size blocks.

Typedef Documentation

◆ tommy_allocator

Allocator of fixed-size blocks.

Function Documentation

◆ tommy_allocator_init()

TOMMY_API void tommy_allocator_init ( tommy_allocator *  alloc,
tommy_size_t  block_size,
tommy_size_t  align_size 
)

Initializes the allocator.

Parameters
allocThe allocator.
block_sizeSize of the block to allocate.
align_sizeMinimum alignment requirement. Values smaller than sizeof(void*) are raised to sizeof(void*). After this adjustment, align_size must be a multiple of sizeof(void*) because allocator blocks store free-list pointers.

◆ tommy_allocator_done()

TOMMY_API void tommy_allocator_done ( tommy_allocator *  alloc)

Deinitializes the allocator.

It also releases all the allocated memory to the heap.

Parameters
allocThe allocator.

◆ tommy_allocator_clear()

TOMMY_API void tommy_allocator_clear ( tommy_allocator *  alloc)

Releases all the allocated memory to the heap, including blocks still in use.

All previously allocated block pointers become invalid and must not be used or freed. The allocator remains initialized and can be reused immediately with the same block size and alignment. Its memory usage becomes zero. Calling this function on an empty allocator has no effect.

Parameters
allocThe allocator.

◆ tommy_allocator_alloc()

TOMMY_API void * tommy_allocator_alloc ( tommy_allocator *  alloc)

Allocates a block.

Parameters
allocThe allocator.
Returns
Pointer to the allocated block.

◆ tommy_allocator_free()

TOMMY_API void tommy_allocator_free ( tommy_allocator *  alloc,
void *  ptr 
)

Deallocates a block.

You must use the same allocator used in the tommy_allocator_alloc() call.

Parameters
allocThe allocator.
ptrBlock to free.

◆ tommy_allocator_memory_usage()

TOMMY_API tommy_size_t tommy_allocator_memory_usage ( const tommy_allocator *  alloc)

Gets the total size of allocated blocks currently in use.

Excludes free blocks retained by the allocator and segment overhead.

Parameters
allocThe allocator.
Returns
Size of active blocks in bytes.