blob: 479a1e47b640e987b84a1f57d983839acf83ec11 [file] [log] [blame]
Simon Glassc9356be2014-11-10 17:16:43 -07001/*
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>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050011#include <mapmem.h>
Simon Glassc9356be2014-11-10 17:16:43 -070012#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;
Simon Glass836ac742015-09-08 17:52:46 -060022 debug("%s: size=%zx, ptr=%lx, limit=%lx\n", __func__, bytes, new_ptr,
23 gd->malloc_limit);
Simon Glassc9356be2014-11-10 17:16:43 -070024 if (new_ptr > gd->malloc_limit)
Hans de Goede2c857172015-02-04 13:05:50 +010025 return NULL;
Simon Glassc9356be2014-11-10 17:16:43 -070026 ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes);
27 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
Simon Glass836ac742015-09-08 17:52:46 -060028
Simon Glassc9356be2014-11-10 17:16:43 -070029 return ptr;
30}
31
Simon Glassb6bfb6f2015-05-12 14:55:06 -060032void *memalign_simple(size_t align, size_t bytes)
33{
34 ulong addr, new_ptr;
35 void *ptr;
36
Simon Glass972ea532015-08-14 13:26:43 -060037 addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
Philipp Rosenberger596380d2015-09-08 12:41:24 +020038 new_ptr = addr + bytes - gd->malloc_base;
Simon Glassb6bfb6f2015-05-12 14:55:06 -060039 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));
Simon Glass836ac742015-09-08 17:52:46 -060043
Simon Glassb6bfb6f2015-05-12 14:55:06 -060044 return ptr;
45}
46
Hans de Goede1eb0c032015-09-13 14:45:15 +020047#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
Simon Glassc9356be2014-11-10 17:16:43 -070048void *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