1 // SPDX-License-Identifier: GPL-2.0+
3 * Simple malloc implementation
5 * Copyright (c) 2014 Google, Inc
13 DECLARE_GLOBAL_DATA_PTR;
15 void *malloc_simple(size_t bytes)
20 new_ptr = gd->malloc_ptr + bytes;
21 debug("%s: size=%zx, ptr=%lx, limit=%lx: ", __func__, bytes, new_ptr,
23 if (new_ptr > gd->malloc_limit) {
24 debug("space exhausted\n");
27 ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes);
28 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
29 debug("%lx\n", (ulong)ptr);
34 void *memalign_simple(size_t align, size_t bytes)
39 addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
40 new_ptr = addr + bytes - gd->malloc_base;
41 if (new_ptr > gd->malloc_limit) {
42 debug("space exhausted\n");
46 ptr = map_sysmem(addr, bytes);
47 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
48 debug("%lx\n", (ulong)ptr);
53 #if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
54 void *calloc(size_t nmemb, size_t elem_size)
56 size_t size = nmemb * elem_size;
60 memset(ptr, '\0', size);