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