From 92996a3671732b6c883b325414a1e313786d48d6 Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Thu, 16 May 2024 15:15:40 -0700 Subject: checker --- magic_buddy/buddy.h | 55 ----------------------------------------------- magic_buddy/magic_buddy.c | 2 +- magic_buddy/magic_buddy.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 56 deletions(-) delete mode 100644 magic_buddy/buddy.h create mode 100644 magic_buddy/magic_buddy.h (limited to 'magic_buddy') diff --git a/magic_buddy/buddy.h b/magic_buddy/buddy.h deleted file mode 100644 index c8778d6..0000000 --- a/magic_buddy/buddy.h +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once -#include -#include - -#define MAGIC_COOKIE_BYTES 32 -#define ADDRESS_BITS (8 * sizeof(void*)) - -struct buddy { - uint8_t magic[MAGIC_COOKIE_BYTES]; - struct free_block *(avail[ADDRESS_BITS]); - size_t root_logsize; - void *base; -}; - -// Initialize a buddy with (constant) global state stored in @state. -// NOTE: after initializing the buddy, you should always pass the exact same -// pointer in for @state in future calls. If you need to move @state, use -// move_buddy(...). -void init_buddy(uint8_t *base, size_t size, uint8_t magic[MAGIC_COOKIE_BYTES], - struct buddy *state); - -// Allocate a block of size >= @size -void *allocate(size_t size, struct buddy *state); - -// Liberate a block of size @size starting at @base. -void liberate(void *base, size_t size, struct buddy *state); - -// Print debug information for the allocator. -void debug_buddy(struct buddy *state); - -// Simulates @new = allocate, memcpy(@new, @old), free(@old), with some -// optimizations for cases where the reallocation can be done in place. -void *reallocate(void *old, size_t old_size, size_t new_size, - struct buddy *state); - -// Attempts to reserve a range [@start,@start+@size). -// Returns 1 if success, 0 otherwise. -// Whenever possible, we avoid writing anything into the reserved region. -int reserve(void *start, size_t size, struct buddy *state); - -// Update @state to assume the memory pool has been copied to -// [@new_base,@new_base+@new_size) -// Can *ONLY* be used when @new_size >= the existing size. -void grow_buddy(uint8_t *new_base, size_t new_size, struct buddy *state); - -// Update @state to only use the subset of the pool in range -// [@state->base,@state->base+new_size) -// Can *ONLY* be used when @new_size <= the existing size. -// This *CAN* write to anything in the old pool. -// This *CAN* fail, in which case everything is unmodified and 0 is returned. -// Upon success, 1 is returned. -int shrink_buddy(size_t new_size, struct buddy *state); - -// Used to move the global state of the buddy. -void move_buddy(struct buddy *new_state, struct buddy *old_state); diff --git a/magic_buddy/magic_buddy.c b/magic_buddy/magic_buddy.c index 80bc069..c6283d8 100644 --- a/magic_buddy/magic_buddy.c +++ b/magic_buddy/magic_buddy.c @@ -1,7 +1,7 @@ #include #include #include -#include "buddy.h" +#include "magic_buddy.h" struct free_block { struct free_block *next; diff --git a/magic_buddy/magic_buddy.h b/magic_buddy/magic_buddy.h new file mode 100644 index 0000000..c8778d6 --- /dev/null +++ b/magic_buddy/magic_buddy.h @@ -0,0 +1,55 @@ +#pragma once +#include +#include + +#define MAGIC_COOKIE_BYTES 32 +#define ADDRESS_BITS (8 * sizeof(void*)) + +struct buddy { + uint8_t magic[MAGIC_COOKIE_BYTES]; + struct free_block *(avail[ADDRESS_BITS]); + size_t root_logsize; + void *base; +}; + +// Initialize a buddy with (constant) global state stored in @state. +// NOTE: after initializing the buddy, you should always pass the exact same +// pointer in for @state in future calls. If you need to move @state, use +// move_buddy(...). +void init_buddy(uint8_t *base, size_t size, uint8_t magic[MAGIC_COOKIE_BYTES], + struct buddy *state); + +// Allocate a block of size >= @size +void *allocate(size_t size, struct buddy *state); + +// Liberate a block of size @size starting at @base. +void liberate(void *base, size_t size, struct buddy *state); + +// Print debug information for the allocator. +void debug_buddy(struct buddy *state); + +// Simulates @new = allocate, memcpy(@new, @old), free(@old), with some +// optimizations for cases where the reallocation can be done in place. +void *reallocate(void *old, size_t old_size, size_t new_size, + struct buddy *state); + +// Attempts to reserve a range [@start,@start+@size). +// Returns 1 if success, 0 otherwise. +// Whenever possible, we avoid writing anything into the reserved region. +int reserve(void *start, size_t size, struct buddy *state); + +// Update @state to assume the memory pool has been copied to +// [@new_base,@new_base+@new_size) +// Can *ONLY* be used when @new_size >= the existing size. +void grow_buddy(uint8_t *new_base, size_t new_size, struct buddy *state); + +// Update @state to only use the subset of the pool in range +// [@state->base,@state->base+new_size) +// Can *ONLY* be used when @new_size <= the existing size. +// This *CAN* write to anything in the old pool. +// This *CAN* fail, in which case everything is unmodified and 0 is returned. +// Upon success, 1 is returned. +int shrink_buddy(size_t new_size, struct buddy *state); + +// Used to move the global state of the buddy. +void move_buddy(struct buddy *new_state, struct buddy *old_state); -- cgit v1.2.3