blob: 9811ab60f6d174a35c9b4e68d39321bca4beb8be [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;
22 if (new_ptr > gd->malloc_limit)
Hans de Goede8b4e7282015-02-04 13:05:50 +010023 return NULL;
Simon Glass94890462014-11-10 17:16:43 -070024 ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes);
25 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
26 return ptr;
27}
28
Simon Glassde9d70f2015-05-12 14:55:06 -060029void *memalign_simple(size_t align, size_t bytes)
30{
31 ulong addr, new_ptr;
32 void *ptr;
33
34 addr = ALIGN(gd->malloc_base + gd->malloc_ptr, bytes);
35 new_ptr = addr + bytes;
36 if (new_ptr > gd->malloc_limit)
37 return NULL;
38 ptr = map_sysmem(addr, bytes);
39 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
40 return ptr;
41}
42
Simon Glass94890462014-11-10 17:16:43 -070043#ifdef CONFIG_SYS_MALLOC_SIMPLE
44void *calloc(size_t nmemb, size_t elem_size)
45{
46 size_t size = nmemb * elem_size;
47 void *ptr;
48
49 ptr = malloc(size);
50 memset(ptr, '\0', size);
51
52 return ptr;
53}
54#endif