Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Simple malloc implementation |
| 4 | * |
| 5 | * Copyright (c) 2014 Google, Inc |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | 8ed64a4 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_ALLOC |
| 9 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
Joe Hershberger | 65b905b | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 12 | #include <mapmem.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 14 | #include <asm/io.h> |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 15 | #include <valgrind/valgrind.h> |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Simon Glass | 8ed64a4 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 19 | static void *alloc_simple(size_t bytes, int align) |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 20 | { |
Simon Glass | 8ed64a4 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 21 | ulong addr, new_ptr; |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 22 | void *ptr; |
| 23 | |
Simon Glass | 8ed64a4 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 24 | addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align); |
| 25 | new_ptr = addr + bytes - gd->malloc_base; |
Simon Glass | 91bbb3b | 2024-08-23 14:27:04 -0600 | [diff] [blame] | 26 | log_debug("size=%lx, ptr=%lx, limit=%x: ", (ulong)bytes, new_ptr, |
Simon Glass | 8ed64a4 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 27 | gd->malloc_limit); |
Simon Glass | e8211b4 | 2016-03-06 19:27:55 -0700 | [diff] [blame] | 28 | if (new_ptr > gd->malloc_limit) { |
Simon Glass | 8ed64a4 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 29 | log_err("alloc space exhausted\n"); |
Hans de Goede | 8b4e728 | 2015-02-04 13:05:50 +0100 | [diff] [blame] | 30 | return NULL; |
Simon Glass | e8211b4 | 2016-03-06 19:27:55 -0700 | [diff] [blame] | 31 | } |
Simon Glass | 8ed64a4 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 32 | |
| 33 | ptr = map_sysmem(addr, bytes); |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 34 | gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr)); |
Simon Glass | 65ba412 | 2015-09-08 17:52:46 -0600 | [diff] [blame] | 35 | |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 36 | return ptr; |
| 37 | } |
| 38 | |
Simon Glass | 8ed64a4 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 39 | void *malloc_simple(size_t bytes) |
Simon Glass | de9d70f | 2015-05-12 14:55:06 -0600 | [diff] [blame] | 40 | { |
Simon Glass | de9d70f | 2015-05-12 14:55:06 -0600 | [diff] [blame] | 41 | void *ptr; |
| 42 | |
Simon Glass | 8ed64a4 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 43 | ptr = alloc_simple(bytes, 1); |
| 44 | if (!ptr) |
| 45 | return ptr; |
Andrew F. Davis | 34597e4 | 2017-01-27 10:39:18 -0600 | [diff] [blame] | 46 | |
Simon Glass | 8ed64a4 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 47 | log_debug("%lx\n", (ulong)ptr); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 48 | VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false); |
Simon Glass | 8ed64a4 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 49 | |
| 50 | return ptr; |
| 51 | } |
| 52 | |
| 53 | void *memalign_simple(size_t align, size_t bytes) |
| 54 | { |
| 55 | void *ptr; |
| 56 | |
| 57 | ptr = alloc_simple(bytes, align); |
| 58 | if (!ptr) |
| 59 | return ptr; |
| 60 | log_debug("aligned to %lx\n", (ulong)ptr); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 61 | VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false); |
Simon Glass | 65ba412 | 2015-09-08 17:52:46 -0600 | [diff] [blame] | 62 | |
Simon Glass | de9d70f | 2015-05-12 14:55:06 -0600 | [diff] [blame] | 63 | return ptr; |
| 64 | } |
| 65 | |
Hans de Goede | 9f9df6f | 2015-09-13 14:45:15 +0200 | [diff] [blame] | 66 | #if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE) |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 67 | void *calloc(size_t nmemb, size_t elem_size) |
| 68 | { |
| 69 | size_t size = nmemb * elem_size; |
| 70 | void *ptr; |
| 71 | |
| 72 | ptr = malloc(size); |
Simon Glass | 8ed64a4 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 73 | if (!ptr) |
| 74 | return ptr; |
| 75 | memset(ptr, '\0', size); |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 76 | |
| 77 | return ptr; |
| 78 | } |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 79 | |
| 80 | #if IS_ENABLED(CONFIG_VALGRIND) |
| 81 | void free_simple(void *ptr) |
| 82 | { |
| 83 | VALGRIND_FREELIKE_BLOCK(ptr, 0); |
| 84 | } |
| 85 | #endif |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 86 | #endif |
Simon Glass | 8ed64a4 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 87 | |
| 88 | void malloc_simple_info(void) |
| 89 | { |
Simon Glass | 91bbb3b | 2024-08-23 14:27:04 -0600 | [diff] [blame] | 90 | log_info("malloc_simple: %x bytes used, %x remain\n", gd->malloc_ptr, |
Simon Glass | 8ed64a4 | 2018-11-18 08:14:26 -0700 | [diff] [blame] | 91 | CONFIG_VAL(SYS_MALLOC_F_LEN) - gd->malloc_ptr); |
| 92 | } |