2024-09-18 00:46:36 -04:00
|
|
|
#ifndef GC_MALLOC_H
|
|
|
|
#define GC_MALLOC_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2024-09-18 13:54:53 -04:00
|
|
|
#include <stdio.h>
|
2024-09-18 00:46:36 -04:00
|
|
|
#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
|
|
|
|
|
2024-09-18 13:54:53 -04:00
|
|
|
#define ALIGN 8 /* Number of bytes to which chunk size is aligned */
|
2024-09-18 00:46:36 -04:00
|
|
|
#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 */
|
|
|
|
|
|
|
|
/*
|
2024-09-18 02:02:44 -04:00
|
|
|
* Metadata and user data for allocated memory
|
2024-09-18 00:46:36 -04:00
|
|
|
*
|
2024-09-18 13:54:53 -04:00
|
|
|
* footer: size of previous block or user data of previous block if allocated
|
2024-09-18 00:46:36 -04:00
|
|
|
* size: size of chunk in bytes
|
2024-09-18 13:54:53 -04:00
|
|
|
* merge: flag to identify when the chunk can be coalesced,
|
|
|
|
* possible values 0 (false, don't merge) or non-zero (true, do merge)
|
|
|
|
* free: flag to identify when the chunk is free,
|
|
|
|
* possible values 0 (false, allocated) or non-zero (true, chunk is free)
|
2024-09-18 00:46:36 -04:00
|
|
|
* 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;
|
2024-09-18 13:54:53 -04:00
|
|
|
uint16_t merge; /* 1 = do merge, initialize to 0 */
|
|
|
|
uint16_t free; /* 0 = allocated, initialize to 1 */
|
2024-09-18 00:46:36 -04:00
|
|
|
struct gc_chunk *next;
|
|
|
|
struct gc_chunk *prev;
|
|
|
|
};
|
|
|
|
|
2024-09-18 02:02:44 -04:00
|
|
|
// Gets a pointer to the next chunk
|
2024-09-18 13:54:53 -04:00
|
|
|
struct gc_chunk *find_next_chunk(struct gc_chunk *curr);
|
2024-09-18 02:02:44 -04:00
|
|
|
|
|
|
|
// Gets a pointer to the previous chunk, ensuring that current can be coalesced
|
2024-09-18 13:54:53 -04:00
|
|
|
struct gc_chunk *find_prev_chunk(struct gc_chunk *curr);
|
2024-09-18 02:02:44 -04:00
|
|
|
|
2024-09-18 13:54:53 -04:00
|
|
|
// Updates chunk footer information (remember that the current chunk's footer
|
|
|
|
// data is actually part of the next chunk)
|
2024-09-18 02:02:44 -04:00
|
|
|
void set_footer(struct gc_chunk *curr);
|
|
|
|
|
2024-09-18 13:54:53 -04:00
|
|
|
// Gets footer information for the current chunk - useful for debugging
|
2024-09-18 17:22:21 -04:00
|
|
|
uint64_t get_footer(struct gc_chunk *curr);
|
2024-09-18 02:02:44 -04:00
|
|
|
|
2024-09-18 13:54:53 -04:00
|
|
|
// Creates a new chunk with sentinel
|
|
|
|
struct gc_chunk *init_gc_chunk(void);
|
|
|
|
|
2024-09-18 17:22:21 -04:00
|
|
|
// Gets a pointer to a suitable chunk (first fit) from the free list, otherwise
|
|
|
|
// creates a new chunk and returns it's pointer
|
2024-09-18 13:54:53 -04:00
|
|
|
struct gc_chunk *find_free_chunk(size_t size);
|
|
|
|
|
|
|
|
// Coalesces chunks when merge flag is set
|
|
|
|
void merge_chunk(struct gc_chunk *curr);
|
|
|
|
|
2024-09-18 00:46:36 -04:00
|
|
|
// Performs allocations of SIZE bytes
|
|
|
|
void *gc_malloc(size_t size);
|
|
|
|
|
2024-09-18 17:22:21 -04:00
|
|
|
// Frees allocations for re-use
|
2024-09-18 00:46:36 -04:00
|
|
|
void gc_free(void *ptr);
|
|
|
|
|
2024-09-18 17:22:21 -04:00
|
|
|
// Completely de-allocates the heap (break glass in case of emergency)
|
|
|
|
void destroy_heap(struct gc_chunk *freelist);
|
|
|
|
|
2024-09-18 00:46:36 -04:00
|
|
|
#endif /* GC_MALLOC_H */
|