Wedson Almeida Filho | 11a9b0b | 2018-11-30 18:21:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google LLC |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "hf/mpool.h" |
| 18 | |
| 19 | #include <stdbool.h> |
| 20 | |
| 21 | struct mpool_chunk { |
| 22 | struct mpool_chunk *next_chunk; |
| 23 | struct mpool_chunk *limit; |
| 24 | }; |
| 25 | |
| 26 | struct mpool_entry { |
| 27 | struct mpool_entry *next; |
| 28 | }; |
| 29 | |
| 30 | static bool mpool_locks_enabled = false; |
| 31 | |
| 32 | /** |
| 33 | * Enables the locks protecting memory pools. Before this function is called, |
| 34 | * the locks are disabled, that is, acquiring/releasing them is a no-op. |
| 35 | */ |
| 36 | void mpool_enable_locks(void) |
| 37 | { |
| 38 | mpool_locks_enabled = true; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Acquires the lock protecting the given memory pool, if locks are enabled. |
| 43 | */ |
| 44 | static void mpool_lock(struct mpool *p) |
| 45 | { |
| 46 | if (mpool_locks_enabled) { |
| 47 | sl_lock(&p->lock); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Releases the lock protecting the given memory pool, if locks are enabled. |
| 53 | */ |
| 54 | static void mpool_unlock(struct mpool *p) |
| 55 | { |
| 56 | if (mpool_locks_enabled) { |
| 57 | sl_unlock(&p->lock); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Initialises the given memory pool with the given entry size, which must be |
| 63 | * at least the size of two pointers. |
| 64 | * |
| 65 | * All entries stored in the memory pool will be aligned to at least the entry |
| 66 | * size. |
| 67 | */ |
| 68 | void mpool_init(struct mpool *p, size_t entry_size) |
| 69 | { |
| 70 | p->entry_size = entry_size; |
| 71 | p->chunk_list = NULL; |
| 72 | p->entry_list = NULL; |
Andrew Scull | 63d1f3f | 2018-12-06 13:29:10 +0000 | [diff] [blame^] | 73 | p->fallback = NULL; |
Wedson Almeida Filho | 11a9b0b | 2018-11-30 18:21:51 +0000 | [diff] [blame] | 74 | sl_init(&p->lock); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Initialises the given memory pool by replicating the properties of `from`. It |
| 79 | * also pulls the chunk and free lists from `from`, consuming all its resources |
| 80 | * and making them available via the new memory pool. |
| 81 | */ |
| 82 | void mpool_init_from(struct mpool *p, struct mpool *from) |
| 83 | { |
| 84 | mpool_init(p, from->entry_size); |
| 85 | |
| 86 | mpool_lock(from); |
| 87 | p->chunk_list = from->chunk_list; |
| 88 | p->entry_list = from->entry_list; |
Andrew Scull | 63d1f3f | 2018-12-06 13:29:10 +0000 | [diff] [blame^] | 89 | p->fallback = from->fallback; |
Wedson Almeida Filho | 11a9b0b | 2018-11-30 18:21:51 +0000 | [diff] [blame] | 90 | |
| 91 | from->chunk_list = NULL; |
| 92 | from->entry_list = NULL; |
Andrew Scull | 63d1f3f | 2018-12-06 13:29:10 +0000 | [diff] [blame^] | 93 | from->fallback = NULL; |
Wedson Almeida Filho | 11a9b0b | 2018-11-30 18:21:51 +0000 | [diff] [blame] | 94 | mpool_unlock(from); |
| 95 | } |
| 96 | |
| 97 | /** |
Andrew Scull | 63d1f3f | 2018-12-06 13:29:10 +0000 | [diff] [blame^] | 98 | * Initialises the given memory pool with a fallback memory pool if this pool |
| 99 | * runs out of memory. |
| 100 | */ |
| 101 | void mpool_init_with_fallback(struct mpool *p, struct mpool *fallback) |
| 102 | { |
| 103 | mpool_init(p, fallback->entry_size); |
| 104 | p->fallback = fallback; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Finishes the given memory pool, giving all free memory to the fallback pool |
| 109 | * if there is one. |
| 110 | */ |
| 111 | void mpool_fini(struct mpool *p) |
| 112 | { |
| 113 | struct mpool_entry *entry; |
| 114 | struct mpool_chunk *chunk; |
| 115 | |
| 116 | if (!p->fallback) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | mpool_lock(p); |
| 121 | |
| 122 | /* Merge the freelist into the fallback. */ |
| 123 | entry = p->entry_list; |
| 124 | while (entry != NULL) { |
| 125 | void *ptr = entry; |
| 126 | entry = entry->next; |
| 127 | mpool_free(p->fallback, ptr); |
| 128 | } |
| 129 | |
| 130 | /* Merge the chunk list into the fallback. */ |
| 131 | chunk = p->chunk_list; |
| 132 | while (chunk != NULL) { |
| 133 | void *ptr = chunk; |
| 134 | size_t size = (uintptr_t)chunk->limit - (uintptr_t)chunk; |
| 135 | chunk = chunk->next_chunk; |
| 136 | mpool_add_chunk(p->fallback, ptr, size); |
| 137 | } |
| 138 | |
| 139 | p->chunk_list = NULL; |
| 140 | p->entry_list = NULL; |
| 141 | p->fallback = NULL; |
| 142 | |
| 143 | mpool_unlock(p); |
| 144 | } |
| 145 | |
| 146 | /** |
Wedson Almeida Filho | 11a9b0b | 2018-11-30 18:21:51 +0000 | [diff] [blame] | 147 | * Adds a contiguous chunk of memory to the given memory pool. The chunk will |
| 148 | * eventually be broken up into entries of the size held by the memory pool. |
| 149 | * |
| 150 | * Only the portions aligned to the entry size will be added to the pool. |
| 151 | * |
| 152 | * Returns true if at least a portion of the chunk was added to pool, or false |
| 153 | * if none of the buffer was usable in the pool. |
| 154 | */ |
| 155 | bool mpool_add_chunk(struct mpool *p, void *begin, size_t size) |
| 156 | { |
| 157 | struct mpool_chunk *chunk; |
| 158 | uintptr_t new_begin; |
| 159 | uintptr_t new_end; |
| 160 | |
| 161 | /* Round begin address up, and end address down. */ |
| 162 | new_begin = ((uintptr_t)begin + p->entry_size - 1) / p->entry_size * |
| 163 | p->entry_size; |
| 164 | new_end = ((uintptr_t)begin + size) / p->entry_size * p->entry_size; |
| 165 | |
| 166 | /* Nothing to do if there isn't enough room for an entry. */ |
| 167 | if (new_begin >= new_end || new_end - new_begin < p->entry_size) { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | chunk = (struct mpool_chunk *)new_begin; |
| 172 | chunk->limit = (struct mpool_chunk *)new_end; |
| 173 | |
| 174 | mpool_lock(p); |
| 175 | chunk->next_chunk = p->chunk_list; |
| 176 | p->chunk_list = chunk; |
| 177 | mpool_unlock(p); |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | /** |
Andrew Scull | 63d1f3f | 2018-12-06 13:29:10 +0000 | [diff] [blame^] | 183 | * Allocates and entry from the given memory pool, if one is available. The |
| 184 | * fallback will not be used even if there is one. |
Wedson Almeida Filho | 11a9b0b | 2018-11-30 18:21:51 +0000 | [diff] [blame] | 185 | */ |
Andrew Scull | 63d1f3f | 2018-12-06 13:29:10 +0000 | [diff] [blame^] | 186 | static void *mpool_alloc_no_fallback(struct mpool *p) |
Wedson Almeida Filho | 11a9b0b | 2018-11-30 18:21:51 +0000 | [diff] [blame] | 187 | { |
| 188 | void *ret; |
| 189 | struct mpool_chunk *chunk; |
| 190 | struct mpool_chunk *new_chunk; |
| 191 | |
| 192 | /* Fetch an entry from the free list if one is available. */ |
| 193 | mpool_lock(p); |
| 194 | if (p->entry_list != NULL) { |
| 195 | struct mpool_entry *entry = p->entry_list; |
| 196 | p->entry_list = entry->next; |
| 197 | ret = entry; |
| 198 | goto exit; |
| 199 | } |
| 200 | |
| 201 | chunk = p->chunk_list; |
| 202 | if (chunk == NULL) { |
| 203 | /* The chunk list is also empty, we're out of entries. */ |
| 204 | ret = NULL; |
| 205 | goto exit; |
| 206 | } |
| 207 | |
| 208 | new_chunk = (struct mpool_chunk *)((uintptr_t)chunk + p->entry_size); |
| 209 | if (new_chunk >= chunk->limit) { |
| 210 | p->chunk_list = chunk->next_chunk; |
| 211 | } else { |
| 212 | *new_chunk = *chunk; |
| 213 | p->chunk_list = new_chunk; |
| 214 | } |
| 215 | |
| 216 | ret = chunk; |
| 217 | |
| 218 | exit: |
| 219 | mpool_unlock(p); |
Andrew Scull | 63d1f3f | 2018-12-06 13:29:10 +0000 | [diff] [blame^] | 220 | |
Wedson Almeida Filho | 11a9b0b | 2018-11-30 18:21:51 +0000 | [diff] [blame] | 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | /** |
Andrew Scull | 63d1f3f | 2018-12-06 13:29:10 +0000 | [diff] [blame^] | 225 | * Allocates an entry from the given memory pool, if one is available. If there |
| 226 | * isn't one available, try and allocate from the fallback if there is one. |
| 227 | */ |
| 228 | void *mpool_alloc(struct mpool *p) |
| 229 | { |
| 230 | do { |
| 231 | void *ret = mpool_alloc_no_fallback(p); |
| 232 | |
| 233 | if (ret != NULL) { |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | p = p->fallback; |
| 238 | } while (p != NULL); |
| 239 | |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | /** |
Wedson Almeida Filho | 11a9b0b | 2018-11-30 18:21:51 +0000 | [diff] [blame] | 244 | * Frees an entry back into the memory pool, making it available for reuse. |
| 245 | * |
| 246 | * This is meant to be used for freeing single entries. To free multiple |
| 247 | * entries, one must call mpool_add_chunk instead. |
| 248 | */ |
| 249 | void mpool_free(struct mpool *p, void *ptr) |
| 250 | { |
| 251 | struct mpool_entry *e = ptr; |
| 252 | |
| 253 | /* Store the newly freed entry in the front of the free list. */ |
| 254 | mpool_lock(p); |
| 255 | e->next = p->entry_list; |
| 256 | p->entry_list = e; |
| 257 | mpool_unlock(p); |
| 258 | } |
| 259 | |
| 260 | /** |
Andrew Scull | 63d1f3f | 2018-12-06 13:29:10 +0000 | [diff] [blame^] | 261 | * Allocates a number of contiguous and aligned entries. If a suitable |
| 262 | * allocation could not be found, the fallback will not be used even if there is |
| 263 | * one. |
Wedson Almeida Filho | 11a9b0b | 2018-11-30 18:21:51 +0000 | [diff] [blame] | 264 | */ |
Andrew Scull | 63d1f3f | 2018-12-06 13:29:10 +0000 | [diff] [blame^] | 265 | void *mpool_alloc_contiguous_no_fallback(struct mpool *p, size_t count, |
| 266 | size_t align) |
Wedson Almeida Filho | 11a9b0b | 2018-11-30 18:21:51 +0000 | [diff] [blame] | 267 | { |
| 268 | struct mpool_chunk **prev; |
| 269 | void *ret = NULL; |
| 270 | |
| 271 | align *= p->entry_size; |
| 272 | |
| 273 | mpool_lock(p); |
| 274 | |
| 275 | /* |
| 276 | * Go through the chunk list in search of one with enough room for the |
| 277 | * requested allocation |
| 278 | */ |
| 279 | prev = &p->chunk_list; |
| 280 | while (*prev != NULL) { |
| 281 | uintptr_t start; |
| 282 | struct mpool_chunk *new_chunk; |
| 283 | struct mpool_chunk *chunk = *prev; |
| 284 | |
| 285 | /* Round start address up to the required alignment. */ |
Andrew Scull | 63d1f3f | 2018-12-06 13:29:10 +0000 | [diff] [blame^] | 286 | start = (((uintptr_t)chunk + align - 1) / align) * align; |
Wedson Almeida Filho | 11a9b0b | 2018-11-30 18:21:51 +0000 | [diff] [blame] | 287 | |
| 288 | /* |
| 289 | * Calculate where the new chunk would be if we consume the |
| 290 | * requested number of entries. Then check if this chunk is big |
| 291 | * enough to satisfy the request. |
| 292 | */ |
| 293 | new_chunk = |
Andrew Scull | 63d1f3f | 2018-12-06 13:29:10 +0000 | [diff] [blame^] | 294 | (struct mpool_chunk *)(start + (count * p->entry_size)); |
Wedson Almeida Filho | 11a9b0b | 2018-11-30 18:21:51 +0000 | [diff] [blame] | 295 | if (new_chunk <= chunk->limit) { |
| 296 | /* Remove the consumed area. */ |
| 297 | if (new_chunk == chunk->limit) { |
| 298 | *prev = chunk->next_chunk; |
| 299 | } else { |
| 300 | *new_chunk = *chunk; |
| 301 | *prev = new_chunk; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Add back the space consumed by the alignment |
| 306 | * requirement, if it's big enough to fit an entry. |
| 307 | */ |
| 308 | if (start - (uintptr_t)chunk >= p->entry_size) { |
| 309 | chunk->next_chunk = *prev; |
| 310 | *prev = chunk; |
| 311 | chunk->limit = (struct mpool_chunk *)start; |
| 312 | } |
| 313 | |
| 314 | ret = (void *)start; |
| 315 | break; |
| 316 | } |
| 317 | |
| 318 | prev = &chunk->next_chunk; |
| 319 | } |
| 320 | |
| 321 | mpool_unlock(p); |
| 322 | |
| 323 | return ret; |
| 324 | } |
Andrew Scull | 63d1f3f | 2018-12-06 13:29:10 +0000 | [diff] [blame^] | 325 | |
| 326 | /** |
| 327 | * Allocates a number of contiguous and aligned entries. This is a best-effort |
| 328 | * operation and only succeeds if such entries can be found in the chunks list |
| 329 | * or the chunks of the fallbacks (i.e., the entry list is never used to satisfy |
| 330 | * these allocations). |
| 331 | * |
| 332 | * The alignment is specified as the number of entries, that is, if `align` is |
| 333 | * 4, the alignment in bytes will be 4 * entry_size. |
| 334 | * |
| 335 | * The caller can enventually free the returned entries by calling |
| 336 | * mpool_add_chunk. |
| 337 | */ |
| 338 | void *mpool_alloc_contiguous(struct mpool *p, size_t count, size_t align) |
| 339 | { |
| 340 | do { |
| 341 | void *ret = mpool_alloc_contiguous_no_fallback(p, count, align); |
| 342 | |
| 343 | if (ret != NULL) { |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | p = p->fallback; |
| 348 | } while (p != NULL); |
| 349 | |
| 350 | return NULL; |
| 351 | } |