diff options
-rw-r--r-- | core/mem_region.c | 2 | ||||
-rw-r--r-- | core/test/run-malloc.c | 25 |
2 files changed, 25 insertions, 2 deletions
diff --git a/core/mem_region.c b/core/mem_region.c index 8904a18..dd1f64f 100644 --- a/core/mem_region.c +++ b/core/mem_region.c @@ -399,7 +399,7 @@ void mem_free(struct mem_region *region, void *mem, const char *location) size_t mem_size(const struct mem_region *region __unused, const void *ptr) { const struct alloc_hdr *hdr = ptr - sizeof(*hdr); - return hdr->num_longs * sizeof(long); + return hdr->num_longs * sizeof(long) - sizeof(struct alloc_hdr); } bool mem_resize(struct mem_region *region, void *mem, size_t len, diff --git a/core/test/run-malloc.c b/core/test/run-malloc.c index 226ce75..080e689 100644 --- a/core/test/run-malloc.c +++ b/core/test/run-malloc.c @@ -25,6 +25,23 @@ struct cpu_thread { unsigned int chip_id; }; +#include <stdlib.h> + +/* Use these before we undefine them below. */ +static inline void *real_malloc(size_t size) +{ + return malloc(size); +} + +static inline void real_free(void *p) +{ + return free(p); +} + +#undef malloc +#undef free +#undef realloc + #include <skiboot.h> #define is_rodata(p) true @@ -60,7 +77,8 @@ static bool heap_empty(void) int main(void) { - char test_heap[TEST_HEAP_SIZE], *p, *p2, *p3, *p4; + char *test_heap = real_malloc(TEST_HEAP_SIZE); + char *p, *p2, *p3, *p4; size_t i; /* Use malloc for the heap, so valgrind can find issues. */ @@ -116,13 +134,17 @@ int main(void) /* Realloc with move. */ p2 = malloc(TEST_HEAP_SIZE - 64 - sizeof(struct alloc_hdr)*2); + memset(p2, 'a', TEST_HEAP_SIZE - 64 - sizeof(struct alloc_hdr)*2); assert(p2); p = malloc(64); + memset(p, 'b', 64); + p[63] = 'c'; assert(p); free(p2); p2 = realloc(p, 128); assert(p2 != p); + assert(p2[63] == 'c'); free(p2); assert(heap_empty()); assert(!mem_region_lock.lock_val); @@ -140,5 +162,6 @@ int main(void) assert(heap_empty()); assert(!mem_region_lock.lock_val); + real_free(test_heap); return 0; } |