|
| 1 | +#include "m61gc.h" |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <string.h> |
| 5 | +#include <assert.h> |
| 6 | + |
| 7 | + |
| 8 | +// In this exercise, we track allocations in an array sorted by allocated |
| 9 | +// pointer (not a linked list). The sorted array lets us quickly find the |
| 10 | +// allocation containing a pointer, using binary search. |
| 11 | + |
| 12 | +// structure representing a single allocation |
| 13 | +typedef struct allocation { |
| 14 | + char* ptr; // pointer to first allocated byte |
| 15 | + size_t sz; // size of allocation |
| 16 | + int marked; // used in GC |
| 17 | +} allocation; |
| 18 | + |
| 19 | +static allocation* allocs = NULL; // active allocations (sorted by ptr) |
| 20 | +static size_t nallocs = 0; // number of active allocations |
| 21 | +static size_t allocs_capacity = 0; // capacity of `allocs` array |
| 22 | + |
| 23 | +char* m61_stack_bottom; |
| 24 | + |
| 25 | +/// find_allocation_index(ptr) |
| 26 | +/// Return the index in the `allocs` array where `ptr` belongs. |
| 27 | +/// Uses binary search for speed. |
| 28 | +static size_t find_allocation_index(char* ptr); |
| 29 | + |
| 30 | +/// find_allocation(ptr) |
| 31 | +/// Return a pointer to the active allocation containing `ptr`, or |
| 32 | +/// NULL if no active allocation contains `ptr`. |
| 33 | +static allocation* find_allocation(char* ptr); |
| 34 | + |
| 35 | +/// allocation_atexit() |
| 36 | +/// Used to clean up the `allocs` array. |
| 37 | +static void allocation_atexit(void) { |
| 38 | + free(allocs); |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +void* m61_malloc(size_t sz) { |
| 43 | + static size_t allocation_count = 0; |
| 44 | + ++allocation_count; |
| 45 | + |
| 46 | + void* ptr = malloc(sz); |
| 47 | + // Garbage collect every 2**16 allocations, or sooner if malloc fails |
| 48 | + if (!ptr || allocation_count % (1U << 16) == 0) { |
| 49 | + m61_gc(); |
| 50 | + ptr = malloc(sz); |
| 51 | + } |
| 52 | + if (!ptr) |
| 53 | + return NULL; |
| 54 | + |
| 55 | + // find index to insert this allocation |
| 56 | + size_t i = find_allocation_index(ptr); |
| 57 | + // this ptr must not overlap with a current allocation |
| 58 | + assert(i == nallocs || (char*) ptr + sz <= allocs[i].ptr); |
| 59 | + // make space for the new allocation |
| 60 | + if (nallocs == allocs_capacity) { |
| 61 | + if (!allocs_capacity) { |
| 62 | + atexit(allocation_atexit); // free allocations array on exit |
| 63 | + allocs_capacity = 1024; |
| 64 | + } else |
| 65 | + allocs_capacity *= 2; |
| 66 | + allocs = realloc(allocs, sizeof(allocation) * allocs_capacity); |
| 67 | + assert(allocs); |
| 68 | + } |
| 69 | + memmove(&allocs[i + 1], &allocs[i], sizeof(allocation) * (nallocs - i)); |
| 70 | + // store the new allocation |
| 71 | + allocs[i].ptr = ptr; |
| 72 | + allocs[i].sz = sz; |
| 73 | + ++nallocs; |
| 74 | + // clear and return it |
| 75 | + memset(ptr, 0, sz); |
| 76 | + return ptr; |
| 77 | +} |
| 78 | + |
| 79 | +void m61_free(void* ptr) { |
| 80 | + if (!ptr) |
| 81 | + return; |
| 82 | + // find index of this allocation |
| 83 | + size_t i = find_allocation_index(ptr); |
| 84 | + // that allocation must match `ptr` exactly |
| 85 | + assert(i < nallocs && allocs[i].ptr == ptr); |
| 86 | + // remove the allocation from the list and free `ptr` |
| 87 | + memmove(&allocs[i], &allocs[i + 1], sizeof(allocation) * (nallocs - i - 1)); |
| 88 | + --nallocs; |
| 89 | + free(ptr); |
| 90 | +} |
| 91 | + |
| 92 | +void m61_print_allocations(void) { |
| 93 | + printf("%zu allocations\n", nallocs); |
| 94 | + for (size_t i = 0; i != nallocs; ++i) |
| 95 | + printf(" #%zu: %p: %zu bytes\n", i, allocs[i].ptr, allocs[i].sz); |
| 96 | +} |
| 97 | + |
| 98 | +static void mark_allocations(const char* base, size_t sz) { |
| 99 | + (void) base; |
| 100 | + if (sz < sizeof(void*)) |
| 101 | + return; |
| 102 | + for (size_t i = 0; i <= sz - sizeof(void*); ++i) { |
| 103 | + // check if the data at `base + i` contains a pointer |
| 104 | + // YOUR CODE HERE |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +void m61_gc(void) { |
| 109 | +#if __x86_64__ |
| 110 | + // ensure all of our callers' variables are located on the stack, |
| 111 | + // rather than in registers |
| 112 | + __asm__ __volatile__("" : : : "rbx", "r12", "r13", "r14", "r15", "memory"); |
| 113 | +#endif |
| 114 | + |
| 115 | + char* stack_top = (char*) __builtin_frame_address(0); |
| 116 | + |
| 117 | + // unmark all active allocations |
| 118 | + // YOUR CODE HERE |
| 119 | + |
| 120 | + // mark allocations in the stack |
| 121 | + mark_allocations(stack_top, m61_stack_bottom - stack_top); |
| 122 | + |
| 123 | +#if __linux__ |
| 124 | + // mark allocations in globals |
| 125 | + extern char data_start[]; |
| 126 | + extern char _end[]; |
| 127 | + mark_allocations(data_start, _end - data_start); |
| 128 | +#endif |
| 129 | + |
| 130 | + // free unmarked allocations |
| 131 | + // YOUR CODE HERE |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +//////////////////////////////////////////////////////////////////////////////// |
| 136 | +/// Helper method definitions |
| 137 | + |
| 138 | +static size_t find_allocation_index(char* ptr) { |
| 139 | + size_t l = 0, r = nallocs; |
| 140 | + while (l < r) { |
| 141 | + size_t m = l + (r - l) / 2; |
| 142 | + if (ptr < allocs[m].ptr) |
| 143 | + r = m; |
| 144 | + else if (ptr >= allocs[m].ptr + allocs[m].sz) |
| 145 | + l = m + 1; |
| 146 | + else |
| 147 | + return m; |
| 148 | + } |
| 149 | + return l; |
| 150 | +} |
| 151 | + |
| 152 | +static allocation* find_allocation(char* ptr) __attribute__((used)); |
| 153 | +static allocation* find_allocation(char* ptr) { |
| 154 | + size_t i = find_allocation_index(ptr); |
| 155 | + assert(i == nallocs || ptr < allocs[i].ptr + allocs[i].sz); |
| 156 | + if (i < nallocs && ptr >= allocs[i].ptr) |
| 157 | + return &allocs[i]; |
| 158 | + else |
| 159 | + return NULL; |
| 160 | +} |
0 commit comments