|
| 1 | +/* |
| 2 | + * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 | + * |
| 4 | + * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 | + * a Linking Exception. For full terms see the included COPYING file. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "clar_libgit2_alloc.h" |
| 9 | + |
| 10 | +static size_t bytes_available; |
| 11 | + |
| 12 | +/* |
| 13 | + * The clar allocator uses a tagging mechanism for pointers that |
| 14 | + * prepends the actual pointer's number bytes as `size_t`. |
| 15 | + * |
| 16 | + * First, this is required in order to be able to implement |
| 17 | + * proper bookkeeping of allocated bytes in both `free` and |
| 18 | + * `realloc`. |
| 19 | + * |
| 20 | + * Second, it may also be able to spot bugs that are |
| 21 | + * otherwise hard to grasp, as the returned pointer cannot be |
| 22 | + * free'd directly via free(3P). Instead, one is forced to use |
| 23 | + * the tandem of `cl__malloc` and `cl__free`, as otherwise the |
| 24 | + * code is going to crash hard. This is considered to be a |
| 25 | + * feature, as it helps e.g. in finding cases where by accident |
| 26 | + * malloc(3P) and free(3P) were used instead of git__malloc and |
| 27 | + * git__free, respectively. |
| 28 | + * |
| 29 | + * The downside is obviously that each allocation grows by |
| 30 | + * sizeof(size_t) bytes. As the allocator is for testing purposes |
| 31 | + * only, this tradeoff is considered to be perfectly fine, |
| 32 | + * though. |
| 33 | + */ |
| 34 | + |
| 35 | +static void *cl__malloc(size_t len, const char *file, int line) |
| 36 | +{ |
| 37 | + char *ptr = NULL; |
| 38 | + size_t alloclen; |
| 39 | + |
| 40 | + GIT_UNUSED(file); |
| 41 | + GIT_UNUSED(line); |
| 42 | + |
| 43 | + if (len > bytes_available) |
| 44 | + goto out; |
| 45 | + |
| 46 | + if (GIT_ADD_SIZET_OVERFLOW(&alloclen, len, sizeof(size_t)) || |
| 47 | + (ptr = malloc(alloclen)) == NULL) |
| 48 | + goto out; |
| 49 | + memcpy(ptr, &len, sizeof(size_t)); |
| 50 | + |
| 51 | + bytes_available -= len; |
| 52 | + |
| 53 | +out: |
| 54 | + return ptr ? ptr + sizeof(size_t) : NULL; |
| 55 | +} |
| 56 | + |
| 57 | +static void cl__free(void *ptr) |
| 58 | +{ |
| 59 | + if (ptr) { |
| 60 | + char *p = ptr; |
| 61 | + size_t len; |
| 62 | + memcpy(&len, p - sizeof(size_t), sizeof(size_t)); |
| 63 | + free(p - sizeof(size_t)); |
| 64 | + bytes_available += len; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +static void *cl__realloc(void *ptr, size_t size, const char *file, int line) |
| 69 | +{ |
| 70 | + size_t copybytes = 0; |
| 71 | + char *p = ptr; |
| 72 | + void *new; |
| 73 | + |
| 74 | + if (p) |
| 75 | + memcpy(©bytes, p - sizeof(size_t), sizeof(size_t)); |
| 76 | + if (copybytes > size) |
| 77 | + copybytes = size; |
| 78 | + |
| 79 | + if ((new = cl__malloc(size, file, line)) == NULL) |
| 80 | + goto out; |
| 81 | + memcpy(new, p, copybytes); |
| 82 | + cl__free(p); |
| 83 | + |
| 84 | +out: |
| 85 | + return new; |
| 86 | +} |
| 87 | + |
| 88 | +void cl_alloc_limit(size_t bytes) |
| 89 | +{ |
| 90 | + git_allocator alloc; |
| 91 | + |
| 92 | + alloc.gmalloc = cl__malloc; |
| 93 | + alloc.grealloc = cl__realloc; |
| 94 | + alloc.gfree = cl__free; |
| 95 | + |
| 96 | + git_allocator_setup(&alloc); |
| 97 | + |
| 98 | + bytes_available = bytes; |
| 99 | +} |
| 100 | + |
| 101 | +void cl_alloc_reset(void) |
| 102 | +{ |
| 103 | + git_allocator stdalloc; |
| 104 | + git_stdalloc_init_allocator(&stdalloc); |
| 105 | + git_allocator_setup(&stdalloc); |
| 106 | +} |
0 commit comments