"Das U-Boot" Source Tree
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Simple malloc implementation
4 *
5 * Copyright (c) 2014 Google, Inc
6 */
7
8#define LOG_CATEGORY LOGC_ALLOC
9
10#include <log.h>
11#include <malloc.h>
12#include <mapmem.h>
13#include <asm/global_data.h>
14#include <asm/io.h>
15#include <valgrind/valgrind.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19static void *alloc_simple(size_t bytes, int align)
20{
21 ulong addr, new_ptr;
22 void *ptr;
23
24 addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
25 new_ptr = addr + bytes - gd->malloc_base;
26 log_debug("size=%lx, ptr=%lx, limit=%x: ", (ulong)bytes, new_ptr,
27 gd->malloc_limit);
28 if (new_ptr > gd->malloc_limit) {
29 log_err("alloc space exhausted ptr %lx limit %x\n", new_ptr,
30 gd->malloc_limit);
31 return NULL;
32 }
33
34 ptr = map_sysmem(addr, bytes);
35 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
36
37 return ptr;
38}
39
40void *malloc_simple(size_t bytes)
41{
42 void *ptr;
43
44 ptr = alloc_simple(bytes, 1);
45 if (!ptr)
46 return ptr;
47
48 log_debug("%lx\n", (ulong)ptr);
49 VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
50
51 return ptr;
52}
53
54void *memalign_simple(size_t align, size_t bytes)
55{
56 void *ptr;
57
58 ptr = alloc_simple(bytes, align);
59 if (!ptr)
60 return ptr;
61 log_debug("aligned to %lx\n", (ulong)ptr);
62 VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
63
64 return ptr;
65}
66
67#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
68void *calloc(size_t nmemb, size_t elem_size)
69{
70 size_t size = nmemb * elem_size;
71 void *ptr;
72
73 ptr = malloc(size);
74 if (!ptr)
75 return ptr;
76 memset(ptr, '\0', size);
77
78 return ptr;
79}
80
81#if IS_ENABLED(CONFIG_VALGRIND)
82void free_simple(void *ptr)
83{
84 VALGRIND_FREELIKE_BLOCK(ptr, 0);
85}
86#endif
87#endif
88
89void malloc_simple_info(void)
90{
91 log_info("malloc_simple: %x bytes used, %x remain\n", gd->malloc_ptr,
92 CONFIG_VAL(SYS_MALLOC_F_LEN) - gd->malloc_ptr);
93}