blob: 0a004d40e1ec80ae9e7b5d61c4f8d831462083d8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass94890462014-11-10 17:16:43 -07002/*
3 * Simple malloc implementation
4 *
5 * Copyright (c) 2014 Google, Inc
Simon Glass94890462014-11-10 17:16:43 -07006 */
7
Simon Glass8ed64a42018-11-18 08:14:26 -07008#define LOG_CATEGORY LOGC_ALLOC
9
Simon Glass94890462014-11-10 17:16:43 -070010#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass94890462014-11-10 17:16:43 -070012#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050013#include <mapmem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glass94890462014-11-10 17:16:43 -070015#include <asm/io.h>
Sean Anderson98011e22022-03-23 14:04:49 -040016#include <valgrind/valgrind.h>
Simon Glass94890462014-11-10 17:16:43 -070017
18DECLARE_GLOBAL_DATA_PTR;
19
Simon Glass8ed64a42018-11-18 08:14:26 -070020static void *alloc_simple(size_t bytes, int align)
Simon Glass94890462014-11-10 17:16:43 -070021{
Simon Glass8ed64a42018-11-18 08:14:26 -070022 ulong addr, new_ptr;
Simon Glass94890462014-11-10 17:16:43 -070023 void *ptr;
24
Simon Glass8ed64a42018-11-18 08:14:26 -070025 addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
26 new_ptr = addr + bytes - gd->malloc_base;
Pali Rohárdb8cde62022-02-03 19:51:37 +010027 log_debug("size=%lx, ptr=%lx, limit=%lx: ", (ulong)bytes, new_ptr,
Simon Glass8ed64a42018-11-18 08:14:26 -070028 gd->malloc_limit);
Simon Glasse8211b42016-03-06 19:27:55 -070029 if (new_ptr > gd->malloc_limit) {
Simon Glass8ed64a42018-11-18 08:14:26 -070030 log_err("alloc space exhausted\n");
Hans de Goede8b4e7282015-02-04 13:05:50 +010031 return NULL;
Simon Glasse8211b42016-03-06 19:27:55 -070032 }
Simon Glass8ed64a42018-11-18 08:14:26 -070033
34 ptr = map_sysmem(addr, bytes);
Simon Glass94890462014-11-10 17:16:43 -070035 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
Simon Glass65ba4122015-09-08 17:52:46 -060036
Simon Glass94890462014-11-10 17:16:43 -070037 return ptr;
38}
39
Simon Glass8ed64a42018-11-18 08:14:26 -070040void *malloc_simple(size_t bytes)
Simon Glassde9d70f2015-05-12 14:55:06 -060041{
Simon Glassde9d70f2015-05-12 14:55:06 -060042 void *ptr;
43
Simon Glass8ed64a42018-11-18 08:14:26 -070044 ptr = alloc_simple(bytes, 1);
45 if (!ptr)
46 return ptr;
Andrew F. Davis34597e42017-01-27 10:39:18 -060047
Simon Glass8ed64a42018-11-18 08:14:26 -070048 log_debug("%lx\n", (ulong)ptr);
Sean Anderson98011e22022-03-23 14:04:49 -040049 VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
Simon Glass8ed64a42018-11-18 08:14:26 -070050
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);
Sean Anderson98011e22022-03-23 14:04:49 -040062 VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
Simon Glass65ba4122015-09-08 17:52:46 -060063
Simon Glassde9d70f2015-05-12 14:55:06 -060064 return ptr;
65}
66
Hans de Goede9f9df6f2015-09-13 14:45:15 +020067#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
Simon Glass94890462014-11-10 17:16:43 -070068void *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 Glass8ed64a42018-11-18 08:14:26 -070074 if (!ptr)
75 return ptr;
76 memset(ptr, '\0', size);
Simon Glass94890462014-11-10 17:16:43 -070077
78 return ptr;
79}
Sean Anderson98011e22022-03-23 14:04:49 -040080
81#if IS_ENABLED(CONFIG_VALGRIND)
82void free_simple(void *ptr)
83{
84 VALGRIND_FREELIKE_BLOCK(ptr, 0);
85}
86#endif
Simon Glass94890462014-11-10 17:16:43 -070087#endif
Simon Glass8ed64a42018-11-18 08:14:26 -070088
89void 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}