Gama C Library
Gama C API Documentation
malloc.h File Reference

Custom memory allocation functions (malloc, free, calloc, realloc) using a fixed-size static memory pool. More...

#include <stdlib.h>
#include <stddef.h>

Go to the source code of this file.

Data Structures

struct  _memory_spot

Macros

#define GM_MALLOC
#define MEMORY   10
#define MEMORY_B   0
#define MEMORY_TOTAL   ((MEMORY << 20) + MEMORY_B)
#define MEMORY_SPOTS   (MEMORY_TOTAL / 100)
#define _MALLOC_H   1

Functions

void * malloc (size_t size)
 Custom implementation of malloc using a static memory pool.
void free (void *ptr)
 Custom implementation of free for memory allocated by malloc (this custom version).
void * calloc (size_t count, size_t size)
 Custom implementation of calloc using a static memory pool.
void * realloc (void *ptr, size_t size)
 Custom implementation of realloc for memory allocated by malloc (this custom version).

Detailed Description

Custom memory allocation functions (malloc, free, calloc, realloc) using a fixed-size static memory pool.

This file provides an alternative memory management system for Gama, primarily for environments where dynamic system malloc might be unavailable or undesirable (e.g., embedded systems, WebAssembly with specific memory requirements). It pre-allocates a large static buffer and manages memory chunks within it.

Warning
This implementation redefines standard C library functions (malloc, free, calloc, realloc). Care must be taken to ensure this does not conflict with system-level memory allocation or other libraries that expect standard libc behavior. This file is typically included conditionally.

Macro Definition Documentation

◆ _MALLOC_H

#define _MALLOC_H   1

◆ GM_MALLOC

#define GM_MALLOC

◆ MEMORY

#define MEMORY   10

◆ MEMORY_B

#define MEMORY_B   0

◆ MEMORY_SPOTS

#define MEMORY_SPOTS   (MEMORY_TOTAL / 100)

◆ MEMORY_TOTAL

#define MEMORY_TOTAL   ((MEMORY << 20) + MEMORY_B)

Function Documentation

◆ calloc()

void * calloc ( size_t count,
size_t size )

Custom implementation of calloc using a static memory pool.

Allocates a block of memory for an array of count elements, each of size bytes, and initializes all bytes in the allocated block to zero.

Parameters
countThe number of elements to allocate.
sizeThe size of each element in bytes.
Returns
A pointer to the allocated and zero-initialized memory, or NULL if allocation fails.

◆ free()

void free ( void * ptr)

Custom implementation of free for memory allocated by malloc (this custom version).

Frees a previously allocated memory block, making it available for future allocations.

Parameters
ptrA pointer to the memory block to free. If ptr is NULL, no operation is performed.

◆ malloc()

void * malloc ( size_t size)

Custom implementation of malloc using a static memory pool.

Allocates a block of size bytes from the predefined static memory pool.

Parameters
sizeThe number of bytes to allocate.
Returns
A pointer to the allocated memory block, or NULL if allocation fails (e.g., out of memory or no suitable spot).

◆ realloc()

void * realloc ( void * ptr,
size_t size )

Custom implementation of realloc for memory allocated by malloc (this custom version).

Resizes a previously allocated memory block.

Parameters
ptrA pointer to the memory block to reallocate. If ptr is NULL, behaves like malloc.
sizeThe new size for the memory block. If size is 0, behaves like free.
Returns
A pointer to the reallocated memory block, or NULL if reallocation fails.