]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
c9356be3 SG |
2 | /* |
3 | * Simple malloc implementation | |
4 | * | |
5 | * Copyright (c) 2014 Google, Inc | |
c9356be3 SG |
6 | */ |
7 | ||
7cbd2d2e SG |
8 | #define LOG_CATEGORY LOGC_ALLOC |
9 | ||
c9356be3 | 10 | #include <common.h> |
f7ae49fc | 11 | #include <log.h> |
c9356be3 | 12 | #include <malloc.h> |
0eb25b61 | 13 | #include <mapmem.h> |
401d1c4f | 14 | #include <asm/global_data.h> |
c9356be3 SG |
15 | #include <asm/io.h> |
16 | ||
17 | DECLARE_GLOBAL_DATA_PTR; | |
18 | ||
7cbd2d2e | 19 | static void *alloc_simple(size_t bytes, int align) |
c9356be3 | 20 | { |
7cbd2d2e | 21 | ulong addr, new_ptr; |
c9356be3 SG |
22 | void *ptr; |
23 | ||
7cbd2d2e SG |
24 | addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align); |
25 | new_ptr = addr + bytes - gd->malloc_base; | |
26 | log_debug("size=%zx, ptr=%lx, limit=%lx: ", bytes, new_ptr, | |
27 | gd->malloc_limit); | |
9a01cca7 | 28 | if (new_ptr > gd->malloc_limit) { |
7cbd2d2e | 29 | log_err("alloc space exhausted\n"); |
2c857170 | 30 | return NULL; |
9a01cca7 | 31 | } |
7cbd2d2e SG |
32 | |
33 | ptr = map_sysmem(addr, bytes); | |
c9356be3 | 34 | gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr)); |
836ac74c | 35 | |
c9356be3 SG |
36 | return ptr; |
37 | } | |
38 | ||
7cbd2d2e | 39 | void *malloc_simple(size_t bytes) |
b6bfb6ff | 40 | { |
b6bfb6ff SG |
41 | void *ptr; |
42 | ||
7cbd2d2e SG |
43 | ptr = alloc_simple(bytes, 1); |
44 | if (!ptr) | |
45 | return ptr; | |
1923d54b | 46 | |
7cbd2d2e SG |
47 | log_debug("%lx\n", (ulong)ptr); |
48 | ||
49 | return ptr; | |
50 | } | |
51 | ||
52 | void *memalign_simple(size_t align, size_t bytes) | |
53 | { | |
54 | void *ptr; | |
55 | ||
56 | ptr = alloc_simple(bytes, align); | |
57 | if (!ptr) | |
58 | return ptr; | |
59 | log_debug("aligned to %lx\n", (ulong)ptr); | |
836ac74c | 60 | |
b6bfb6ff SG |
61 | return ptr; |
62 | } | |
63 | ||
1eb0c03c | 64 | #if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE) |
c9356be3 SG |
65 | void *calloc(size_t nmemb, size_t elem_size) |
66 | { | |
67 | size_t size = nmemb * elem_size; | |
68 | void *ptr; | |
69 | ||
70 | ptr = malloc(size); | |
7cbd2d2e SG |
71 | if (!ptr) |
72 | return ptr; | |
73 | memset(ptr, '\0', size); | |
c9356be3 SG |
74 | |
75 | return ptr; | |
76 | } | |
77 | #endif | |
7cbd2d2e SG |
78 | |
79 | void malloc_simple_info(void) | |
80 | { | |
81 | log_info("malloc_simple: %lx bytes used, %lx remain\n", gd->malloc_ptr, | |
82 | CONFIG_VAL(SYS_MALLOC_F_LEN) - gd->malloc_ptr); | |
83 | } |