blob: 4e6d7952b3ca9711113a5c9f5d14c1bf97aa1e8c [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 Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass94890462014-11-10 17:16:43 -070011#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050012#include <mapmem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glass94890462014-11-10 17:16:43 -070014#include <asm/io.h>
Sean Anderson98011e22022-03-23 14:04:49 -040015#include <valgrind/valgrind.h>
Simon Glass94890462014-11-10 17:16:43 -070016
17DECLARE_GLOBAL_DATA_PTR;
18
Simon Glass8ed64a42018-11-18 08:14:26 -070019static void *alloc_simple(size_t bytes, int align)
Simon Glass94890462014-11-10 17:16:43 -070020{
Simon Glass8ed64a42018-11-18 08:14:26 -070021 ulong addr, new_ptr;
Simon Glass94890462014-11-10 17:16:43 -070022 void *ptr;
23
Simon Glass8ed64a42018-11-18 08:14:26 -070024 addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
25 new_ptr = addr + bytes - gd->malloc_base;
Pali Rohárdb8cde62022-02-03 19:51:37 +010026 log_debug("size=%lx, ptr=%lx, limit=%lx: ", (ulong)bytes, new_ptr,
Simon Glass8ed64a42018-11-18 08:14:26 -070027 gd->malloc_limit);
Simon Glasse8211b42016-03-06 19:27:55 -070028 if (new_ptr > gd->malloc_limit) {
Simon Glass8ed64a42018-11-18 08:14:26 -070029 log_err("alloc space exhausted\n");
Hans de Goede8b4e7282015-02-04 13:05:50 +010030 return NULL;
Simon Glasse8211b42016-03-06 19:27:55 -070031 }
Simon Glass8ed64a42018-11-18 08:14:26 -070032
33 ptr = map_sysmem(addr, bytes);
Simon Glass94890462014-11-10 17:16:43 -070034 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
Simon Glass65ba4122015-09-08 17:52:46 -060035
Simon Glass94890462014-11-10 17:16:43 -070036 return ptr;
37}
38
Simon Glass8ed64a42018-11-18 08:14:26 -070039void *malloc_simple(size_t bytes)
Simon Glassde9d70f2015-05-12 14:55:06 -060040{
Simon Glassde9d70f2015-05-12 14:55:06 -060041 void *ptr;
42
Simon Glass8ed64a42018-11-18 08:14:26 -070043 ptr = alloc_simple(bytes, 1);
44 if (!ptr)
45 return ptr;
Andrew F. Davis34597e42017-01-27 10:39:18 -060046
Simon Glass8ed64a42018-11-18 08:14:26 -070047 log_debug("%lx\n", (ulong)ptr);
Sean Anderson98011e22022-03-23 14:04:49 -040048 VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
Simon Glass8ed64a42018-11-18 08:14:26 -070049
50 return ptr;
51}
52
53void *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 Anderson98011e22022-03-23 14:04:49 -040061 VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
Simon Glass65ba4122015-09-08 17:52:46 -060062
Simon Glassde9d70f2015-05-12 14:55:06 -060063 return ptr;
64}
65
Hans de Goede9f9df6f2015-09-13 14:45:15 +020066#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
Simon Glass94890462014-11-10 17:16:43 -070067void *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 Glass8ed64a42018-11-18 08:14:26 -070073 if (!ptr)
74 return ptr;
75 memset(ptr, '\0', size);
Simon Glass94890462014-11-10 17:16:43 -070076
77 return ptr;
78}
Sean Anderson98011e22022-03-23 14:04:49 -040079
80#if IS_ENABLED(CONFIG_VALGRIND)
81void free_simple(void *ptr)
82{
83 VALGRIND_FREELIKE_BLOCK(ptr, 0);
84}
85#endif
Simon Glass94890462014-11-10 17:16:43 -070086#endif
Simon Glass8ed64a42018-11-18 08:14:26 -070087
88void malloc_simple_info(void)
89{
90 log_info("malloc_simple: %lx bytes used, %lx remain\n", gd->malloc_ptr,
91 CONFIG_VAL(SYS_MALLOC_F_LEN) - gd->malloc_ptr);
92}