Skip to content
Konstantin Khait edited this page Jul 22, 2026 · 1 revision

Loviisa provides the following primitives for memory management:

  • Three basic memory control functions that can be a replacement for RTL alternatives for platforms where memory.h is not supported
  • Memory pools mechanism

void lvs_memset(void* ptr, unsigned char value, int size) fills memory with the given value. Same to the standard C memset function.

int lvs_memcmp(void* p1, void* p2, int size) compares two memory blocks of the given size. Same to the standard C memcmp function.

void lvs_memcpy(void* p1, void* p2, int size) copy memory block of the given size. Same to the standard C memcmp function.

Memory pools give you more predictable method of dynamical memory allocation than the regular allocation using heaps. Memory pool is a set of elements of the same type that can be allocated and deallocated by the application. When all elements of the pool are allocated, next allocation attempt causes an error.

To make a memory pool you shall define a data type that each memory pool item will have.

LVS_START_DATA(type_name) begins data type definition LVS_END_DATA(type_name) ends data type definition

Data members of the given type shall be placed between start and end in a form of the regular C structure fields, i.e.:

LVS_START_DATA(example)
  int i;
  char* cptr;
  struct complex_t c;
LVS_END_DATA(example)

Defined types can be used to declare memory pools with:

LVS_DEFINE_DATA_POOL(name, type, quantity) where name is the name of the pool, type is the data type declared like above, quantity is the number of elements in the pool. The definition is to be made in a global context of C file.

LVS_CREATE_DATA_POOL(name) must be run in an initialization function to setup the given memory pool.

LVS_USE_DATA_POOL(name) is to use memory pool from outside the C file where the pool is defined.

LVS_ALLOCATE(name) allocates a free item from the given pool. If all items are previously allocated, returns NULL. Returns a pointer to the LVS_DATA(name) type.

LVS_DEALLOCATE(data) deallocates previously allocated data. Uses the pointer returned by LVS_ALLOCATE.

LVS_DATA(name) is used as a type definition for a data type of the given pool. Type name defined by LVS_START_DATA/LVS_END_DATA and used in LVS_DEFINE_DATA_POOL is used as the parameter.

Clone this wiki locally