]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/malloc_simple.c
Merge branch 'master' of git://git.denx.de/u-boot-video
[people/ms/u-boot.git] / common / malloc_simple.c
CommitLineData
c9356be3
SG
1/*
2 * Simple malloc implementation
3 *
4 * Copyright (c) 2014 Google, Inc
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <malloc.h>
0eb25b61 11#include <mapmem.h>
c9356be3
SG
12#include <asm/io.h>
13
14DECLARE_GLOBAL_DATA_PTR;
15
16void *malloc_simple(size_t bytes)
17{
18 ulong new_ptr;
19 void *ptr;
20
21 new_ptr = gd->malloc_ptr + bytes;
836ac74c
SG
22 debug("%s: size=%zx, ptr=%lx, limit=%lx\n", __func__, bytes, new_ptr,
23 gd->malloc_limit);
c9356be3 24 if (new_ptr > gd->malloc_limit)
2c857170 25 return NULL;
c9356be3
SG
26 ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes);
27 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
836ac74c 28
c9356be3
SG
29 return ptr;
30}
31
b6bfb6ff
SG
32void *memalign_simple(size_t align, size_t bytes)
33{
34 ulong addr, new_ptr;
35 void *ptr;
36
972ea533 37 addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
596380db 38 new_ptr = addr + bytes - gd->malloc_base;
b6bfb6ff
SG
39 if (new_ptr > gd->malloc_limit)
40 return NULL;
41 ptr = map_sysmem(addr, bytes);
42 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
836ac74c 43
b6bfb6ff
SG
44 return ptr;
45}
46
1eb0c03c 47#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
c9356be3
SG
48void *calloc(size_t nmemb, size_t elem_size)
49{
50 size_t size = nmemb * elem_size;
51 void *ptr;
52
53 ptr = malloc(size);
54 memset(ptr, '\0', size);
55
56 return ptr;
57}
58#endif