|
| 1 | +/** |
| 2 | + * Copyright (c) 2012 MIT License by 6.172 Staff |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to |
| 6 | + * deal in the Software without restriction, including without limitation the |
| 7 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 | + * IN THE SOFTWARE. |
| 21 | + **/ |
| 22 | + |
| 23 | +#include <stdio.h> |
| 24 | +#include <stdint.h> |
| 25 | +#include <cstdlib> |
| 26 | +#include <cstring> |
| 27 | +#include "./allocator_interface.h" |
| 28 | +#include "./memlib.h" |
| 29 | + |
| 30 | +// All blocks must have a specified minimum alignment. |
| 31 | +#define ALIGNMENT 8 |
| 32 | + |
| 33 | +// Rounds up to the nearest multiple of ALIGNMENT. |
| 34 | +#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~(ALIGNMENT-1)) |
| 35 | + |
| 36 | +// The smallest aligned size that will hold a size_t value. |
| 37 | +#define SIZE_T_SIZE (ALIGN(sizeof(size_t))) |
| 38 | + |
| 39 | +namespace my { |
| 40 | + // check - This checks our invariant that the size_t header before every |
| 41 | + // block points to either the beginning of the next block, or the end of the |
| 42 | + // heap. |
| 43 | + int allocator::check() { |
| 44 | + char *p; |
| 45 | + char *lo = (char*)mem_heap_lo(); |
| 46 | + char *hi = (char*)mem_heap_hi() + 1; |
| 47 | + size_t size = 0; |
| 48 | + |
| 49 | + p = lo; |
| 50 | + while (lo <= p && p < hi) { |
| 51 | + size = ALIGN(*(size_t*)p + SIZE_T_SIZE); |
| 52 | + p += size; |
| 53 | + } |
| 54 | + |
| 55 | + if (p != hi) { |
| 56 | + printf("Bad headers did not end at heap_hi!\n"); |
| 57 | + printf("heap_lo: %p, heap_hi: %p, size: %lu, p: %p\n", lo, hi, size, p); |
| 58 | + return -1; |
| 59 | + } |
| 60 | + |
| 61 | + return 0; |
| 62 | + } |
| 63 | + |
| 64 | + // init - Initialize the malloc package. Called once before any other |
| 65 | + // calls are made. Since this is a very simple implementation, we just |
| 66 | + // return success. |
| 67 | + int allocator::init() { |
| 68 | + return 0; |
| 69 | + } |
| 70 | + |
| 71 | + // malloc - Allocate a block by incrementing the brk pointer. |
| 72 | + // Always allocate a block whose size is a multiple of the alignment. |
| 73 | + void * allocator::malloc(size_t size) { |
| 74 | + // We allocate a little bit of extra memory so that we can store the |
| 75 | + // size of the block we've allocated. Take a look at realloc to see |
| 76 | + // one example of a place where this can come in handy. |
| 77 | + int aligned_size = ALIGN(size + SIZE_T_SIZE); |
| 78 | + |
| 79 | + // Expands the heap by the given number of bytes and returns a pointer to |
| 80 | + // the newly-allocated area. This is a slow call, so you will want to |
| 81 | + // make sure you don't wind up calling it on every malloc. |
| 82 | + void *p = mem_sbrk(aligned_size); |
| 83 | + |
| 84 | + if (p == (void *)-1) { |
| 85 | + // Whoops, an error of some sort occurred. We return NULL to let |
| 86 | + // the client code know that we weren't able to allocate memory. |
| 87 | + return NULL; |
| 88 | + } else { |
| 89 | + // We store the size of the block we've allocated in the first |
| 90 | + // SIZE_T_SIZE bytes. |
| 91 | + *(size_t*)p = size; |
| 92 | + |
| 93 | + // Then, we return a pointer to the rest of the block of memory, |
| 94 | + // which is at least size bytes long. We have to cast to uint8_t |
| 95 | + // before we try any pointer arithmetic because voids have no size |
| 96 | + // and so the compiler doesn't know how far to move the pointer. |
| 97 | + // Since a uint8_t is always one byte, adding SIZE_T_SIZE after |
| 98 | + // casting advances the pointer by SIZE_T_SIZE bytes. |
| 99 | + return (void *)((char *)p + SIZE_T_SIZE); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // free - Freeing a block does nothing. |
| 104 | + void allocator::free(void *ptr) { |
| 105 | + } |
| 106 | + |
| 107 | + // realloc - Implemented simply in terms of malloc and free |
| 108 | + void * allocator::realloc(void *ptr, size_t size) { |
| 109 | + void *newptr; |
| 110 | + size_t copy_size; |
| 111 | + |
| 112 | + // Allocate a new chunk of memory, and fail if that allocation fails. |
| 113 | + newptr = malloc(size); |
| 114 | + if (NULL == newptr) |
| 115 | + return NULL; |
| 116 | + |
| 117 | + // Get the size of the old block of memory. Take a peek at malloc(), |
| 118 | + // where we stashed this in the SIZE_T_SIZE bytes directly before the |
| 119 | + // address we returned. Now we can back up by that many bytes and read |
| 120 | + // the size. |
| 121 | + copy_size = *(size_t*)((uint8_t*)ptr - SIZE_T_SIZE); |
| 122 | + |
| 123 | + // If the new block is smaller than the old one, we have to stop copying |
| 124 | + // early so that we don't write off the end of the new block of memory. |
| 125 | + if (size < copy_size) |
| 126 | + copy_size = size; |
| 127 | + |
| 128 | + // This is a standard library call that performs a simple memory copy. |
| 129 | + std::memcpy(newptr, ptr, copy_size); |
| 130 | + |
| 131 | + // Release the old block. |
| 132 | + free(ptr); |
| 133 | + |
| 134 | + // Return a pointer to the new block. |
| 135 | + return newptr; |
| 136 | + } |
| 137 | + |
| 138 | + // call mem_reset_brk. |
| 139 | + void allocator::reset_brk() { |
| 140 | + mem_reset_brk(); |
| 141 | + } |
| 142 | + |
| 143 | + // call mem_heap_lo |
| 144 | + void * allocator::heap_lo() { |
| 145 | + return mem_heap_lo(); |
| 146 | + } |
| 147 | + |
| 148 | + // call mem_heap_hi |
| 149 | + void * allocator::heap_hi() { |
| 150 | + return mem_heap_hi(); |
| 151 | + } |
| 152 | +}; |
0 commit comments