]> git.ipfire.org Git - thirdparty/u-boot.git/blame - common/malloc_simple.c
mmc: sdhci: Correct ADMA_DESC_LEN to 12
[thirdparty/u-boot.git] / common / malloc_simple.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
c9356be3
SG
2/*
3 * Simple malloc implementation
4 *
5 * Copyright (c) 2014 Google, Inc
c9356be3
SG
6 */
7
7cbd2d2e
SG
8#define LOG_CATEGORY LOGC_ALLOC
9
c9356be3 10#include <common.h>
f7ae49fc 11#include <log.h>
c9356be3 12#include <malloc.h>
0eb25b61 13#include <mapmem.h>
401d1c4f 14#include <asm/global_data.h>
c9356be3 15#include <asm/io.h>
bdaeea1b 16#include <valgrind/valgrind.h>
c9356be3
SG
17
18DECLARE_GLOBAL_DATA_PTR;
19
7cbd2d2e 20static void *alloc_simple(size_t bytes, int align)
c9356be3 21{
7cbd2d2e 22 ulong addr, new_ptr;
c9356be3
SG
23 void *ptr;
24
7cbd2d2e
SG
25 addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
26 new_ptr = addr + bytes - gd->malloc_base;
eebcdb34 27 log_debug("size=%lx, ptr=%lx, limit=%lx: ", (ulong)bytes, new_ptr,
7cbd2d2e 28 gd->malloc_limit);
9a01cca7 29 if (new_ptr > gd->malloc_limit) {
7cbd2d2e 30 log_err("alloc space exhausted\n");
2c857170 31 return NULL;
9a01cca7 32 }
7cbd2d2e
SG
33
34 ptr = map_sysmem(addr, bytes);
c9356be3 35 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
836ac74c 36
c9356be3
SG
37 return ptr;
38}
39
7cbd2d2e 40void *malloc_simple(size_t bytes)
b6bfb6ff 41{
b6bfb6ff
SG
42 void *ptr;
43
7cbd2d2e
SG
44 ptr = alloc_simple(bytes, 1);
45 if (!ptr)
46 return ptr;
1923d54b 47
7cbd2d2e 48 log_debug("%lx\n", (ulong)ptr);
bdaeea1b 49 VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
7cbd2d2e
SG
50
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);
bdaeea1b 62 VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
836ac74c 63
b6bfb6ff
SG
64 return ptr;
65}
66
1eb0c03c 67#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
c9356be3
SG
68void *calloc(size_t nmemb, size_t elem_size)
69{
70 size_t size = nmemb * elem_size;
71 void *ptr;
72
73 ptr = malloc(size);
7cbd2d2e
SG
74 if (!ptr)
75 return ptr;
76 memset(ptr, '\0', size);
c9356be3
SG
77
78 return ptr;
79}
bdaeea1b
SA
80
81#if IS_ENABLED(CONFIG_VALGRIND)
82void free_simple(void *ptr)
83{
84 VALGRIND_FREELIKE_BLOCK(ptr, 0);
85}
86#endif
c9356be3 87#endif
7cbd2d2e
SG
88
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}