From cb605a619b96b9261994f28f4af0f3c7e77f0892 Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Thu, 16 May 2024 15:46:02 -0700 Subject: test harness includes move_buddy --- README | 2 +- imc/checker.c | 23 +++++++++++++++++------ magic_buddy/magic_buddy.c | 3 ++- magic_buddy/magic_buddy.h | 2 +- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/README b/README index ad827a4..b350f4f 100644 --- a/README +++ b/README @@ -65,10 +65,10 @@ Current status of the test harness(es): - [X] allocate - [X] liberate - [X] reallocate + - [X] move_buddy - [ ] reserve - [ ] grow_buddy - [ ] shrink_buddy - - [ ] move_buddy ### Usage diff --git a/imc/checker.c b/imc/checker.c index 2a519ef..2bde59f 100644 --- a/imc/checker.c +++ b/imc/checker.c @@ -35,15 +35,26 @@ void check_main() { if (choose(2, 0)) pool_size += 11; if (choose(2, 0)) pool = (void*)((uint8_t*)pool + 11); - struct buddy buddy; + struct buddy buddy1; + struct buddy buddy2; + struct buddy *curr_buddy = &buddy1; uint8_t magic[MAGIC_COOKIE_BYTES]; get_random(magic, MAGIC_COOKIE_BYTES); - init_buddy(pool, pool_size, magic, &buddy); + init_buddy(pool, pool_size, magic, curr_buddy); size_t size_options[] = {1, 128, 1024, 5, 167, 10500}; const size_t n_size_options = sizeof(size_options) / sizeof(size_options[0]); for (int t = 0; t < n_timesteps; t++) { + int which_buddy = choose(3, 0); + if (which_buddy == 1) { + move_buddy(&buddy1, curr_buddy); + curr_buddy = &buddy1; + } else if (which_buddy == 2) { + move_buddy(&buddy2, curr_buddy); + curr_buddy = &buddy2; + } + // free the regions at this timestep, and ensure its contents are good for (struct ll *p = time_to_dielist[t]; p;) { for (size_t i = 0; i < p->data_size; i++) { @@ -56,12 +67,12 @@ void check_main() { if (action == n_size_options) { // do a free! struct ll *next = p->next; - liberate(p, p->data_size + sizeof(struct ll), &buddy); + liberate(p, p->data_size + sizeof(struct ll), curr_buddy); p = next; } else if (action == n_size_options + 1) { // do a free, but with reallocate struct ll *next = p->next; - p = reallocate(p, p->data_size + sizeof(struct ll), 0, &buddy); + p = reallocate(p, 0, p->data_size + sizeof(struct ll), curr_buddy); assert(!p); p = next; } else { @@ -70,7 +81,7 @@ void check_main() { size_t old_size = p->data_size + sizeof(struct ll); p->data_size = size_options[action]; size_t new_size = p->data_size + sizeof(struct ll); - p = reallocate(p, old_size, new_size, &buddy); + p = reallocate(p, new_size, old_size, curr_buddy); for (size_t i = 0; i < p->data_size; i++) p->data[i] = (uint8_t)p->data_size; int lifetime = choose(n_timesteps - t, 0) + 1; @@ -84,7 +95,7 @@ void check_main() { // try allocating a region size_t size = size_options[choose(n_size_options, 0)]; - struct ll *ll = allocate(size + sizeof(struct ll), &buddy); + struct ll *ll = allocate(size + sizeof(struct ll), curr_buddy); if (!ll) continue; assert((void*)ll >= pool); assert((uint8_t*)ll + size + sizeof(struct ll) diff --git a/magic_buddy/magic_buddy.c b/magic_buddy/magic_buddy.c index c6283d8..9deaa8f 100644 --- a/magic_buddy/magic_buddy.c +++ b/magic_buddy/magic_buddy.c @@ -197,7 +197,7 @@ static void *_naive_reallocate(void *old, size_t old_size, size_t new_size, return new; } -void *reallocate(void *old, size_t old_size, size_t new_size, +void *reallocate(void *old, size_t new_size, size_t old_size, struct buddy *state) { if (new_size == 0) return liberate(old, old_size, state), (void*)0; @@ -316,6 +316,7 @@ int shrink_buddy(size_t new_size, struct buddy *state) { } void move_buddy(struct buddy *new_state, struct buddy *old_state) { + if (new_state == old_state) return; memcpy(new_state, old_state, sizeof(struct buddy)); for (size_t i = 0; i < ADDRESS_BITS; i++) { if (!new_state->avail[i]) continue; diff --git a/magic_buddy/magic_buddy.h b/magic_buddy/magic_buddy.h index c8778d6..a25ceb7 100644 --- a/magic_buddy/magic_buddy.h +++ b/magic_buddy/magic_buddy.h @@ -30,7 +30,7 @@ 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, +void *reallocate(void *old, size_t new_size, size_t old_size, struct buddy *state); // Attempts to reserve a range [@start,@start+@size). -- cgit v1.2.3