ShinyAllocator  1.0
ShinyAllocator is a library of a block memory allocator for real-time high-integrity embedded systems.
Classes | Macros | Typedefs | Functions
shinyAllocator.c File Reference
#include "shinyAllocator.h"
#include <assert.h>
#include <limits.h>
#include <stdint.h>
#include <stddef.h>
#include <pthread.h>
Include dependency graph for shinyAllocator.c:

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)
 
shinyAllocatorInstanceshinyInit (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...
 
shinyAllocatorThreadSafeInstanceshinyInitThreadSafe (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...
 

Macro Definition Documentation

◆ _static_assert_gl

#define _static_assert_gl (   a,
 
)    _static_assert_gl_impl(a, b)

◆ _static_assert_gl_impl

#define _static_assert_gl_impl (   a,
 
)    a##b

◆ FRAGMENT_SIZE_MAX

#define FRAGMENT_SIZE_MAX   ((SIZE_MAX >> 1U) + 1U)

◆ FRAGMENT_SIZE_MIN

#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.

◆ INSTANCE_SIZE_PADDED

#define INSTANCE_SIZE_PADDED   ((sizeof(shinyAllocatorInstance) + SHINYALLOCATOR_ALIGNMENT - 1U) & ~(SHINYALLOCATOR_ALIGNMENT - 1U))

the amount of space the aligned allocator instance takes

◆ NUM_FRAGMENTS_MAX

#define NUM_FRAGMENTS_MAX   (sizeof(size_t) * CHAR_BIT)

The maximum number of fragments that can be allocated to the pool.

◆ SHINYALLOCATOR_ASSERT

#define SHINYALLOCATOR_ASSERT (   x)    assert(x)

◆ SHINYALLOCATOR_ERROR

#define SHINYALLOCATOR_ERROR   -1

◆ SHINYALLOCATOR_LIKELY

#define SHINYALLOCATOR_LIKELY (   x)    x

◆ SHINYALLOCATOR_OK

#define SHINYALLOCATOR_OK   0

◆ SHINYALLOCATOR_PRIVATE

#define SHINYALLOCATOR_PRIVATE   static inline

◆ SHINYALLOCATOR_USE_INTRINSICS

#define SHINYALLOCATOR_USE_INTRINSICS   1

Activates compiler optimizations for annotations.

◆ static_assert

#define static_assert (   x,
  ... 
)    typedef char _static_assert_gl(_static_assertion_, __LINE__)[(x) ? 1 : -1]

Typedef Documentation

◆ Fragment

typedef struct Fragment Fragment

◆ FragmentHeader

structuer to store fragment information

Parameters
nextstores the pointer to the next fragment
prevstores the pointer to the previous fragment
sizestores the size of the fragment
usedstores current used capacity of the fragment

◆ mutex_t

typedef pthread_mutex_t mutex_t

Function Documentation

◆ appendFragment()

SHINYALLOCATOR_PRIVATE void appendFragment ( shinyAllocatorInstance *const  handle,
Fragment *const  fragment 
)

◆ fragmentLink()

SHINYALLOCATOR_PRIVATE void fragmentLink ( Fragment left,
Fragment right 
)

links the given fragments previous and next fragments in a LeftToRight manner.

Parameters
left
right

◆ log2Ceil()

SHINYALLOCATOR_PRIVATE uint_fast8_t log2Ceil ( const size_t  x)

efficient binary logarithm ceiling of x implementation

!

Parameters
x
Returns
floor of x log 2

◆ log2Floor()

SHINYALLOCATOR_PRIVATE uint_fast8_t log2Floor ( const size_t  x)

efficient binary logarithm floor of x implementation

!

Parameters
x
Returns
floor of x log 2

◆ mutex_destroy()

SHINYALLOCATOR_PRIVATE SHINY_STATUS mutex_destroy ( mutex_t mutex)

◆ mutex_init()

SHINYALLOCATOR_PRIVATE SHINY_STATUS mutex_init ( mutex_t mutex)

◆ mutex_lock()

SHINYALLOCATOR_PRIVATE SHINY_STATUS mutex_lock ( mutex_t mutex)

◆ mutex_trylock()

SHINYALLOCATOR_PRIVATE SHINY_STATUS mutex_trylock ( mutex_t mutex)

◆ mutex_unlock()

