ShinyAllocator
1.0
ShinyAllocator is a library of a block memory allocator for real-time high-integrity embedded systems.
|
#include "shinyAllocator.h"
#include <assert.h>
#include <limits.h>
#include <stdint.h>
#include <stddef.h>
#include <pthread.h>
Classes | |
struct | FragmentHeader |
structuer to store fragment information More... | |
struct | Fragment |
Stores current fragment status. More... | |
struct | shinyAllocatorInstance |
the allocator which stores the information about the pool structure More... | |
struct | shinyAllocatorThreadSafeInstance |
Initializes the allocator. More... | |
Macros | |
#define | SHINYALLOCATOR_ERROR -1 |
#define | SHINYALLOCATOR_OK 0 |
#define | SHINYALLOCATOR_ASSERT(x) assert(x) |
#define | SHINYALLOCATOR_USE_INTRINSICS 1 |
Activates compiler optimizations for annotations. More... | |
#define | SHINYALLOCATOR_LIKELY(x) x |
#define | SHINYALLOCATOR_PRIVATE static inline |
#define | static_assert(x, ...) typedef char _static_assert_gl(_static_assertion_, __LINE__)[(x) ? 1 : -1] |
#define | _static_assert_gl(a, b) _static_assert_gl_impl(a, b) |
#define | _static_assert_gl_impl(a, b) a##b |
#define | FRAGMENT_SIZE_MIN (SHINYALLOCATOR_ALIGNMENT * 2U) |
The occupying space by the allocator is maximum SHINYALLOCATOR_ALIGNMENT bytes big and better to be bound the over head with the maximum possible fragment size regarding the overhead of the allocation. More... | |
#define | FRAGMENT_SIZE_MAX ((SIZE_MAX >> 1U) + 1U) |
#define | NUM_FRAGMENTS_MAX (sizeof(size_t) * CHAR_BIT) |
The maximum number of fragments that can be allocated to the pool. More... | |
#define | INSTANCE_SIZE_PADDED ((sizeof(shinyAllocatorInstance) + SHINYALLOCATOR_ALIGNMENT - 1U) & ~(SHINYALLOCATOR_ALIGNMENT - 1U)) |
the amount of space the aligned allocator instance takes More... | |
Typedefs | |
typedef pthread_mutex_t | mutex_t |
typedef struct Fragment | Fragment |
typedef struct FragmentHeader | FragmentHeader |
structuer to store fragment information More... | |
Functions | |
SHINYALLOCATOR_PRIVATE uint_fast8_t | SHINYALLOCATOR_CLZ (const size_t x) |
SHINYALLOCATOR_PRIVATE SHINY_STATUS | mutex_init (mutex_t *mutex) |
SHINYALLOCATOR_PRIVATE SHINY_STATUS | mutex_destroy (mutex_t *mutex) |
SHINYALLOCATOR_PRIVATE SHINY_STATUS | mutex_lock (mutex_t *mutex) |
SHINYALLOCATOR_PRIVATE SHINY_STATUS | mutex_trylock (mutex_t *mutex) |
SHINYALLOCATOR_PRIVATE SHINY_STATUS | mutex_unlock (mutex_t *mutex) |
SHINYALLOCATOR_PRIVATE uint_fast8_t | log2Floor (const size_t x) |
efficient binary logarithm floor of x implementation More... | |
SHINYALLOCATOR_PRIVATE uint_fast8_t | log2Ceil (const size_t x) |
efficient binary logarithm ceiling of x implementation More... | |
SHINYALLOCATOR_PRIVATE size_t | pow2 (const uint_fast8_t power) |
efficient x-th power of two implementation More... | |
SHINYALLOCATOR_PRIVATE size_t | roundUpToPowerOfTwo (const size_t x) |
SHINYALLOCATOR_PRIVATE void | fragmentLink (Fragment *left, Fragment *right) |
links the given fragments previous and next fragments in a LeftToRight manner. More... | |
SHINYALLOCATOR_PRIVATE void | appendFragment (shinyAllocatorInstance *const handle, Fragment *const fragment) |
SHINYALLOCATOR_PRIVATE void | removeFragment (shinyAllocatorInstance *const handle, const Fragment *const fragment) |
size_t | sizeof_shinyAllocatorInstance (void) |
size_t | sizeof_shinyAllocatorThreadSafeInstance (void) |
shinyAllocatorDiagnostics | shinyGetDiagnostics (shinyAllocatorInstance *handle) |
shinyAllocatorInstance * | shinyInit (void *const base, const size_t size) |
Initializes the shinyAllocator for the given base pointer and size. More... | |
void * | shinyAllocate (shinyAllocatorInstance *const handle, const size_t amount) |
Allocated the requested memory to the given the pool handle, returns NULL if it fails. More... | |
void | shinyFree (shinyAllocatorInstance *const handle, void *const pointer) |
Frees the memory allocated to the given the pool handle. More... | |
shinyAllocatorDiagnostics | shinyGetDiagnosticsThreadSafe (shinyAllocatorThreadSafeInstance *const threadSafeHandle) |
Thread-safe wrapper for shinyGetDiagnostics(). More... | |
shinyAllocatorThreadSafeInstance * | shinyInitThreadSafe (void *const base, const size_t size) |
Initializes a thread-safe shinyAllocator instance. More... | |
void * | shinyAllocateThreadSafe (shinyAllocatorThreadSafeInstance *const threadSafeHandle, const size_t amount) |
Allocates memory from a thread-safe shinyAllocator instance. More... | |
SHINY_STATUS | shinyFreeThreadSafe (shinyAllocatorThreadSafeInstance *const threadSafeHandle, void *const pointer) |
Frees memory allocated by a thread-safe shinyAllocator instance. More... | |
SHINY_STATUS | shinyDeinitThreadSafe (shinyAllocatorThreadSafeInstance *const threadSafeHandle) |
Deinitializes a thread-safe shinyAllocator instance. More... | |
#define _static_assert_gl | ( | a, | |
b | |||
) | _static_assert_gl_impl(a, b) |
#define _static_assert_gl_impl | ( | a, | |
b | |||
) | a##b |
#define FRAGMENT_SIZE_MAX ((SIZE_MAX >> 1U) + 1U) |
#define FRAGMENT_SIZE_MIN (SHINYALLOCATOR_ALIGNMENT * 2U) |
The occupying space by the allocator is maximum SHINYALLOCATOR_ALIGNMENT bytes big and better to be bound the over head with the maximum possible fragment size regarding the overhead of the allocation.
#define INSTANCE_SIZE_PADDED ((sizeof(shinyAllocatorInstance) + SHINYALLOCATOR_ALIGNMENT - 1U) & ~(SHINYALLOCATOR_ALIGNMENT - 1U)) |
the amount of space the aligned allocator instance takes
#define NUM_FRAGMENTS_MAX (sizeof(size_t) * CHAR_BIT) |
The maximum number of fragments that can be allocated to the pool.
#define SHINYALLOCATOR_ASSERT | ( | x | ) | assert(x) |
#define SHINYALLOCATOR_ERROR -1 |
#define SHINYALLOCATOR_LIKELY | ( | x | ) | x |
#define SHINYALLOCATOR_OK 0 |
#define SHINYALLOCATOR_PRIVATE static inline |
#define SHINYALLOCATOR_USE_INTRINSICS 1 |
Activates compiler optimizations for annotations.
#define static_assert | ( | x, | |
... | |||
) | typedef char _static_assert_gl(_static_assertion_, __LINE__)[(x) ? 1 : -1] |
typedef struct FragmentHeader FragmentHeader |
structuer to store fragment information
next | stores the pointer to the next fragment |
prev | stores the pointer to the previous fragment |
size | stores the size of the fragment |
used | stores current used capacity of the fragment |
typedef pthread_mutex_t mutex_t |
SHINYALLOCATOR_PRIVATE void appendFragment | ( | shinyAllocatorInstance *const | handle, |
Fragment *const | fragment | ||
) |
SHINYALLOCATOR_PRIVATE void fragmentLink | ( | Fragment * | left, |
Fragment * | right | ||
) |
links the given fragments previous and next fragments in a LeftToRight manner.
left | |
right |
SHINYALLOCATOR_PRIVATE uint_fast8_t log2Ceil | ( | const size_t | x | ) |
efficient binary logarithm ceiling of x implementation
!
x |
SHINYALLOCATOR_PRIVATE uint_fast8_t log2Floor | ( | const size_t | x | ) |
efficient binary logarithm floor of x implementation
!
x |
SHINYALLOCATOR_PRIVATE SHINY_STATUS mutex_destroy | ( | mutex_t * | mutex | ) |
SHINYALLOCATOR_PRIVATE SHINY_STATUS mutex_init | ( | mutex_t * | mutex | ) |
SHINYALLOCATOR_PRIVATE SHINY_STATUS mutex_lock | ( | mutex_t * | mutex | ) |
SHINYALLOCATOR_PRIVATE SHINY_STATUS mutex_trylock | ( | mutex_t * | mutex | ) |
SHINYALLOCATOR_PRIVATE SHINY_STATUS mutex_unlock | ( | mutex_t * | mutex | ) |
SHINYALLOCATOR_PRIVATE size_t pow2 | ( | const uint_fast8_t | power | ) |
efficient x-th power of two implementation
!
power |
SHINYALLOCATOR_PRIVATE void removeFragment | ( | shinyAllocatorInstance *const | handle, |
const Fragment *const | fragment | ||
) |
SHINYALLOCATOR_PRIVATE size_t roundUpToPowerOfTwo | ( | const size_t | x | ) |
x |
void* shinyAllocate | ( | shinyAllocatorInstance *const | handle, |
const size_t | amount | ||
) |
Allocated the requested memory to the given the pool handle, returns NULL if it fails.
handle | allocater handle to the pool. |
size | the requested allocation size. |
void* shinyAllocateThreadSafe | ( | shinyAllocatorThreadSafeInstance *const | threadSafeHandle, |
const size_t | amount | ||
) |
Allocates memory from a thread-safe shinyAllocator instance.
threadSafeHandle | Thread-safe shinyAllocator instance. |
amount | Amount of memory to allocate. |
SHINYALLOCATOR_PRIVATE uint_fast8_t SHINYALLOCATOR_CLZ | ( | const size_t | x | ) |
SHINY_STATUS shinyDeinitThreadSafe | ( | shinyAllocatorThreadSafeInstance *const | threadSafeHandle | ) |
Deinitializes a thread-safe shinyAllocator instance.
threadSafeHandle |
void shinyFree | ( | shinyAllocatorInstance *const | handle, |
void *const | pointer | ||
) |
Frees the memory allocated to the given the pool handle.
handle | allocator handle to the pool. |
pointer | pointer to the allocated memory. |
SHINY_STATUS shinyFreeThreadSafe | ( | shinyAllocatorThreadSafeInstance *const | threadSafeHandle, |
void *const | pointer | ||
) |
Frees memory allocated by a thread-safe shinyAllocator instance.
threadSafeHandle | Thread-safe shinyAllocator instance. |
pointer | Pointer to the memory to be freed. |
shinyAllocatorDiagnostics shinyGetDiagnostics | ( | shinyAllocatorInstance * | handle | ) |
handle | pointer |
shinyAllocatorDiagnostics shinyGetDiagnosticsThreadSafe | ( | shinyAllocatorThreadSafeInstance *const | threadSafeHandle | ) |
Thread-safe wrapper for shinyGetDiagnostics().
threadSafeHandle | Thread-safe shinyAllocator instance. |
shinyAllocatorInstance* shinyInit | ( | void *const | base, |
const size_t | size | ||
) |
Initializes the shinyAllocator for the given base pointer and size.
base | base pointer for the pool, it should be aligned to SHINYALLOCATOR_ALIGNMENT. |
size | size of the pool, this parameter should not exceed SIZE_MAX/2. |
allocator occupy 40+ bytes (up to 600 bytes depending on architecture) of the pool for holding its configuration.
shinyAllocatorThreadSafeInstance* shinyInitThreadSafe | ( | void *const | base, |
const size_t | size | ||
) |
Initializes a thread-safe shinyAllocator instance.
base | Base address for the shinyAllocator instance. |
size | Size of the shinyAllocator instance. |
size_t sizeof_shinyAllocatorInstance | ( | void | ) |
size_t sizeof_shinyAllocatorThreadSafeInstance | ( | void | ) |