blob: 611400265ba3efc30db2f1519e72984098d35685 [file] [log] [blame]
Simon Glass94890462014-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 Hershberger65b905b2015-03-22 17:08:59 -050011#include <mapmem.h>
Simon Glass94890462014-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 Glasse8211b42016-03-06 19:27:55 -070022 debug("%s: size=%zx, ptr=%lx, limit=%lx: ", __func__, bytes, new_ptr,
Simon Glass65ba4122015-09-08 17:52:46 -060023 gd->malloc_limit);
Simon Glasse8211b42016-03-06 19:27:55 -070024 if (new_ptr > gd->malloc_limit) {
25 debug("space exhausted\n");
Hans de Goede8b4e7282015-02-04 13:05:50 +010026 return NULL;
Simon Glasse8211b42016-03-06 19:27:55 -070027 }
Simon Glass94890462014-11-10 17:16:43 -070028 ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes);
29 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
Simon Glasse8211b42016-03-06 19:27:55 -070030 debug("%lx\n", (ulong)ptr);
Simon Glass65ba4122015-09-08 17:52:46 -060031
Simon Glass94890462014-11-10 17:16:43 -070032 return ptr;
33}
34
Simon Glassde9d70f2015-05-12 14:55:06 -060035void *memalign_simple(size_t align, size_t bytes)
36{
37 ulong addr, new_ptr;
38 void *ptr;
39
Simon Glass57dbd912015-08-14 13:26:43 -060040 addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
Philipp Rosenberger4dd5bdc2015-09-08 12:41:24 +020041 new_ptr = addr + bytes - gd->malloc_base;
Andrew F. Davis34597e42017-01-27 10:39:18 -060042 if (new_ptr > gd->malloc_limit) {
43 debug("space exhausted\n");
Simon Glassde9d70f2015-05-12 14:55:06 -060044 return NULL;
Andrew F. Davis34597e42017-01-27 10:39:18 -060045 }
46
Simon Glassde9d70f2015-05-12 14:55:06 -060047 ptr = map_sysmem(addr, bytes);
48 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
Andrew F. Davis34597e42017-01-27 10:39:18 -060049 debug("%lx\n", (ulong)ptr);
Simon Glass65ba4122015-09-08 17:52:46 -060050
Simon Glassde9d70f2015-05-12 14:55:06 -060051 return ptr;
52}
53
Hans de Goede9f9df6f2015-09-13 14:45:15 +020054#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
Simon Glass94890462014-11-10 17:16:43 -070055void *calloc(size_t nmemb, size_t elem_size)
56{
57 size_t size = nmemb * elem_size;
58 void *ptr;
59
60 ptr = malloc(size);
61 memset(ptr, '\0', size);
62
63 return ptr;
64}
65#endif