Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 1 | /* |
| 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 Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 11 | #include <mapmem.h> |
Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 12 | #include <asm/io.h> |
| 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
| 16 | void *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 Goede | 2c85717 | 2015-02-04 13:05:50 +0100 | [diff] [blame] | 23 | return NULL; |
Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 24 | 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 | |
| 29 | #ifdef CONFIG_SYS_MALLOC_SIMPLE |
| 30 | void *calloc(size_t nmemb, size_t elem_size) |
| 31 | { |
| 32 | size_t size = nmemb * elem_size; |
| 33 | void *ptr; |
| 34 | |
| 35 | ptr = malloc(size); |
| 36 | memset(ptr, '\0', size); |
| 37 | |
| 38 | return ptr; |
| 39 | } |
| 40 | #endif |