1 // SPDX-License-Identifier: GPL-2.0+
3 * Simple malloc implementation
5 * Copyright (c) 2014 Google, Inc
8 #define LOG_CATEGORY LOGC_ALLOC
16 DECLARE_GLOBAL_DATA_PTR;
18 static void *alloc_simple(size_t bytes, int align)
23 addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
24 new_ptr = addr + bytes - gd->malloc_base;
25 log_debug("size=%zx, ptr=%lx, limit=%lx: ", bytes, new_ptr,
27 if (new_ptr > gd->malloc_limit) {
28 log_err("alloc space exhausted\n");
32 ptr = map_sysmem(addr, bytes);
33 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
38 void *malloc_simple(size_t bytes)
42 ptr = alloc_simple(bytes, 1);
46 log_debug("%lx\n", (ulong)ptr);
51 void *memalign_simple(size_t align, size_t bytes)
55 ptr = alloc_simple(bytes, align);
58 log_debug("aligned to %lx\n", (ulong)ptr);
63 #if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
64 void *calloc(size_t nmemb, size_t elem_size)
66 size_t size = nmemb * elem_size;
72 memset(ptr, '\0', size);
78 void malloc_simple_info(void)
80 log_info("malloc_simple: %lx bytes used, %lx remain\n", gd->malloc_ptr,
81 CONFIG_VAL(SYS_MALLOC_F_LEN) - gd->malloc_ptr);