Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | /* * Copyright (c) 2018 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <stdlib.h> #include <zephyr.h> #include <init.h> #include <errno.h> #include <sys/math_extras.h> #include <sys/mempool.h> #include <string.h> #include <app_memory/app_memdomain.h> #define LOG_LEVEL CONFIG_KERNEL_LOG_LEVEL #include <logging/log.h> LOG_MODULE_DECLARE(os); #if (CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE > 0) #ifdef CONFIG_USERSPACE K_APPMEM_PARTITION_DEFINE(z_malloc_partition); #define POOL_SECTION K_APP_DMEM_SECTION(z_malloc_partition) #else #define POOL_SECTION .data #endif /* CONFIG_USERSPACE */ SYS_MEM_POOL_DEFINE(z_malloc_mem_pool, NULL, 16, CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE, 1, 4, POOL_SECTION); void *malloc(size_t size) { void *ret; ret = sys_mem_pool_alloc(&z_malloc_mem_pool, size); if (ret == NULL) { errno = ENOMEM; } return ret; } static int malloc_prepare(struct device *unused) { ARG_UNUSED(unused); sys_mem_pool_init(&z_malloc_mem_pool); return 0; } SYS_INIT(malloc_prepare, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); #else /* No malloc arena */ void *malloc(size_t size) { ARG_UNUSED(size); LOG_DBG("CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE is 0"); errno = ENOMEM; return NULL; } #endif void free(void *ptr) { sys_mem_pool_free(ptr); } void *calloc(size_t nmemb, size_t size) { void *ret; if (size_mul_overflow(nmemb, size, &size)) { errno = ENOMEM; return NULL; } ret = malloc(size); if (ret != NULL) { (void)memset(ret, 0, size); } return ret; } void *realloc(void *ptr, size_t requested_size) { struct sys_mem_pool_block *blk; size_t struct_blk_size = WB_UP(sizeof(struct sys_mem_pool_block)); size_t block_size, total_requested_size; void *new_ptr; if (ptr == NULL) { return malloc(requested_size); } if (requested_size == 0) { free(ptr); return NULL; } /* Stored right before the pointer passed to the user */ blk = (struct sys_mem_pool_block *)((char *)ptr - struct_blk_size); /* Determine size of previously allocated block by its level. * Most likely a bit larger than the original allocation */ block_size = blk->pool->base.max_sz; for (int i = 1; i <= blk->level; i++) { block_size = WB_DN(block_size / 4); } /* We really need this much memory */ total_requested_size = requested_size + struct_blk_size; if (block_size >= total_requested_size) { /* Existing block large enough, nothing to do */ return ptr; } new_ptr = malloc(requested_size); if (new_ptr == NULL) { return NULL; } memcpy(new_ptr, ptr, block_size - struct_blk_size); free(ptr); return new_ptr; } void *reallocarray(void *ptr, size_t nmemb, size_t size) { if (size_mul_overflow(nmemb, size, &size)) { errno = ENOMEM; return NULL; } return realloc(ptr, size); } |