SHINYALLOCATOR_PRIVATE SHINY_STATUS mutex_unlock ( mutex_t mutex)

◆ pow2()

SHINYALLOCATOR_PRIVATE size_t pow2 ( const uint_fast8_t  power)

efficient x-th power of two implementation

!

Parameters
power
Returns
x-th power of 2

◆ removeFragment()

SHINYALLOCATOR_PRIVATE void removeFragment ( shinyAllocatorInstance *const  handle,
const Fragment *const  fragment 
)

◆ roundUpToPowerOfTwo()

SHINYALLOCATOR_PRIVATE size_t roundUpToPowerOfTwo ( const size_t  x)
Parameters
x
Returns
closest power of two

◆ shinyAllocate()

void* shinyAllocate ( shinyAllocatorInstance *const  handle,
const size_t  amount 
)

Allocated the requested memory to the given the pool handle, returns NULL if it fails.

Parameters
handleallocater handle to the pool.
sizethe requested allocation size.
Note
may cause integer-overflow for bigger sizes. It's recommended keep overall allocated bytes less than SIZE_MAX/2-1.

◆ shinyAllocateThreadSafe()

void* shinyAllocateThreadSafe ( shinyAllocatorThreadSafeInstance *const  threadSafeHandle,
const size_t  amount 
)

Allocates memory from a thread-safe shinyAllocator instance.

Parameters
threadSafeHandleThread-safe shinyAllocator instance.
amountAmount of memory to allocate.
Returns
Pointer to the allocated memory.

◆ SHINYALLOCATOR_CLZ()

SHINYALLOCATOR_PRIVATE uint_fast8_t SHINYALLOCATOR_CLZ ( const size_t  x)

◆ shinyDeinitThreadSafe()

SHINY_STATUS shinyDeinitThreadSafe ( shinyAllocatorThreadSafeInstance *const  threadSafeHandle)

Deinitializes a thread-safe shinyAllocator instance.

Parameters
threadSafeHandle

◆ shinyFree()

void shinyFree ( shinyAllocatorInstance *const  handle,
void *const  pointer 
)

Frees the memory allocated to the given the pool handle.

Parameters
handleallocator handle to the pool.
pointerpointer to the allocated memory.

◆ shinyFreeThreadSafe()

SHINY_STATUS shinyFreeThreadSafe ( shinyAllocatorThreadSafeInstance *const  threadSafeHandle,
void *const  pointer 
)

Frees memory allocated by a thread-safe shinyAllocator instance.

Parameters
threadSafeHandleThread-safe shinyAllocator instance.
pointerPointer to the memory to be freed.

◆ shinyGetDiagnostics()

shinyAllocatorDiagnostics shinyGetDiagnostics ( shinyAllocatorInstance handle)
Parameters
handlepointer
Returns
current diagnostics

◆ shinyGetDiagnosticsThreadSafe()

shinyAllocatorDiagnostics shinyGetDiagnosticsThreadSafe ( shinyAllocatorThreadSafeInstance *const  threadSafeHandle)

Thread-safe wrapper for shinyGetDiagnostics().

Parameters
threadSafeHandleThread-safe shinyAllocator instance.
Returns
Diagnostics for the shinyAllocator instance.

◆ shinyInit()

shinyAllocatorInstance* shinyInit ( void *const  base,
const size_t  size 
)

Initializes the shinyAllocator for the given base pointer and size.

Parameters
basebase pointer for the pool, it should be aligned to SHINYALLOCATOR_ALIGNMENT.
sizesize 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.

Returns
NULL if the pool is not sufficient for the given size otherwise returns a pointer to the newly initialized allocator.
Note
An initialized any resources, hence you can discard it without any de-initialization if it is not needed.

◆ shinyInitThreadSafe()

shinyAllocatorThreadSafeInstance* shinyInitThreadSafe ( void *const  base,
const size_t  size 
)

Initializes a thread-safe shinyAllocator instance.

Parameters
baseBase address for the shinyAllocator instance.
sizeSize of the shinyAllocator instance.
Returns
Thread-safe shinyAllocator instance.

◆ sizeof_shinyAllocatorInstance()

size_t sizeof_shinyAllocatorInstance ( void  )
Returns
size of the shinyAllocatorInstance

◆ sizeof_shinyAllocatorThreadSafeInstance()

size_t sizeof_shinyAllocatorThreadSafeInstance ( void  )