mirror of
https://codeberg.org/andyscott/marCsweep.git
synced 2024-11-09 22:01:01 -05:00
Compare commits
3 commits
15e07e2c9a
...
37d5cfd532
Author | SHA1 | Date | |
---|---|---|---|
37d5cfd532 | |||
f80156344b | |||
00f62da35c |
4 changed files with 93 additions and 21 deletions
|
@ -1,19 +0,0 @@
|
||||||
#ifndef GC_ALLOC_H
|
|
||||||
#define GC_ALLOC_H
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
// Metadata for allocated memory
|
|
||||||
struct gc_block_header {
|
|
||||||
size_t size;
|
|
||||||
int free;
|
|
||||||
struct gc_block_header *next;
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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_ALLOC_H */
|
|
51
include/gc_malloc.h
Normal file
51
include/gc_malloc.h
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
#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 and user data 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;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Gets a pointer to the next chunk
|
||||||
|
struct gc_chunk *get_next_chunk(struct gc_chunk *curr);
|
||||||
|
|
||||||
|
// Gets a pointer to the previous chunk, ensuring that current can be coalesced
|
||||||
|
struct gc_chunk *get_prev_chunk(struct gc_chunk *curr);
|
||||||
|
|
||||||
|
// Updates chunk footer information
|
||||||
|
void set_footer(struct gc_chunk *curr);
|
||||||
|
|
||||||
|
// Gets footer information for the current chunk
|
||||||
|
int get_footer(struct gc_chunk *curr);
|
||||||
|
|
||||||
|
// 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 */
|
|
@ -1,2 +0,0 @@
|
||||||
#include "gc_alloc.h"
|
|
||||||
|
|
42
src/gc_malloc.c
Normal file
42
src/gc_malloc.c
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
|
||||||
|
#include "gc_malloc.h"
|
||||||
|
|
||||||
|
// Hides chunk header
|
||||||
|
static inline void *hide(struct gc_chunk *curr)
|
||||||
|
{
|
||||||
|
return (void *)((uint64_t *)curr + MAGIC);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reveals chunk header
|
||||||
|
static inline struct gc_chunk *magic(void *mem)
|
||||||
|
{
|
||||||
|
return (struct gc_chunk *)((uint64_t *)mem - MAGIC);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Aligns the amount of memory requested by the user with chunk size
|
||||||
|
static inline size_t align(size_t size)
|
||||||
|
{
|
||||||
|
const size_t alignment = 8;
|
||||||
|
size = (size + (alignment - 1)) & ~(alignment - 1);
|
||||||
|
|
||||||
|
if (size <= MIN)
|
||||||
|
return MIN;
|
||||||
|
else
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct gc_chunk *get_next_chunk(struct gc_chunk *curr)
|
||||||
|
{
|
||||||
|
unsigned size = curr->size;
|
||||||
|
return (struct gc_chunk *)((uint64_t *)curr + MAGIC + (size - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
struct gc_chunk *get_prev_chunk(struct gc_chunk *curr)
|
||||||
|
{
|
||||||
|
assert(curr->merge);
|
||||||
|
unsigned size = curr->footer;
|
||||||
|
return (struct gc_chunk *)((uint64_t *)curr - (size - 1) - MAGIC);
|
||||||
|
}
|
Loading…
Reference in a new issue