blob: c14f8b59c178ee251cc79299f9fcccfbcb1e340a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc9356be2014-11-10 17:16:43 -07002/*
3 * Simple malloc implementation
4 *
5 * Copyright (c) 2014 Google, Inc
Simon Glassc9356be2014-11-10 17:16:43 -07006 */
7
8#include <common.h>
9#include <malloc.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050010#include <mapmem.h>
Simon Glassc9356be2014-11-10 17:16:43 -070011#include <asm/io.h>
12
13DECLARE_GLOBAL_DATA_PTR;
14
15void *malloc_simple(size_t bytes)
16{
17 ulong new_ptr;
18 void *ptr;
19
20 new_ptr = gd->malloc_ptr + bytes;
Simon Glass9a01cca2016-03-06 19:27:55 -070021 debug("%s: size=%zx, ptr=%lx, limit=%lx: ", __func__, bytes, new_ptr,
Simon Glass836ac742015-09-08 17:52:46 -060022 gd->malloc_limit);
Simon Glass9a01cca2016-03-06 19:27:55 -070023 if (new_ptr > gd->malloc_limit) {
24 debug("space exhausted\n");
Hans de Goede2c857172015-02-04 13:05:50 +010025 return NULL;
Simon Glass9a01cca2016-03-06 19:27:55 -070026 }
Simon Glassc9356be2014-11-10 17:16:43 -070027 ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes);
28 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
Simon Glass9a01cca2016-03-06 19:27:55 -070029 debug("%lx\n", (ulong)ptr);
Simon Glass836ac742015-09-08 17:52:46 -060030
Simon Glassc9356be2014-11-10 17:16:43 -070031 return ptr;
32}
33
Simon Glassb6bfb6f2015-05-12 14:55:06 -060034void *memalign_simple(size_t align, size_t bytes)
35{
36 ulong addr, new_ptr;
37 void *ptr;
38
Simon Glass972ea532015-08-14 13:26:43 -060039 addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
Philipp Rosenberger596380d2015-09-08 12:41:24 +020040 new_ptr = addr + bytes - gd->malloc_base;
Andrew F. Davis1923d542017-01-27 10:39:18 -060041 if (new_ptr > gd->malloc_limit) {
42 debug("space exhausted\n");
Simon Glassb6bfb6f2015-05-12 14:55:06 -060043 return NULL;
Andrew F. Davis1923d542017-01-27 10:39:18 -060044 }
45
Simon Glassb6bfb6f2015-05-12 14:55:06 -060046 ptr = map_sysmem(addr, bytes);
47 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
Andrew F. Davis1923d542017-01-27 10:39:18 -060048 debug("%lx\n", (ulong)ptr);
Simon Glass836ac742015-09-08 17:52:46 -060049
Simon Glassb6bfb6f2015-05-12 14:55:06 -060050 return ptr;
51}
52
Hans de Goede1eb0c032015-09-13 14:45:15 +020053#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
Simon Glassc9356be2014-11-10 17:16:43 -070054void *calloc(size_t nmemb, size_t elem_size)
55{
56 size_t size = nmemb * elem_size;
57 void *ptr;
58
59 ptr = malloc(size);
60 memset(ptr, '\0', size);
61
62 return ptr;
63}
64#endif