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> |
| 11 | #include <asm/io.h> |
| 12 | |
| 13 | DECLARE_GLOBAL_DATA_PTR; |
| 14 | |
| 15 | void *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) |
| 22 | panic("Out of pre-reloc memory"); |
| 23 | 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 |
| 29 | void *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 |