blob: 64ae0365afc52f055621e2c2e6e18e9c095dc9ed [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>
11#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;
21 if (new_ptr > gd->malloc_limit)
Hans de Goede2c857172015-02-04 13:05:50 +010022 return NULL;
Simon Glassc9356be2014-11-10 17:16:43 -070023 ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes);
24 gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
25 return ptr;
26}
27
28#ifdef CONFIG_SYS_MALLOC_SIMPLE
29void *calloc(size_t nmemb, size_t elem_size)
30{
31 size_t size = nmemb * elem_size;
32 void *ptr;
33
34 ptr = malloc(size);
35 memset(ptr, '\0', size);
36
37 return ptr;
38}
39#endif