1
2
3
4
5
6
7
8#define LOG_CATEGORY LOGC_ALLOC
9
10#include <common.h>
11#include <log.h>
12#include <malloc.h>
13#include <mapmem.h>
14#include <asm/io.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18static void *alloc_simple(size_t bytes, int align)
19{
20 ulong addr, new_ptr;
21 void *ptr;
22
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,
26 gd->malloc_limit);
27 if (new_ptr > gd->malloc_limit) {
28 log_err("alloc space exhausted\n");
29 return NULL;
30 }
31
32 ptr = map_sysmem(addr, bytes);
33 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
34
35 return ptr;
36}
37
38void *malloc_simple(size_t bytes)
39{
40 void *ptr;
41
42 ptr = alloc_simple(bytes, 1);
43 if (!ptr)
44 return ptr;
45
46 log_debug("%lx\n", (ulong)ptr);
47
48 return ptr;
49}
50
51void *memalign_simple(size_t align, size_t bytes)
52{
53 void *ptr;
54
55 ptr = alloc_simple(bytes, align);
56 if (!ptr)
57 return ptr;
58 log_debug("aligned to %lx\n", (ulong)ptr);
59
60 return ptr;
61}
62
63#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
64void *calloc(size_t nmemb, size_t elem_size)
65{
66 size_t size = nmemb * elem_size;
67 void *ptr;
68
69 ptr = malloc(size);
70 if (!ptr)
71 return ptr;
72 memset(ptr, '\0', size);
73
74 return ptr;
75}
76#endif
77
78void malloc_simple_info(void)
79{
80 log_info("malloc_simple: %lx bytes used, %lx remain\n", gd->malloc_ptr,
81 CONFIG_VAL(SYS_MALLOC_F_LEN) - gd->malloc_ptr);
82}
83