Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Simple malloc implementation |
| 4 | * |
| 5 | * Copyright (c) 2014 Google, Inc |
Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | 7cbd2d2 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_ALLOC |
| 9 | |
Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Joe Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 13 | #include <mapmem.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 15 | #include <asm/io.h> |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 16 | #include <valgrind/valgrind.h> |
Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 17 | |
| 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Simon Glass | 7cbd2d2 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 20 | static void *alloc_simple(size_t bytes, int align) |
Simon Glass | b6bfb6f | 2015-05-12 14:55:06 -0600 | [diff] [blame] | 21 | { |
| 22 | ulong addr, new_ptr; |
| 23 | void *ptr; |
| 24 | |
Simon Glass | 972ea53 | 2015-08-14 13:26:43 -0600 | [diff] [blame] | 25 | addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align); |
Philipp Rosenberger | 596380d | 2015-09-08 12:41:24 +0200 | [diff] [blame] | 26 | new_ptr = addr + bytes - gd->malloc_base; |
Pali Rohár | eebcdb3 | 2022-02-03 19:51:37 +0100 | [diff] [blame] | 27 | log_debug("size=%lx, ptr=%lx, limit=%lx: ", (ulong)bytes, new_ptr, |
Simon Glass | 7cbd2d2 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 28 | gd->malloc_limit); |
Andrew F. Davis | 1923d54 | 2017-01-27 10:39:18 -0600 | [diff] [blame] | 29 | if (new_ptr > gd->malloc_limit) { |
Simon Glass | 7cbd2d2 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 30 | log_err("alloc space exhausted\n"); |
Simon Glass | b6bfb6f | 2015-05-12 14:55:06 -0600 | [diff] [blame] | 31 | return NULL; |
Andrew F. Davis | 1923d54 | 2017-01-27 10:39:18 -0600 | [diff] [blame] | 32 | } |
| 33 | |
Simon Glass | b6bfb6f | 2015-05-12 14:55:06 -0600 | [diff] [blame] | 34 | ptr = map_sysmem(addr, bytes); |
| 35 | gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr)); |
Simon Glass | 7cbd2d2 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 36 | |
| 37 | return ptr; |
| 38 | } |
| 39 | |
| 40 | void *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); |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 49 | VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false); |
Simon Glass | 7cbd2d2 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 50 | |
| 51 | return ptr; |
| 52 | } |
| 53 | |
| 54 | void *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); |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 62 | VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false); |
Simon Glass | 836ac74 | 2015-09-08 17:52:46 -0600 | [diff] [blame] | 63 | |
Simon Glass | b6bfb6f | 2015-05-12 14:55:06 -0600 | [diff] [blame] | 64 | return ptr; |
| 65 | } |
| 66 | |
Hans de Goede | 1eb0c03 | 2015-09-13 14:45:15 +0200 | [diff] [blame] | 67 | #if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE) |
Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 68 | void *calloc(size_t nmemb, size_t elem_size) |
| 69 | { |
| 70 | size_t size = nmemb * elem_size; |
| 71 | void *ptr; |
| 72 | |
| 73 | ptr = malloc(size); |
Simon Glass | 7cbd2d2 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 74 | if (!ptr) |
| 75 | return ptr; |
| 76 | memset(ptr, '\0', size); |
Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 77 | |
| 78 | return ptr; |
| 79 | } |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 80 | |
| 81 | #if IS_ENABLED(CONFIG_VALGRIND) |
| 82 | void free_simple(void *ptr) |
| 83 | { |
| 84 | VALGRIND_FREELIKE_BLOCK(ptr, 0); |
| 85 | } |
| 86 | #endif |
Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 87 | #endif |
Simon Glass | 7cbd2d2 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 88 | |
| 89 | void malloc_simple_info(void) |
| 90 | { |
| 91 | log_info("malloc_simple: %lx bytes used, %lx remain\n", gd->malloc_ptr, |
| 92 | CONFIG_VAL(SYS_MALLOC_F_LEN) - gd->malloc_ptr); |
| 93 | } |