blob: eabbb70128b375286691489fe2248bb94b1296f1 [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>
11#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050012#include <mapmem.h>
Simon Glass94890462014-11-10 17:16:43 -070013#include <asm/io.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
Simon Glass8ed64a42018-11-18 08:14:26 -070017static void *alloc_simple(size_t bytes, int align)
Simon Glass94890462014-11-10 17:16:43 -070018{
Simon Glass8ed64a42018-11-18 08:14:26 -070019 ulong addr, new_ptr;
Simon Glass94890462014-11-10 17:16:43 -070020 void *ptr;
21
Simon Glass8ed64a42018-11-18 08:14:26 -070022 addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
23 new_ptr = addr + bytes - gd->malloc_base;
24 log_debug("size=%zx, ptr=%lx, limit=%lx: ", bytes, new_ptr,
25 gd->malloc_limit);
Simon Glasse8211b42016-03-06 19:27:55 -070026 if (new_ptr > gd->malloc_limit) {
Simon Glass8ed64a42018-11-18 08:14:26 -070027 log_err("alloc space exhausted\n");
Hans de Goede8b4e7282015-02-04 13:05:50 +010028 return NULL;
Simon Glasse8211b42016-03-06 19:27:55 -070029 }
Simon Glass8ed64a42018-11-18 08:14:26 -070030
31 ptr = map_sysmem(addr, bytes);
Simon Glass94890462014-11-10 17:16:43 -070032 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
Simon Glass65ba4122015-09-08 17:52:46 -060033
Simon Glass94890462014-11-10 17:16:43 -070034 return ptr;
35}
36
Simon Glass8ed64a42018-11-18 08:14:26 -070037void *malloc_simple(size_t bytes)
Simon Glassde9d70f2015-05-12 14:55:06 -060038{
Simon Glassde9d70f2015-05-12 14:55:06 -060039 void *ptr;
40
Simon Glass8ed64a42018-11-18 08:14:26 -070041 ptr = alloc_simple(bytes, 1);
42 if (!ptr)
43 return ptr;
Andrew F. Davis34597e42017-01-27 10:39:18 -060044
Simon Glass8ed64a42018-11-18 08:14:26 -070045 log_debug("%lx\n", (ulong)ptr);
46
47 return ptr;
48}
49
50void *memalign_simple(size_t align, size_t bytes)
51{
52 void *ptr;
53
54 ptr = alloc_simple(bytes, align);
55 if (!ptr)
56 return ptr;
57 log_debug("aligned to %lx\n", (ulong)ptr);
Simon Glass65ba4122015-09-08 17:52:46 -060058
Simon Glassde9d70f2015-05-12 14:55:06 -060059 return ptr;
60}
61
Hans de Goede9f9df6f2015-09-13 14:45:15 +020062#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
Simon Glass94890462014-11-10 17:16:43 -070063void *calloc(size_t nmemb, size_t elem_size)
64{
65 size_t size = nmemb * elem_size;
66 void *ptr;
67
68 ptr = malloc(size);
Simon Glass8ed64a42018-11-18 08:14:26 -070069 if (!ptr)
70 return ptr;
71 memset(ptr, '\0', size);
Simon Glass94890462014-11-10 17:16:43 -070072
73 return ptr;
74}
75#endif
Simon Glass8ed64a42018-11-18 08:14:26 -070076
77void malloc_simple_info(void)
78{
79 log_info("malloc_simple: %lx bytes used, %lx remain\n", gd->malloc_ptr,
80 CONFIG_VAL(SYS_MALLOC_F_LEN) - gd->malloc_ptr);
81}