[6db6fd1] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Martin Decky
|
---|
| 3 | * Copyright (c) 2009 Petr Tuma
|
---|
| 4 | * 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
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup libc
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <malloc.h>
|
---|
[3e6a98c5] | 37 | #include <stdbool.h>
|
---|
[6db6fd1] | 38 | #include <as.h>
|
---|
| 39 | #include <align.h>
|
---|
| 40 | #include <macros.h>
|
---|
| 41 | #include <assert.h>
|
---|
| 42 | #include <errno.h>
|
---|
| 43 | #include <bitops.h>
|
---|
| 44 | #include <mem.h>
|
---|
[3292623] | 45 | #include <futex.h>
|
---|
[d161715] | 46 | #include <stdlib.h>
|
---|
[6db6fd1] | 47 | #include <adt/gcdlcm.h>
|
---|
[47b7006] | 48 | #include "private/malloc.h"
|
---|
[6db6fd1] | 49 |
|
---|
[0b37882] | 50 | /** Magic used in heap headers. */
|
---|
| 51 | #define HEAP_BLOCK_HEAD_MAGIC UINT32_C(0xBEEF0101)
|
---|
[6db6fd1] | 52 |
|
---|
[0b37882] | 53 | /** Magic used in heap footers. */
|
---|
| 54 | #define HEAP_BLOCK_FOOT_MAGIC UINT32_C(0xBEEF0202)
|
---|
[6db6fd1] | 55 |
|
---|
[0b37882] | 56 | /** Magic used in heap descriptor. */
|
---|
| 57 | #define HEAP_AREA_MAGIC UINT32_C(0xBEEFCAFE)
|
---|
| 58 |
|
---|
| 59 | /** Allocation alignment.
|
---|
| 60 | *
|
---|
| 61 | * This also covers the alignment of fields
|
---|
| 62 | * in the heap header and footer.
|
---|
| 63 | *
|
---|
| 64 | */
|
---|
[6db6fd1] | 65 | #define BASE_ALIGN 16
|
---|
| 66 |
|
---|
[013a5d7] | 67 | /** Heap shrink granularity
|
---|
| 68 | *
|
---|
[21799398] | 69 | * Try not to pump and stress the heap too much
|
---|
[013a5d7] | 70 | * by shrinking and enlarging it too often.
|
---|
[21799398] | 71 | * A heap area won't shrink if the released
|
---|
[013a5d7] | 72 | * free block is smaller than this constant.
|
---|
| 73 | *
|
---|
| 74 | */
|
---|
| 75 | #define SHRINK_GRANULARITY (64 * PAGE_SIZE)
|
---|
| 76 |
|
---|
[0b37882] | 77 | /** Overhead of each heap block. */
|
---|
| 78 | #define STRUCT_OVERHEAD \
|
---|
| 79 | (sizeof(heap_block_head_t) + sizeof(heap_block_foot_t))
|
---|
| 80 |
|
---|
[207533f] | 81 | /** Overhead of each area. */
|
---|
| 82 | #define AREA_OVERHEAD(size) \
|
---|
| 83 | (ALIGN_UP(size + sizeof(heap_area_t), BASE_ALIGN))
|
---|
| 84 |
|
---|
[0b37882] | 85 | /** Calculate real size of a heap block.
|
---|
| 86 | *
|
---|
| 87 | * Add header and footer size.
|
---|
| 88 | *
|
---|
[6db6fd1] | 89 | */
|
---|
[0b37882] | 90 | #define GROSS_SIZE(size) ((size) + STRUCT_OVERHEAD)
|
---|
[6db6fd1] | 91 |
|
---|
[0b37882] | 92 | /** Calculate net size of a heap block.
|
---|
| 93 | *
|
---|
| 94 | * Subtract header and footer size.
|
---|
[6db6fd1] | 95 | *
|
---|
| 96 | */
|
---|
[0b37882] | 97 | #define NET_SIZE(size) ((size) - STRUCT_OVERHEAD)
|
---|
[6db6fd1] | 98 |
|
---|
[0b37882] | 99 | /** Get first block in heap area.
|
---|
| 100 | *
|
---|
[6db6fd1] | 101 | */
|
---|
[013a5d7] | 102 | #define AREA_FIRST_BLOCK_HEAD(area) \
|
---|
[0b37882] | 103 | (ALIGN_UP(((uintptr_t) (area)) + sizeof(heap_area_t), BASE_ALIGN))
|
---|
[6db6fd1] | 104 |
|
---|
[013a5d7] | 105 | /** Get last block in heap area.
|
---|
| 106 | *
|
---|
| 107 | */
|
---|
| 108 | #define AREA_LAST_BLOCK_FOOT(area) \
|
---|
| 109 | (((uintptr_t) (area)->end) - sizeof(heap_block_foot_t))
|
---|
| 110 |
|
---|
[7aafdb86] | 111 | #define AREA_LAST_BLOCK_HEAD(area) \
|
---|
[da287d1] | 112 | ((uintptr_t) BLOCK_HEAD(((heap_block_foot_t *) AREA_LAST_BLOCK_FOOT(area))))
|
---|
[7aafdb86] | 113 |
|
---|
[013a5d7] | 114 | /** Get header in heap block.
|
---|
| 115 | *
|
---|
| 116 | */
|
---|
| 117 | #define BLOCK_HEAD(foot) \
|
---|
| 118 | ((heap_block_head_t *) \
|
---|
| 119 | (((uintptr_t) (foot)) + sizeof(heap_block_foot_t) - (foot)->size))
|
---|
| 120 |
|
---|
[0b37882] | 121 | /** Get footer in heap block.
|
---|
| 122 | *
|
---|
[6db6fd1] | 123 | */
|
---|
[0b37882] | 124 | #define BLOCK_FOOT(head) \
|
---|
| 125 | ((heap_block_foot_t *) \
|
---|
[013a5d7] | 126 | (((uintptr_t) (head)) + (head)->size - sizeof(heap_block_foot_t)))
|
---|
[0b37882] | 127 |
|
---|
| 128 | /** Heap area.
|
---|
| 129 | *
|
---|
| 130 | * The memory managed by the heap allocator is divided into
|
---|
| 131 | * multiple discontinuous heaps. Each heap is represented
|
---|
| 132 | * by a separate address space area which has this structure
|
---|
| 133 | * at its very beginning.
|
---|
| 134 | *
|
---|
| 135 | */
|
---|
| 136 | typedef struct heap_area {
|
---|
| 137 | /** Start of the heap area (including this structure)
|
---|
| 138 | *
|
---|
| 139 | * Aligned on page boundary.
|
---|
| 140 | *
|
---|
| 141 | */
|
---|
| 142 | void *start;
|
---|
| 143 |
|
---|
| 144 | /** End of the heap area (aligned on page boundary) */
|
---|
| 145 | void *end;
|
---|
| 146 |
|
---|
[013a5d7] | 147 | /** Previous heap area */
|
---|
| 148 | struct heap_area *prev;
|
---|
| 149 |
|
---|
[0b37882] | 150 | /** Next heap area */
|
---|
| 151 | struct heap_area *next;
|
---|
| 152 |
|
---|
| 153 | /** A magic value */
|
---|
| 154 | uint32_t magic;
|
---|
| 155 | } heap_area_t;
|
---|
[6db6fd1] | 156 |
|
---|
| 157 | /** Header of a heap block
|
---|
| 158 | *
|
---|
| 159 | */
|
---|
| 160 | typedef struct {
|
---|
| 161 | /* Size of the block (including header and footer) */
|
---|
| 162 | size_t size;
|
---|
| 163 |
|
---|
| 164 | /* Indication of a free block */
|
---|
| 165 | bool free;
|
---|
| 166 |
|
---|
[0b37882] | 167 | /** Heap area this block belongs to */
|
---|
| 168 | heap_area_t *area;
|
---|
| 169 |
|
---|
[6db6fd1] | 170 | /* A magic value to detect overwrite of heap header */
|
---|
| 171 | uint32_t magic;
|
---|
| 172 | } heap_block_head_t;
|
---|
| 173 |
|
---|
| 174 | /** Footer of a heap block
|
---|
| 175 | *
|
---|
| 176 | */
|
---|
| 177 | typedef struct {
|
---|
| 178 | /* Size of the block (including header and footer) */
|
---|
| 179 | size_t size;
|
---|
| 180 |
|
---|
| 181 | /* A magic value to detect overwrite of heap footer */
|
---|
| 182 | uint32_t magic;
|
---|
| 183 | } heap_block_foot_t;
|
---|
| 184 |
|
---|
[0b37882] | 185 | /** First heap area */
|
---|
| 186 | static heap_area_t *first_heap_area = NULL;
|
---|
[6db6fd1] | 187 |
|
---|
[0b37882] | 188 | /** Last heap area */
|
---|
| 189 | static heap_area_t *last_heap_area = NULL;
|
---|
[6db6fd1] | 190 |
|
---|
[0b37882] | 191 | /** Next heap block to examine (next fit algorithm) */
|
---|
[207533f] | 192 | static heap_block_head_t *next_fit = NULL;
|
---|
[6db6fd1] | 193 |
|
---|
[0b37882] | 194 | /** Futex for thread-safe heap manipulation */
|
---|
| 195 | static futex_t malloc_futex = FUTEX_INITIALIZER;
|
---|
[6db6fd1] | 196 |
|
---|
[13f2461] | 197 | #ifndef NDEBUG
|
---|
| 198 |
|
---|
| 199 | #define malloc_assert(expr) \
|
---|
| 200 | do { \
|
---|
| 201 | if (!(expr)) {\
|
---|
[af5dfa5b] | 202 | heap_unlock(); \
|
---|
[e06e2716] | 203 | assert_abort(#expr, __FILE__, __LINE__); \
|
---|
[13f2461] | 204 | } \
|
---|
| 205 | } while (0)
|
---|
| 206 |
|
---|
| 207 | #else /* NDEBUG */
|
---|
| 208 |
|
---|
| 209 | #define malloc_assert(expr)
|
---|
| 210 |
|
---|
| 211 | #endif /* NDEBUG */
|
---|
| 212 |
|
---|
[af5dfa5b] | 213 |
|
---|
| 214 | #ifdef FUTEX_UPGRADABLE
|
---|
| 215 | /** True if the heap may be accessed from multiple threads. */
|
---|
| 216 | static bool multithreaded = false;
|
---|
| 217 |
|
---|
| 218 | /** Makes accesses to the heap thread safe. */
|
---|
| 219 | void malloc_enable_multithreaded(void)
|
---|
| 220 | {
|
---|
| 221 | multithreaded = true;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | /** Serializes access to the heap from multiple threads. */
|
---|
| 225 | static inline void heap_lock(void)
|
---|
| 226 | {
|
---|
| 227 | if (multithreaded) {
|
---|
| 228 | futex_down(&malloc_futex);
|
---|
| 229 | } else {
|
---|
| 230 | /*
|
---|
| 231 | * Malloc never switches fibrils while the heap is locked.
|
---|
| 232 | * Similarly, it never creates new threads from within the
|
---|
| 233 | * locked region. Therefore, if there are no other threads
|
---|
| 234 | * except this one, the whole operation will complete without
|
---|
| 235 | * any interruptions.
|
---|
| 236 | */
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | /** Serializes access to the heap from multiple threads. */
|
---|
| 241 | static inline void heap_unlock(void)
|
---|
| 242 | {
|
---|
| 243 | if (multithreaded) {
|
---|
| 244 | futex_up(&malloc_futex);
|
---|
| 245 | } else {
|
---|
| 246 | /*
|
---|
| 247 | * Malloc never switches fibrils while the heap is locked.
|
---|
| 248 | * Similarly, it never creates new threads from within the
|
---|
| 249 | * locked region. Therefore, if there are no other threads
|
---|
| 250 | * except this one, the whole operation will complete without
|
---|
| 251 | * any interruptions.
|
---|
| 252 | */
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | #else
|
---|
| 257 |
|
---|
| 258 | /** Makes accesses to the heap thread safe. */
|
---|
| 259 | void malloc_enable_multithreaded(void)
|
---|
| 260 | {
|
---|
| 261 | /* No-op. Already using thread-safe heap locking operations. */
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | /** Serializes access to the heap from multiple threads. */
|
---|
| 265 | static inline void heap_lock(void)
|
---|
| 266 | {
|
---|
| 267 | futex_down(&malloc_futex);
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | /** Serializes access to the heap from multiple threads. */
|
---|
| 271 | static inline void heap_unlock(void)
|
---|
| 272 | {
|
---|
| 273 | futex_up(&malloc_futex);
|
---|
| 274 | }
|
---|
| 275 | #endif
|
---|
| 276 |
|
---|
| 277 |
|
---|
[6db6fd1] | 278 | /** Initialize a heap block
|
---|
| 279 | *
|
---|
[0b37882] | 280 | * Fill in the structures related to a heap block.
|
---|
[3292623] | 281 | * Should be called only inside the critical section.
|
---|
[6db6fd1] | 282 | *
|
---|
| 283 | * @param addr Address of the block.
|
---|
| 284 | * @param size Size of the block including the header and the footer.
|
---|
| 285 | * @param free Indication of a free block.
|
---|
[0b37882] | 286 | * @param area Heap area the block belongs to.
|
---|
[6db6fd1] | 287 | *
|
---|
| 288 | */
|
---|
[0b37882] | 289 | static void block_init(void *addr, size_t size, bool free, heap_area_t *area)
|
---|
[6db6fd1] | 290 | {
|
---|
| 291 | /* Calculate the position of the header and the footer */
|
---|
| 292 | heap_block_head_t *head = (heap_block_head_t *) addr;
|
---|
| 293 |
|
---|
| 294 | head->size = size;
|
---|
| 295 | head->free = free;
|
---|
[0b37882] | 296 | head->area = area;
|
---|
[6db6fd1] | 297 | head->magic = HEAP_BLOCK_HEAD_MAGIC;
|
---|
| 298 |
|
---|
[0b37882] | 299 | heap_block_foot_t *foot = BLOCK_FOOT(head);
|
---|
| 300 |
|
---|
[6db6fd1] | 301 | foot->size = size;
|
---|
| 302 | foot->magic = HEAP_BLOCK_FOOT_MAGIC;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | /** Check a heap block
|
---|
| 306 | *
|
---|
| 307 | * Verifies that the structures related to a heap block still contain
|
---|
| 308 | * the magic constants. This helps detect heap corruption early on.
|
---|
[3292623] | 309 | * Should be called only inside the critical section.
|
---|
[6db6fd1] | 310 | *
|
---|
| 311 | * @param addr Address of the block.
|
---|
| 312 | *
|
---|
| 313 | */
|
---|
| 314 | static void block_check(void *addr)
|
---|
| 315 | {
|
---|
| 316 | heap_block_head_t *head = (heap_block_head_t *) addr;
|
---|
| 317 |
|
---|
[13f2461] | 318 | malloc_assert(head->magic == HEAP_BLOCK_HEAD_MAGIC);
|
---|
[6db6fd1] | 319 |
|
---|
[0b37882] | 320 | heap_block_foot_t *foot = BLOCK_FOOT(head);
|
---|
[6db6fd1] | 321 |
|
---|
[13f2461] | 322 | malloc_assert(foot->magic == HEAP_BLOCK_FOOT_MAGIC);
|
---|
| 323 | malloc_assert(head->size == foot->size);
|
---|
[6db6fd1] | 324 | }
|
---|
| 325 |
|
---|
[0b37882] | 326 | /** Check a heap area structure
|
---|
[013a5d7] | 327 | *
|
---|
| 328 | * Should be called only inside the critical section.
|
---|
[3292623] | 329 | *
|
---|
[0b37882] | 330 | * @param addr Address of the heap area.
|
---|
[3292623] | 331 | *
|
---|
[0b37882] | 332 | */
|
---|
| 333 | static void area_check(void *addr)
|
---|
| 334 | {
|
---|
| 335 | heap_area_t *area = (heap_area_t *) addr;
|
---|
| 336 |
|
---|
[13f2461] | 337 | malloc_assert(area->magic == HEAP_AREA_MAGIC);
|
---|
| 338 | malloc_assert(addr == area->start);
|
---|
| 339 | malloc_assert(area->start < area->end);
|
---|
| 340 | malloc_assert(((uintptr_t) area->start % PAGE_SIZE) == 0);
|
---|
| 341 | malloc_assert(((uintptr_t) area->end % PAGE_SIZE) == 0);
|
---|
[0b37882] | 342 | }
|
---|
| 343 |
|
---|
| 344 | /** Create new heap area
|
---|
| 345 | *
|
---|
[013a5d7] | 346 | * Should be called only inside the critical section.
|
---|
| 347 | *
|
---|
| 348 | * @param size Size of the area.
|
---|
[3292623] | 349 | *
|
---|
| 350 | */
|
---|
[0b37882] | 351 | static bool area_create(size_t size)
|
---|
[6db6fd1] | 352 | {
|
---|
[fbcdeb8] | 353 | /* Align the heap area size on page boundary */
|
---|
[0b37882] | 354 | size_t asize = ALIGN_UP(size, PAGE_SIZE);
|
---|
[faba839] | 355 | void *astart = as_area_create(AS_AREA_ANY, asize,
|
---|
[9727b92] | 356 | AS_AREA_WRITE | AS_AREA_READ | AS_AREA_CACHEABLE);
|
---|
[faba839] | 357 | if (astart == AS_MAP_FAILED)
|
---|
[e70edd1] | 358 | return false;
|
---|
[6db6fd1] | 359 |
|
---|
[0b37882] | 360 | heap_area_t *area = (heap_area_t *) astart;
|
---|
| 361 |
|
---|
| 362 | area->start = astart;
|
---|
[013a5d7] | 363 | area->end = (void *) ((uintptr_t) astart + asize);
|
---|
| 364 | area->prev = NULL;
|
---|
[0b37882] | 365 | area->next = NULL;
|
---|
| 366 | area->magic = HEAP_AREA_MAGIC;
|
---|
| 367 |
|
---|
[013a5d7] | 368 | void *block = (void *) AREA_FIRST_BLOCK_HEAD(area);
|
---|
[0b37882] | 369 | size_t bsize = (size_t) (area->end - block);
|
---|
| 370 |
|
---|
| 371 | block_init(block, bsize, true, area);
|
---|
| 372 |
|
---|
| 373 | if (last_heap_area == NULL) {
|
---|
| 374 | first_heap_area = area;
|
---|
| 375 | last_heap_area = area;
|
---|
| 376 | } else {
|
---|
[013a5d7] | 377 | area->prev = last_heap_area;
|
---|
[0b37882] | 378 | last_heap_area->next = area;
|
---|
| 379 | last_heap_area = area;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | return true;
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | /** Try to enlarge a heap area
|
---|
[013a5d7] | 386 | *
|
---|
| 387 | * Should be called only inside the critical section.
|
---|
[0b37882] | 388 | *
|
---|
| 389 | * @param area Heap area to grow.
|
---|
[013a5d7] | 390 | * @param size Gross size to grow (bytes).
|
---|
| 391 | *
|
---|
| 392 | * @return True if successful.
|
---|
[0b37882] | 393 | *
|
---|
| 394 | */
|
---|
| 395 | static bool area_grow(heap_area_t *area, size_t size)
|
---|
| 396 | {
|
---|
| 397 | if (size == 0)
|
---|
| 398 | return true;
|
---|
| 399 |
|
---|
| 400 | area_check(area);
|
---|
| 401 |
|
---|
| 402 | /* New heap area size */
|
---|
[013a5d7] | 403 | size_t gross_size = (size_t) (area->end - area->start) + size;
|
---|
| 404 | size_t asize = ALIGN_UP(gross_size, PAGE_SIZE);
|
---|
| 405 | void *end = (void *) ((uintptr_t) area->start + asize);
|
---|
[0b37882] | 406 |
|
---|
| 407 | /* Check for overflow */
|
---|
| 408 | if (end < area->start)
|
---|
| 409 | return false;
|
---|
[6db6fd1] | 410 |
|
---|
[0b37882] | 411 | /* Resize the address space area */
|
---|
| 412 | int ret = as_area_resize(area->start, asize, 0);
|
---|
| 413 | if (ret != EOK)
|
---|
[6db6fd1] | 414 | return false;
|
---|
| 415 |
|
---|
[da287d1] | 416 | heap_block_head_t *last_head =
|
---|
| 417 | (heap_block_head_t *) AREA_LAST_BLOCK_HEAD(area);
|
---|
[7aafdb86] | 418 |
|
---|
| 419 | if (last_head->free) {
|
---|
| 420 | /* Add the new space to the last block. */
|
---|
| 421 | size_t net_size = (size_t) (end - area->end) + last_head->size;
|
---|
| 422 | malloc_assert(net_size > 0);
|
---|
| 423 | block_init(last_head, net_size, true, area);
|
---|
| 424 | } else {
|
---|
| 425 | /* Add new free block */
|
---|
| 426 | size_t net_size = (size_t) (end - area->end);
|
---|
| 427 | if (net_size > 0)
|
---|
| 428 | block_init(area->end, net_size, true, area);
|
---|
| 429 | }
|
---|
[6db6fd1] | 430 |
|
---|
[0b37882] | 431 | /* Update heap area parameters */
|
---|
| 432 | area->end = end;
|
---|
| 433 |
|
---|
| 434 | return true;
|
---|
| 435 | }
|
---|
| 436 |
|
---|
[013a5d7] | 437 | /** Try to shrink heap
|
---|
[3292623] | 438 | *
|
---|
[013a5d7] | 439 | * Should be called only inside the critical section.
|
---|
[0b37882] | 440 | * In all cases the next pointer is reset.
|
---|
[3292623] | 441 | *
|
---|
[013a5d7] | 442 | * @param area Last modified heap area.
|
---|
| 443 | *
|
---|
[3292623] | 444 | */
|
---|
[013a5d7] | 445 | static void heap_shrink(heap_area_t *area)
|
---|
[6db6fd1] | 446 | {
|
---|
[013a5d7] | 447 | area_check(area);
|
---|
| 448 |
|
---|
| 449 | heap_block_foot_t *last_foot =
|
---|
| 450 | (heap_block_foot_t *) AREA_LAST_BLOCK_FOOT(area);
|
---|
| 451 | heap_block_head_t *last_head = BLOCK_HEAD(last_foot);
|
---|
| 452 |
|
---|
| 453 | block_check((void *) last_head);
|
---|
[13f2461] | 454 | malloc_assert(last_head->area == area);
|
---|
[013a5d7] | 455 |
|
---|
| 456 | if (last_head->free) {
|
---|
| 457 | /*
|
---|
| 458 | * The last block of the heap area is
|
---|
| 459 | * unused. The area might be potentially
|
---|
| 460 | * shrunk.
|
---|
| 461 | */
|
---|
| 462 |
|
---|
| 463 | heap_block_head_t *first_head =
|
---|
| 464 | (heap_block_head_t *) AREA_FIRST_BLOCK_HEAD(area);
|
---|
| 465 |
|
---|
| 466 | block_check((void *) first_head);
|
---|
[13f2461] | 467 | malloc_assert(first_head->area == area);
|
---|
[013a5d7] | 468 |
|
---|
[e6eee2b] | 469 | size_t shrink_size = ALIGN_DOWN(last_head->size, PAGE_SIZE);
|
---|
| 470 |
|
---|
[013a5d7] | 471 | if (first_head == last_head) {
|
---|
| 472 | /*
|
---|
| 473 | * The entire heap area consists of a single
|
---|
| 474 | * free heap block. This means we can get rid
|
---|
| 475 | * of it entirely.
|
---|
| 476 | */
|
---|
| 477 |
|
---|
| 478 | heap_area_t *prev = area->prev;
|
---|
| 479 | heap_area_t *next = area->next;
|
---|
| 480 |
|
---|
| 481 | if (prev != NULL) {
|
---|
| 482 | area_check(prev);
|
---|
| 483 | prev->next = next;
|
---|
| 484 | } else
|
---|
| 485 | first_heap_area = next;
|
---|
| 486 |
|
---|
| 487 | if (next != NULL) {
|
---|
| 488 | area_check(next);
|
---|
| 489 | next->prev = prev;
|
---|
| 490 | } else
|
---|
| 491 | last_heap_area = prev;
|
---|
| 492 |
|
---|
| 493 | as_area_destroy(area->start);
|
---|
[e6eee2b] | 494 | } else if (shrink_size >= SHRINK_GRANULARITY) {
|
---|
[013a5d7] | 495 | /*
|
---|
| 496 | * Make sure that we always shrink the area
|
---|
| 497 | * by a multiple of page size and update
|
---|
| 498 | * the block layout accordingly.
|
---|
| 499 | */
|
---|
| 500 |
|
---|
| 501 | size_t asize = (size_t) (area->end - area->start) - shrink_size;
|
---|
| 502 | void *end = (void *) ((uintptr_t) area->start + asize);
|
---|
| 503 |
|
---|
| 504 | /* Resize the address space area */
|
---|
| 505 | int ret = as_area_resize(area->start, asize, 0);
|
---|
| 506 | if (ret != EOK)
|
---|
| 507 | abort();
|
---|
| 508 |
|
---|
| 509 | /* Update heap area parameters */
|
---|
| 510 | area->end = end;
|
---|
[207533f] | 511 | size_t excess = ((size_t) area->end) - ((size_t) last_head);
|
---|
[013a5d7] | 512 |
|
---|
| 513 | if (excess > 0) {
|
---|
| 514 | if (excess >= STRUCT_OVERHEAD) {
|
---|
| 515 | /*
|
---|
| 516 | * The previous block cannot be free and there
|
---|
| 517 | * is enough free space left in the area to
|
---|
| 518 | * create a new free block.
|
---|
| 519 | */
|
---|
[207533f] | 520 | block_init((void *) last_head, excess, true, area);
|
---|
[013a5d7] | 521 | } else {
|
---|
| 522 | /*
|
---|
| 523 | * The excess is small. Therefore just enlarge
|
---|
| 524 | * the previous block.
|
---|
| 525 | */
|
---|
| 526 | heap_block_foot_t *prev_foot = (heap_block_foot_t *)
|
---|
| 527 | (((uintptr_t) last_head) - sizeof(heap_block_foot_t));
|
---|
| 528 | heap_block_head_t *prev_head = BLOCK_HEAD(prev_foot);
|
---|
| 529 |
|
---|
| 530 | block_check((void *) prev_head);
|
---|
| 531 |
|
---|
| 532 | block_init(prev_head, prev_head->size + excess,
|
---|
| 533 | prev_head->free, area);
|
---|
| 534 | }
|
---|
| 535 | }
|
---|
| 536 | }
|
---|
| 537 | }
|
---|
| 538 |
|
---|
[207533f] | 539 | next_fit = NULL;
|
---|
[6db6fd1] | 540 | }
|
---|
| 541 |
|
---|
| 542 | /** Initialize the heap allocator
|
---|
| 543 | *
|
---|
[47b7006] | 544 | * Create initial heap memory area. This routine is
|
---|
| 545 | * only called from libc initialization, thus we do not
|
---|
| 546 | * take any locks.
|
---|
[6db6fd1] | 547 | *
|
---|
| 548 | */
|
---|
[47b7006] | 549 | void __malloc_init(void)
|
---|
[6db6fd1] | 550 | {
|
---|
[0b37882] | 551 | if (!area_create(PAGE_SIZE))
|
---|
[47b7006] | 552 | abort();
|
---|
[6db6fd1] | 553 | }
|
---|
| 554 |
|
---|
[3292623] | 555 | /** Split heap block and mark it as used.
|
---|
| 556 | *
|
---|
| 557 | * Should be called only inside the critical section.
|
---|
| 558 | *
|
---|
| 559 | * @param cur Heap block to split.
|
---|
| 560 | * @param size Number of bytes to split and mark from the beginning
|
---|
| 561 | * of the block.
|
---|
| 562 | *
|
---|
| 563 | */
|
---|
[6db6fd1] | 564 | static void split_mark(heap_block_head_t *cur, const size_t size)
|
---|
| 565 | {
|
---|
[13f2461] | 566 | malloc_assert(cur->size >= size);
|
---|
[6db6fd1] | 567 |
|
---|
| 568 | /* See if we should split the block. */
|
---|
| 569 | size_t split_limit = GROSS_SIZE(size);
|
---|
| 570 |
|
---|
| 571 | if (cur->size > split_limit) {
|
---|
| 572 | /* Block big enough -> split. */
|
---|
| 573 | void *next = ((void *) cur) + size;
|
---|
[0b37882] | 574 | block_init(next, cur->size - size, true, cur->area);
|
---|
| 575 | block_init(cur, size, false, cur->area);
|
---|
[6db6fd1] | 576 | } else {
|
---|
| 577 | /* Block too small -> use as is. */
|
---|
| 578 | cur->free = false;
|
---|
| 579 | }
|
---|
| 580 | }
|
---|
| 581 |
|
---|
[0b37882] | 582 | /** Allocate memory from heap area starting from given block
|
---|
[3292623] | 583 | *
|
---|
| 584 | * Should be called only inside the critical section.
|
---|
[0b37882] | 585 | * As a side effect this function also sets the current
|
---|
| 586 | * pointer on successful allocation.
|
---|
[6db6fd1] | 587 | *
|
---|
[0b37882] | 588 | * @param area Heap area where to allocate from.
|
---|
| 589 | * @param first_block Starting heap block.
|
---|
| 590 | * @param final_block Heap block where to finish the search
|
---|
| 591 | * (may be NULL).
|
---|
| 592 | * @param real_size Gross number of bytes to allocate.
|
---|
| 593 | * @param falign Physical alignment of the block.
|
---|
[6db6fd1] | 594 | *
|
---|
[0b37882] | 595 | * @return Address of the allocated block or NULL on not enough memory.
|
---|
[6db6fd1] | 596 | *
|
---|
| 597 | */
|
---|
[0b37882] | 598 | static void *malloc_area(heap_area_t *area, heap_block_head_t *first_block,
|
---|
| 599 | heap_block_head_t *final_block, size_t real_size, size_t falign)
|
---|
[6db6fd1] | 600 | {
|
---|
[0b37882] | 601 | area_check((void *) area);
|
---|
[13f2461] | 602 | malloc_assert((void *) first_block >= (void *) AREA_FIRST_BLOCK_HEAD(area));
|
---|
| 603 | malloc_assert((void *) first_block < area->end);
|
---|
[6db6fd1] | 604 |
|
---|
[013a5d7] | 605 | for (heap_block_head_t *cur = first_block; (void *) cur < area->end;
|
---|
[0b37882] | 606 | cur = (heap_block_head_t *) (((void *) cur) + cur->size)) {
|
---|
[6db6fd1] | 607 | block_check(cur);
|
---|
| 608 |
|
---|
[0b37882] | 609 | /* Finish searching on the final block */
|
---|
| 610 | if ((final_block != NULL) && (cur == final_block))
|
---|
| 611 | break;
|
---|
| 612 |
|
---|
[6db6fd1] | 613 | /* Try to find a block that is free and large enough. */
|
---|
| 614 | if ((cur->free) && (cur->size >= real_size)) {
|
---|
[0b37882] | 615 | /*
|
---|
| 616 | * We have found a suitable block.
|
---|
| 617 | * Check for alignment properties.
|
---|
| 618 | */
|
---|
| 619 | void *addr = (void *)
|
---|
| 620 | ((uintptr_t) cur + sizeof(heap_block_head_t));
|
---|
| 621 | void *aligned = (void *)
|
---|
| 622 | ALIGN_UP((uintptr_t) addr, falign);
|
---|
[6db6fd1] | 623 |
|
---|
| 624 | if (addr == aligned) {
|
---|
| 625 | /* Exact block start including alignment. */
|
---|
| 626 | split_mark(cur, real_size);
|
---|
[0b37882] | 627 |
|
---|
[207533f] | 628 | next_fit = cur;
|
---|
[0b37882] | 629 | return addr;
|
---|
[6db6fd1] | 630 | } else {
|
---|
[d851f597] | 631 | /* Block start has to be aligned */
|
---|
[6db6fd1] | 632 | size_t excess = (size_t) (aligned - addr);
|
---|
| 633 |
|
---|
| 634 | if (cur->size >= real_size + excess) {
|
---|
[0b37882] | 635 | /*
|
---|
| 636 | * The current block is large enough to fit
|
---|
| 637 | * data in (including alignment).
|
---|
| 638 | */
|
---|
[013a5d7] | 639 | if ((void *) cur > (void *) AREA_FIRST_BLOCK_HEAD(area)) {
|
---|
[0b37882] | 640 | /*
|
---|
| 641 | * There is a block before the current block.
|
---|
| 642 | * This previous block can be enlarged to
|
---|
| 643 | * compensate for the alignment excess.
|
---|
| 644 | */
|
---|
| 645 | heap_block_foot_t *prev_foot = (heap_block_foot_t *)
|
---|
| 646 | ((void *) cur - sizeof(heap_block_foot_t));
|
---|
[6db6fd1] | 647 |
|
---|
[0b37882] | 648 | heap_block_head_t *prev_head = (heap_block_head_t *)
|
---|
| 649 | ((void *) cur - prev_foot->size);
|
---|
[6db6fd1] | 650 |
|
---|
| 651 | block_check(prev_head);
|
---|
| 652 |
|
---|
| 653 | size_t reduced_size = cur->size - excess;
|
---|
[d851f597] | 654 | heap_block_head_t *next_head = ((void *) cur) + excess;
|
---|
[6db6fd1] | 655 |
|
---|
[0b37882] | 656 | if ((!prev_head->free) &&
|
---|
| 657 | (excess >= STRUCT_OVERHEAD)) {
|
---|
| 658 | /*
|
---|
| 659 | * The previous block is not free and there
|
---|
| 660 | * is enough free space left to fill in
|
---|
| 661 | * a new free block between the previous
|
---|
| 662 | * and current block.
|
---|
| 663 | */
|
---|
| 664 | block_init(cur, excess, true, area);
|
---|
[d851f597] | 665 | } else {
|
---|
[0b37882] | 666 | /*
|
---|
| 667 | * The previous block is free (thus there
|
---|
| 668 | * is no need to induce additional
|
---|
| 669 | * fragmentation to the heap) or the
|
---|
| 670 | * excess is small. Therefore just enlarge
|
---|
| 671 | * the previous block.
|
---|
| 672 | */
|
---|
| 673 | block_init(prev_head, prev_head->size + excess,
|
---|
| 674 | prev_head->free, area);
|
---|
[d851f597] | 675 | }
|
---|
| 676 |
|
---|
[0b37882] | 677 | block_init(next_head, reduced_size, true, area);
|
---|
[d851f597] | 678 | split_mark(next_head, real_size);
|
---|
[0b37882] | 679 |
|
---|
[207533f] | 680 | next_fit = next_head;
|
---|
[0b37882] | 681 | return aligned;
|
---|
[6db6fd1] | 682 | } else {
|
---|
[0b37882] | 683 | /*
|
---|
| 684 | * The current block is the first block
|
---|
| 685 | * in the heap area. We have to make sure
|
---|
| 686 | * that the alignment excess is large enough
|
---|
| 687 | * to fit a new free block just before the
|
---|
| 688 | * current block.
|
---|
| 689 | */
|
---|
[6db6fd1] | 690 | while (excess < STRUCT_OVERHEAD) {
|
---|
| 691 | aligned += falign;
|
---|
| 692 | excess += falign;
|
---|
| 693 | }
|
---|
| 694 |
|
---|
[d851f597] | 695 | /* Check for current block size again */
|
---|
[6db6fd1] | 696 | if (cur->size >= real_size + excess) {
|
---|
| 697 | size_t reduced_size = cur->size - excess;
|
---|
[0b37882] | 698 | cur = (heap_block_head_t *)
|
---|
[013a5d7] | 699 | (AREA_FIRST_BLOCK_HEAD(area) + excess);
|
---|
[6db6fd1] | 700 |
|
---|
[013a5d7] | 701 | block_init((void *) AREA_FIRST_BLOCK_HEAD(area),
|
---|
| 702 | excess, true, area);
|
---|
[0b37882] | 703 | block_init(cur, reduced_size, true, area);
|
---|
[6db6fd1] | 704 | split_mark(cur, real_size);
|
---|
[0b37882] | 705 |
|
---|
[207533f] | 706 | next_fit = cur;
|
---|
[0b37882] | 707 | return aligned;
|
---|
[6db6fd1] | 708 | }
|
---|
| 709 | }
|
---|
| 710 | }
|
---|
| 711 | }
|
---|
| 712 | }
|
---|
[0b37882] | 713 | }
|
---|
| 714 |
|
---|
| 715 | return NULL;
|
---|
| 716 | }
|
---|
| 717 |
|
---|
[7aafdb86] | 718 | /** Try to enlarge any of the heap areas.
|
---|
| 719 | *
|
---|
[da287d1] | 720 | * If successful, allocate block of the given size in the area.
|
---|
[7aafdb86] | 721 | * Should be called only inside the critical section.
|
---|
| 722 | *
|
---|
[da287d1] | 723 | * @param size Gross size of item to allocate (bytes).
|
---|
[7aafdb86] | 724 | * @param align Memory address alignment.
|
---|
| 725 | *
|
---|
[da287d1] | 726 | * @return Allocated block.
|
---|
| 727 | * @return NULL on failure.
|
---|
| 728 | *
|
---|
[7aafdb86] | 729 | */
|
---|
| 730 | static void *heap_grow_and_alloc(size_t size, size_t align)
|
---|
| 731 | {
|
---|
| 732 | if (size == 0)
|
---|
| 733 | return NULL;
|
---|
[da287d1] | 734 |
|
---|
[7aafdb86] | 735 | /* First try to enlarge some existing area */
|
---|
| 736 | for (heap_area_t *area = first_heap_area; area != NULL;
|
---|
| 737 | area = area->next) {
|
---|
[da287d1] | 738 |
|
---|
[7aafdb86] | 739 | if (area_grow(area, size + align)) {
|
---|
[da287d1] | 740 | heap_block_head_t *first =
|
---|
| 741 | (heap_block_head_t *) AREA_LAST_BLOCK_HEAD(area);
|
---|
[7aafdb86] | 742 |
|
---|
[da287d1] | 743 | void *addr =
|
---|
| 744 | malloc_area(area, first, NULL, size, align);
|
---|
[7aafdb86] | 745 | malloc_assert(addr != NULL);
|
---|
| 746 | return addr;
|
---|
| 747 | }
|
---|
| 748 | }
|
---|
| 749 |
|
---|
| 750 | /* Eventually try to create a new area */
|
---|
| 751 | if (area_create(AREA_OVERHEAD(size + align))) {
|
---|
[da287d1] | 752 | heap_block_head_t *first =
|
---|
| 753 | (heap_block_head_t *) AREA_FIRST_BLOCK_HEAD(last_heap_area);
|
---|
[7aafdb86] | 754 |
|
---|
[da287d1] | 755 | void *addr =
|
---|
| 756 | malloc_area(last_heap_area, first, NULL, size, align);
|
---|
[7aafdb86] | 757 | malloc_assert(addr != NULL);
|
---|
| 758 | return addr;
|
---|
| 759 | }
|
---|
| 760 |
|
---|
| 761 | return NULL;
|
---|
| 762 | }
|
---|
| 763 |
|
---|
[0b37882] | 764 | /** Allocate a memory block
|
---|
| 765 | *
|
---|
| 766 | * Should be called only inside the critical section.
|
---|
| 767 | *
|
---|
| 768 | * @param size The size of the block to allocate.
|
---|
| 769 | * @param align Memory address alignment.
|
---|
| 770 | *
|
---|
| 771 | * @return Address of the allocated block or NULL on not enough memory.
|
---|
| 772 | *
|
---|
| 773 | */
|
---|
| 774 | static void *malloc_internal(const size_t size, const size_t align)
|
---|
| 775 | {
|
---|
[13f2461] | 776 | malloc_assert(first_heap_area != NULL);
|
---|
[0b37882] | 777 |
|
---|
| 778 | if (align == 0)
|
---|
| 779 | return NULL;
|
---|
| 780 |
|
---|
| 781 | size_t falign = lcm(align, BASE_ALIGN);
|
---|
[da287d1] | 782 |
|
---|
[e7c3fa0] | 783 | /* Check for integer overflow. */
|
---|
| 784 | if (falign < align)
|
---|
| 785 | return NULL;
|
---|
[0b37882] | 786 |
|
---|
[c828803] | 787 | /*
|
---|
| 788 | * The size of the allocated block needs to be naturally
|
---|
| 789 | * aligned, because the footer structure also needs to reside
|
---|
| 790 | * on a naturally aligned address in order to avoid unaligned
|
---|
| 791 | * memory accesses.
|
---|
| 792 | */
|
---|
[da287d1] | 793 | size_t gross_size = GROSS_SIZE(ALIGN_UP(size, BASE_ALIGN));
|
---|
[0b37882] | 794 |
|
---|
| 795 | /* Try the next fit approach */
|
---|
[da287d1] | 796 | heap_block_head_t *split = next_fit;
|
---|
[0b37882] | 797 |
|
---|
| 798 | if (split != NULL) {
|
---|
[7aafdb86] | 799 | void *addr = malloc_area(split->area, split, NULL, gross_size,
|
---|
[0b37882] | 800 | falign);
|
---|
[6db6fd1] | 801 |
|
---|
[0b37882] | 802 | if (addr != NULL)
|
---|
| 803 | return addr;
|
---|
[6db6fd1] | 804 | }
|
---|
| 805 |
|
---|
[0b37882] | 806 | /* Search the entire heap */
|
---|
[013a5d7] | 807 | for (heap_area_t *area = first_heap_area; area != NULL;
|
---|
| 808 | area = area->next) {
|
---|
[0b37882] | 809 | heap_block_head_t *first = (heap_block_head_t *)
|
---|
[013a5d7] | 810 | AREA_FIRST_BLOCK_HEAD(area);
|
---|
[0b37882] | 811 |
|
---|
[7aafdb86] | 812 | void *addr = malloc_area(area, first, split, gross_size,
|
---|
[0b37882] | 813 | falign);
|
---|
| 814 |
|
---|
| 815 | if (addr != NULL)
|
---|
| 816 | return addr;
|
---|
| 817 | }
|
---|
| 818 |
|
---|
[7aafdb86] | 819 | /* Finally, try to grow heap space and allocate in the new area. */
|
---|
| 820 | return heap_grow_and_alloc(gross_size, falign);
|
---|
[6db6fd1] | 821 | }
|
---|
| 822 |
|
---|
[3292623] | 823 | /** Allocate memory by number of elements
|
---|
| 824 | *
|
---|
| 825 | * @param nmemb Number of members to allocate.
|
---|
| 826 | * @param size Size of one member in bytes.
|
---|
| 827 | *
|
---|
| 828 | * @return Allocated memory or NULL.
|
---|
| 829 | *
|
---|
| 830 | */
|
---|
[15b8e495] | 831 | void *calloc(const size_t nmemb, const size_t size)
|
---|
| 832 | {
|
---|
[c828803] | 833 | // FIXME: Check for overflow
|
---|
| 834 |
|
---|
[e6b73ad0] | 835 | void *block = malloc(nmemb * size);
|
---|
| 836 | if (block == NULL)
|
---|
| 837 | return NULL;
|
---|
[3292623] | 838 |
|
---|
[e6b73ad0] | 839 | memset(block, 0, nmemb * size);
|
---|
| 840 | return block;
|
---|
[15b8e495] | 841 | }
|
---|
| 842 |
|
---|
[3292623] | 843 | /** Allocate memory
|
---|
| 844 | *
|
---|
| 845 | * @param size Number of bytes to allocate.
|
---|
| 846 | *
|
---|
| 847 | * @return Allocated memory or NULL.
|
---|
| 848 | *
|
---|
| 849 | */
|
---|
[6db6fd1] | 850 | void *malloc(const size_t size)
|
---|
| 851 | {
|
---|
[af5dfa5b] | 852 | heap_lock();
|
---|
[3292623] | 853 | void *block = malloc_internal(size, BASE_ALIGN);
|
---|
[af5dfa5b] | 854 | heap_unlock();
|
---|
[d54b303] | 855 |
|
---|
[3292623] | 856 | return block;
|
---|
[6db6fd1] | 857 | }
|
---|
| 858 |
|
---|
[3292623] | 859 | /** Allocate memory with specified alignment
|
---|
| 860 | *
|
---|
| 861 | * @param align Alignment in byes.
|
---|
| 862 | * @param size Number of bytes to allocate.
|
---|
| 863 | *
|
---|
| 864 | * @return Allocated memory or NULL.
|
---|
| 865 | *
|
---|
| 866 | */
|
---|
[6db6fd1] | 867 | void *memalign(const size_t align, const size_t size)
|
---|
| 868 | {
|
---|
| 869 | if (align == 0)
|
---|
| 870 | return NULL;
|
---|
| 871 |
|
---|
| 872 | size_t palign =
|
---|
| 873 | 1 << (fnzb(max(sizeof(void *), align) - 1) + 1);
|
---|
[9a3b469] | 874 |
|
---|
[af5dfa5b] | 875 | heap_lock();
|
---|
[3292623] | 876 | void *block = malloc_internal(size, palign);
|
---|
[af5dfa5b] | 877 | heap_unlock();
|
---|
[9a3b469] | 878 |
|
---|
[3292623] | 879 | return block;
|
---|
[6db6fd1] | 880 | }
|
---|
| 881 |
|
---|
[3292623] | 882 | /** Reallocate memory block
|
---|
| 883 | *
|
---|
| 884 | * @param addr Already allocated memory or NULL.
|
---|
| 885 | * @param size New size of the memory block.
|
---|
| 886 | *
|
---|
| 887 | * @return Reallocated memory or NULL.
|
---|
| 888 | *
|
---|
| 889 | */
|
---|
[6db6fd1] | 890 | void *realloc(const void *addr, const size_t size)
|
---|
| 891 | {
|
---|
| 892 | if (addr == NULL)
|
---|
| 893 | return malloc(size);
|
---|
| 894 |
|
---|
[af5dfa5b] | 895 | heap_lock();
|
---|
[3292623] | 896 |
|
---|
[6db6fd1] | 897 | /* Calculate the position of the header. */
|
---|
| 898 | heap_block_head_t *head =
|
---|
| 899 | (heap_block_head_t *) (addr - sizeof(heap_block_head_t));
|
---|
| 900 |
|
---|
| 901 | block_check(head);
|
---|
[13f2461] | 902 | malloc_assert(!head->free);
|
---|
[6db6fd1] | 903 |
|
---|
[0b37882] | 904 | heap_area_t *area = head->area;
|
---|
| 905 |
|
---|
| 906 | area_check(area);
|
---|
[13f2461] | 907 | malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
|
---|
| 908 | malloc_assert((void *) head < area->end);
|
---|
[0b37882] | 909 |
|
---|
[6db6fd1] | 910 | void *ptr = NULL;
|
---|
[3292623] | 911 | bool reloc = false;
|
---|
[f450280] | 912 | size_t real_size = GROSS_SIZE(ALIGN_UP(size, BASE_ALIGN));
|
---|
[6db6fd1] | 913 | size_t orig_size = head->size;
|
---|
| 914 |
|
---|
| 915 | if (orig_size > real_size) {
|
---|
| 916 | /* Shrink */
|
---|
| 917 | if (orig_size - real_size >= STRUCT_OVERHEAD) {
|
---|
[0b37882] | 918 | /*
|
---|
| 919 | * Split the original block to a full block
|
---|
| 920 | * and a trailing free block.
|
---|
| 921 | */
|
---|
| 922 | block_init((void *) head, real_size, false, area);
|
---|
[6db6fd1] | 923 | block_init((void *) head + real_size,
|
---|
[0b37882] | 924 | orig_size - real_size, true, area);
|
---|
[013a5d7] | 925 | heap_shrink(area);
|
---|
[6db6fd1] | 926 | }
|
---|
| 927 |
|
---|
| 928 | ptr = ((void *) head) + sizeof(heap_block_head_t);
|
---|
| 929 | } else {
|
---|
[0b37882] | 930 | /*
|
---|
| 931 | * Look at the next block. If it is free and the size is
|
---|
| 932 | * sufficient then merge the two. Otherwise just allocate
|
---|
| 933 | * a new block, copy the original data into it and
|
---|
| 934 | * free the original block.
|
---|
| 935 | */
|
---|
[6db6fd1] | 936 | heap_block_head_t *next_head =
|
---|
| 937 | (heap_block_head_t *) (((void *) head) + head->size);
|
---|
| 938 |
|
---|
[0b37882] | 939 | if (((void *) next_head < area->end) &&
|
---|
[7d88587] | 940 | (head->size + next_head->size >= real_size) &&
|
---|
| 941 | (next_head->free)) {
|
---|
[6db6fd1] | 942 | block_check(next_head);
|
---|
[0b37882] | 943 | block_init(head, head->size + next_head->size, false, area);
|
---|
[ba0aa6f] | 944 | split_mark(head, real_size);
|
---|
[6db6fd1] | 945 |
|
---|
| 946 | ptr = ((void *) head) + sizeof(heap_block_head_t);
|
---|
[207533f] | 947 | next_fit = NULL;
|
---|
[3292623] | 948 | } else
|
---|
| 949 | reloc = true;
|
---|
| 950 | }
|
---|
| 951 |
|
---|
[af5dfa5b] | 952 | heap_unlock();
|
---|
[3292623] | 953 |
|
---|
| 954 | if (reloc) {
|
---|
| 955 | ptr = malloc(size);
|
---|
| 956 | if (ptr != NULL) {
|
---|
| 957 | memcpy(ptr, addr, NET_SIZE(orig_size));
|
---|
| 958 | free(addr);
|
---|
[6db6fd1] | 959 | }
|
---|
| 960 | }
|
---|
| 961 |
|
---|
| 962 | return ptr;
|
---|
| 963 | }
|
---|
| 964 |
|
---|
| 965 | /** Free a memory block
|
---|
| 966 | *
|
---|
| 967 | * @param addr The address of the block.
|
---|
[3292623] | 968 | *
|
---|
[6db6fd1] | 969 | */
|
---|
| 970 | void free(const void *addr)
|
---|
| 971 | {
|
---|
[3019612] | 972 | if (addr == NULL)
|
---|
| 973 | return;
|
---|
[c828803] | 974 |
|
---|
[af5dfa5b] | 975 | heap_lock();
|
---|
[3292623] | 976 |
|
---|
[6db6fd1] | 977 | /* Calculate the position of the header. */
|
---|
| 978 | heap_block_head_t *head
|
---|
| 979 | = (heap_block_head_t *) (addr - sizeof(heap_block_head_t));
|
---|
| 980 |
|
---|
| 981 | block_check(head);
|
---|
[13f2461] | 982 | malloc_assert(!head->free);
|
---|
[6db6fd1] | 983 |
|
---|
[0b37882] | 984 | heap_area_t *area = head->area;
|
---|
| 985 |
|
---|
| 986 | area_check(area);
|
---|
[13f2461] | 987 | malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
|
---|
| 988 | malloc_assert((void *) head < area->end);
|
---|
[0b37882] | 989 |
|
---|
[6db6fd1] | 990 | /* Mark the block itself as free. */
|
---|
| 991 | head->free = true;
|
---|
| 992 |
|
---|
| 993 | /* Look at the next block. If it is free, merge the two. */
|
---|
| 994 | heap_block_head_t *next_head
|
---|
| 995 | = (heap_block_head_t *) (((void *) head) + head->size);
|
---|
| 996 |
|
---|
[0b37882] | 997 | if ((void *) next_head < area->end) {
|
---|
[6db6fd1] | 998 | block_check(next_head);
|
---|
| 999 | if (next_head->free)
|
---|
[0b37882] | 1000 | block_init(head, head->size + next_head->size, true, area);
|
---|
[6db6fd1] | 1001 | }
|
---|
| 1002 |
|
---|
| 1003 | /* Look at the previous block. If it is free, merge the two. */
|
---|
[013a5d7] | 1004 | if ((void *) head > (void *) AREA_FIRST_BLOCK_HEAD(area)) {
|
---|
[6db6fd1] | 1005 | heap_block_foot_t *prev_foot =
|
---|
| 1006 | (heap_block_foot_t *) (((void *) head) - sizeof(heap_block_foot_t));
|
---|
| 1007 |
|
---|
| 1008 | heap_block_head_t *prev_head =
|
---|
| 1009 | (heap_block_head_t *) (((void *) head) - prev_foot->size);
|
---|
| 1010 |
|
---|
| 1011 | block_check(prev_head);
|
---|
| 1012 |
|
---|
| 1013 | if (prev_head->free)
|
---|
[0b37882] | 1014 | block_init(prev_head, prev_head->size + head->size, true,
|
---|
| 1015 | area);
|
---|
[6db6fd1] | 1016 | }
|
---|
| 1017 |
|
---|
[013a5d7] | 1018 | heap_shrink(area);
|
---|
[3292623] | 1019 |
|
---|
[af5dfa5b] | 1020 | heap_unlock();
|
---|
[6db6fd1] | 1021 | }
|
---|
| 1022 |
|
---|
[1b3e854] | 1023 | void *heap_check(void)
|
---|
| 1024 | {
|
---|
[af5dfa5b] | 1025 | heap_lock();
|
---|
[1b3e854] | 1026 |
|
---|
| 1027 | if (first_heap_area == NULL) {
|
---|
[af5dfa5b] | 1028 | heap_unlock();
|
---|
[1b3e854] | 1029 | return (void *) -1;
|
---|
| 1030 | }
|
---|
| 1031 |
|
---|
| 1032 | /* Walk all heap areas */
|
---|
| 1033 | for (heap_area_t *area = first_heap_area; area != NULL;
|
---|
| 1034 | area = area->next) {
|
---|
| 1035 |
|
---|
| 1036 | /* Check heap area consistency */
|
---|
| 1037 | if ((area->magic != HEAP_AREA_MAGIC) ||
|
---|
| 1038 | ((void *) area != area->start) ||
|
---|
| 1039 | (area->start >= area->end) ||
|
---|
| 1040 | (((uintptr_t) area->start % PAGE_SIZE) != 0) ||
|
---|
| 1041 | (((uintptr_t) area->end % PAGE_SIZE) != 0)) {
|
---|
[af5dfa5b] | 1042 | heap_unlock();
|
---|
[1b3e854] | 1043 | return (void *) area;
|
---|
| 1044 | }
|
---|
| 1045 |
|
---|
| 1046 | /* Walk all heap blocks */
|
---|
| 1047 | for (heap_block_head_t *head = (heap_block_head_t *)
|
---|
| 1048 | AREA_FIRST_BLOCK_HEAD(area); (void *) head < area->end;
|
---|
| 1049 | head = (heap_block_head_t *) (((void *) head) + head->size)) {
|
---|
| 1050 |
|
---|
| 1051 | /* Check heap block consistency */
|
---|
| 1052 | if (head->magic != HEAP_BLOCK_HEAD_MAGIC) {
|
---|
[af5dfa5b] | 1053 | heap_unlock();
|
---|
[1b3e854] | 1054 | return (void *) head;
|
---|
| 1055 | }
|
---|
| 1056 |
|
---|
| 1057 | heap_block_foot_t *foot = BLOCK_FOOT(head);
|
---|
| 1058 |
|
---|
| 1059 | if ((foot->magic != HEAP_BLOCK_FOOT_MAGIC) ||
|
---|
| 1060 | (head->size != foot->size)) {
|
---|
[af5dfa5b] | 1061 | heap_unlock();
|
---|
[1b3e854] | 1062 | return (void *) foot;
|
---|
| 1063 | }
|
---|
| 1064 | }
|
---|
| 1065 | }
|
---|
| 1066 |
|
---|
[af5dfa5b] | 1067 | heap_unlock();
|
---|
[1b3e854] | 1068 |
|
---|
| 1069 | return NULL;
|
---|
| 1070 | }
|
---|
| 1071 |
|
---|
[6db6fd1] | 1072 | /** @}
|
---|
| 1073 | */
|
---|