| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | ||
| 3 | * | ||
| 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * | ||
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 26 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "cx/mempool.h" | ||
| 30 | |||
| 31 | #include <string.h> | ||
| 32 | #include <errno.h> | ||
| 33 | |||
| 34 | struct cx_mempool_memory_s { | ||
| 35 | /** The destructor. */ | ||
| 36 | cx_destructor_func destructor; | ||
| 37 | /** The actual memory. */ | ||
| 38 | char c[]; | ||
| 39 | }; | ||
| 40 | |||
| 41 | 18 | static void *cx_mempool_malloc( | |
| 42 | void *p, | ||
| 43 | size_t n | ||
| 44 | ) { | ||
| 45 | 18 | struct cx_mempool_s *pool = p; | |
| 46 | |||
| 47 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
|
18 | if (pool->size >= pool->capacity) { |
| 48 | 2 | size_t newcap = pool->capacity - (pool->capacity % 16) + 16; | |
| 49 | size_t newmsize; | ||
| 50 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
|
2 | if (pool->capacity > newcap || cx_szmul(newcap, |
| 51 | sizeof(struct cx_mempool_memory_s*), &newmsize)) { | ||
| 52 | ✗ | errno = EOVERFLOW; | |
| 53 | ✗ | return NULL; | |
| 54 | } | ||
| 55 | 2 | struct cx_mempool_memory_s **newdata = realloc(pool->data, newmsize); | |
| 56 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (newdata == NULL) return NULL; |
| 57 | 2 | pool->data = newdata; | |
| 58 | 2 | pool->capacity = newcap; | |
| 59 | } | ||
| 60 | |||
| 61 | 18 | struct cx_mempool_memory_s *mem = malloc(sizeof(cx_destructor_func) + n); | |
| 62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (mem == NULL) return NULL; |
| 63 | |||
| 64 | 18 | mem->destructor = pool->auto_destr; | |
| 65 | 18 | pool->data[pool->size] = mem; | |
| 66 | 18 | pool->size++; | |
| 67 | |||
| 68 | 18 | return mem->c; | |
| 69 | } | ||
| 70 | |||
| 71 | 1 | static void *cx_mempool_calloc( | |
| 72 | void *p, | ||
| 73 | size_t nelem, | ||
| 74 | size_t elsize | ||
| 75 | ) { | ||
| 76 | size_t msz; | ||
| 77 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (cx_szmul(nelem, elsize, &msz)) { |
| 78 | ✗ | errno = EOVERFLOW; | |
| 79 | ✗ | return NULL; | |
| 80 | } | ||
| 81 | 1 | void *ptr = cx_mempool_malloc(p, msz); | |
| 82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ptr == NULL) return NULL; |
| 83 | 1 | memset(ptr, 0, nelem * elsize); | |
| 84 | 1 | return ptr; | |
| 85 | } | ||
| 86 | |||
| 87 | 2 | static void *cx_mempool_realloc( | |
| 88 | void *p, | ||
| 89 | void *ptr, | ||
| 90 | size_t n | ||
| 91 | ) { | ||
| 92 | 2 | struct cx_mempool_s *pool = p; | |
| 93 | |||
| 94 | struct cx_mempool_memory_s *mem, *newm; | ||
| 95 | 2 | mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func)); | |
| 96 | 2 | newm = realloc(mem, n + sizeof(cx_destructor_func)); | |
| 97 | |||
| 98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (newm == NULL) return NULL; |
| 99 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (mem != newm) { |
| 100 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | for (size_t i = 0; i < pool->size; i++) { |
| 101 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (pool->data[i] == mem) { |
| 102 | 1 | pool->data[i] = newm; | |
| 103 | 1 | return ((char*)newm) + sizeof(cx_destructor_func); | |
| 104 | } | ||
| 105 | } | ||
| 106 | − | abort(); // LCOV_EXCL_LINE | |
| 107 | } else { | ||
| 108 | 1 | return ptr; | |
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | 5 | static void cx_mempool_free( | |
| 113 | void *p, | ||
| 114 | void *ptr | ||
| 115 | ) { | ||
| 116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!ptr) return; |
| 117 | 5 | struct cx_mempool_s *pool = p; | |
| 118 | |||
| 119 | 5 | struct cx_mempool_memory_s *mem = (struct cx_mempool_memory_s *) | |
| 120 | ((char *) ptr - sizeof(cx_destructor_func)); | ||
| 121 | |||
| 122 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | for (size_t i = 0; i < pool->size; i++) { |
| 123 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
|
9 | if (mem == pool->data[i]) { |
| 124 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
|
5 | if (mem->destructor) { |
| 125 | 2 | mem->destructor(mem->c); | |
| 126 | } | ||
| 127 | 5 | free(mem); | |
| 128 | 5 | size_t last_index = pool->size - 1; | |
| 129 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
|
5 | if (i != last_index) { |
| 130 | 2 | pool->data[i] = pool->data[last_index]; | |
| 131 | 2 | pool->data[last_index] = NULL; | |
| 132 | } | ||
| 133 | 5 | pool->size--; | |
| 134 | 5 | return; | |
| 135 | } | ||
| 136 | } | ||
| 137 | − | abort(); // LCOV_EXCL_LINE | |
| 138 | } | ||
| 139 | |||
| 140 | 7 | void cxMempoolFree(CxMempool *pool) { | |
| 141 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (pool == NULL) return; |
| 142 | struct cx_mempool_memory_s *mem; | ||
| 143 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7 times.
|
20 | for (size_t i = 0; i < pool->size; i++) { |
| 144 | 13 | mem = pool->data[i]; | |
| 145 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10 times.
|
13 | if (mem->destructor) { |
| 146 | 3 | mem->destructor(mem->c); | |
| 147 | } | ||
| 148 | 13 | free(mem); | |
| 149 | } | ||
| 150 | 7 | free(pool->data); | |
| 151 | 7 | free((void*) pool->allocator); | |
| 152 | 7 | free(pool); | |
| 153 | } | ||
| 154 | |||
| 155 | 3 | void cxMempoolSetDestructor( | |
| 156 | void *ptr, | ||
| 157 | cx_destructor_func func | ||
| 158 | ) { | ||
| 159 | 3 | *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func; | |
| 160 | 3 | } | |
| 161 | |||
| 162 | ✗ | void cxMempoolRemoveDestructor(void *ptr) { | |
| 163 | ✗ | *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = NULL; | |
| 164 | ✗ | } | |
| 165 | |||
| 166 | struct cx_mempool_foreign_mem_s { | ||
| 167 | cx_destructor_func destr; | ||
| 168 | void* mem; | ||
| 169 | }; | ||
| 170 | |||
| 171 | 1 | static void cx_mempool_destr_foreign_mem(void* ptr) { | |
| 172 | 1 | struct cx_mempool_foreign_mem_s *fm = ptr; | |
| 173 | 1 | fm->destr(fm->mem); | |
| 174 | 1 | } | |
| 175 | |||
| 176 | 1 | int cxMempoolRegister( | |
| 177 | CxMempool *pool, | ||
| 178 | void *memory, | ||
| 179 | cx_destructor_func destr | ||
| 180 | ) { | ||
| 181 | 1 | struct cx_mempool_foreign_mem_s *fm = cx_mempool_malloc( | |
| 182 | pool, | ||
| 183 | sizeof(struct cx_mempool_foreign_mem_s) | ||
| 184 | ); | ||
| 185 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (fm == NULL) return 1; |
| 186 | |||
| 187 | 1 | fm->mem = memory; | |
| 188 | 1 | fm->destr = destr; | |
| 189 | 1 | *(cx_destructor_func *) ((char *) fm - sizeof(cx_destructor_func)) = cx_mempool_destr_foreign_mem; | |
| 190 | |||
| 191 | 1 | return 0; | |
| 192 | } | ||
| 193 | |||
| 194 | static cx_allocator_class cx_mempool_allocator_class = { | ||
| 195 | cx_mempool_malloc, | ||
| 196 | cx_mempool_realloc, | ||
| 197 | cx_mempool_calloc, | ||
| 198 | cx_mempool_free | ||
| 199 | }; | ||
| 200 | |||
| 201 | 7 | CxMempool *cxMempoolCreate( | |
| 202 | size_t capacity, | ||
| 203 | cx_destructor_func destr | ||
| 204 | ) { | ||
| 205 | size_t poolsize; | ||
| 206 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | if (cx_szmul(capacity, sizeof(struct cx_mempool_memory_s*), &poolsize)) { |
| 207 | ✗ | errno = EOVERFLOW; | |
| 208 | ✗ | return NULL; | |
| 209 | } | ||
| 210 | |||
| 211 | struct cx_mempool_s *pool = | ||
| 212 | 7 | malloc(sizeof(struct cx_mempool_s)); | |
| 213 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (pool == NULL) return NULL; |
| 214 | |||
| 215 | 7 | CxAllocator *provided_allocator = malloc(sizeof(CxAllocator)); | |
| 216 | − | if (provided_allocator == NULL) { // LCOV_EXCL_START | |
| 217 | − | free(pool); | |
| 218 | − | return NULL; | |
| 219 | } // LCOV_EXCL_STOP | ||
| 220 | 7 | provided_allocator->cl = &cx_mempool_allocator_class; | |
| 221 | 7 | provided_allocator->data = pool; | |
| 222 | |||
| 223 | 7 | pool->allocator = provided_allocator; | |
| 224 | |||
| 225 | 7 | pool->data = malloc(poolsize); | |
| 226 | − | if (pool->data == NULL) { // LCOV_EXCL_START | |
| 227 | − | free(provided_allocator); | |
| 228 | − | free(pool); | |
| 229 | − | return NULL; | |
| 230 | } // LCOV_EXCL_STOP | ||
| 231 | |||
| 232 | 7 | pool->size = 0; | |
| 233 | 7 | pool->capacity = capacity; | |
| 234 | 7 | pool->auto_destr = destr; | |
| 235 | |||
| 236 | 7 | return pool; | |
| 237 | } | ||
| 238 |