marCsweep/include/gc_malloc.h

40 lines
1.1 KiB
C
Raw Normal View History

#ifndef GC_MALLOC_H
#define GC_MALLOC_H
#include <stdint.h>
#include <unistd.h>
// Generally, inline functions are preferable to macros resembling functions.
// https://www.kernel.org/doc/html/v4.10/process/coding-style.html#data-structures
#define OVERHEAD 1 /* Number of 8-byte segments for chunk size & flags */
#define MAGIC 2 /* Number of 8-byte segments from top of chunk to fields */
#define MIN 3 /* Minimum number of 8-byte segments for data in a chunk */
/*
* Metadata for allocated memory
*
* footer: user data for previous chunk
* size: size of chunk in bytes
* merge: flag to identify when the chunk can be coalesced
* free: flag to identify when the chunk is free
* next: pointer to next chunk in the free list
* prev: pointer to previous chunk in the free list
*/
struct gc_chunk {
uint64_t footer;
uint32_t size;
uint16_t merge;
uint16_t free;
struct gc_chunk *next;
struct gc_chunk *prev;
};
// Performs allocations of SIZE bytes
void *gc_malloc(size_t size);
// Frees blocks of memory allocated by 'gc_alloc'
void gc_free(void *ptr);
#endif /* GC_MALLOC_H */