Minus One Header
typedef struct { size_t size; unsigned magic; } header_t;
void *my_malloc(size_t n) {
header_t *h = carve_chunk(n + sizeof(header_t));
h->size = n;
h->magic = 0x1234567;
return (void *)(h + 1);
}
void my_free(void *ptr) {
header_t *h = /* ??? */;
assert(h->magic == 0x1234567);
return_to_free_list(h, h->size + sizeof(header_t));
}
sizeof(header_t) is 16 on this platform (8-byte size_t, 4-byte unsigned, 4 bytes of padding). What belongs at ????
Sign in to answer questions and track your progress
Sign In