]> git.ipfire.org Git - thirdparty/u-boot.git/blame - common/malloc_simple.c
Merge tag '2019.01-next' of https://github.com/mbgg/u-boot
[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
SG
10#include <common.h>
11#include <malloc.h>
0eb25b61 12#include <mapmem.h>
c9356be3
SG
13#include <asm/io.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
7cbd2d2e 17static void *alloc_simple(size_t bytes, int align)
c9356be3 18{
7cbd2d2e 19 ulong addr, new_ptr;
c9356be3
SG
20 void *ptr;
21
7cbd2d2e
SG
22 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);
9a01cca7 26 if (new_ptr > gd->malloc_limit) {
7cbd2d2e 27 log_err("alloc space exhausted\n");
2c857170 28 return NULL;
9a01cca7 29 }
7cbd2d2e
SG
30
31 ptr = map_sysmem(addr, bytes);
c9356be3 32 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
836ac74c 33
c9356be3
SG
34 return ptr;
35}
36
7cbd2d2e 37void *malloc_simple(size_t bytes)
b6bfb6ff 38{
b6bfb6ff
SG
39 void *ptr;
40
7cbd2d2e
SG
41 ptr = alloc_simple(bytes, 1);
42 if (!ptr)
43 return ptr;
1923d54b 44
7cbd2d2e
SG
45 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);
836ac74c 58
b6bfb6ff
SG
59 return ptr;
60}
61
1eb0c03c 62#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
c9356be3
SG
63void *calloc(size_t nmemb, size_t elem_size)
64{
65 size_t size = nmemb * elem_size;
66 void *ptr;
67
68 ptr = malloc(size);
7cbd2d2e
SG
69 if (!ptr)
70 return ptr;
71 memset(ptr, '\0', size);
c9356be3
SG
72
73 return ptr;
74}
75#endif
7cbd2d2e
SG
76
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}