Simon Glass | 9489046 | 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 | 65b905b | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 11 | #include <mapmem.h> |
Simon Glass | 9489046 | 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; |
Simon Glass | e8211b4 | 2016-03-06 19:27:55 -0700 | [diff] [blame] | 22 | debug("%s: size=%zx, ptr=%lx, limit=%lx: ", __func__, bytes, new_ptr, |
Simon Glass | 65ba412 | 2015-09-08 17:52:46 -0600 | [diff] [blame] | 23 | gd->malloc_limit); |
Simon Glass | e8211b4 | 2016-03-06 19:27:55 -0700 | [diff] [blame] | 24 | if (new_ptr > gd->malloc_limit) { |
| 25 | debug("space exhausted\n"); |
Hans de Goede | 8b4e728 | 2015-02-04 13:05:50 +0100 | [diff] [blame] | 26 | return NULL; |
Simon Glass | e8211b4 | 2016-03-06 19:27:55 -0700 | [diff] [blame] | 27 | } |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 28 | ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes); |
| 29 | gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr)); |
Simon Glass | e8211b4 | 2016-03-06 19:27:55 -0700 | [diff] [blame] | 30 | debug("%lx\n", (ulong)ptr); |
Simon Glass | 65ba412 | 2015-09-08 17:52:46 -0600 | [diff] [blame] | 31 | |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 32 | return ptr; |
| 33 | } |
| 34 | |
Simon Glass | de9d70f | 2015-05-12 14:55:06 -0600 | [diff] [blame] | 35 | void *memalign_simple(size_t align, size_t bytes) |
| 36 | { |
| 37 | ulong addr, new_ptr; |
| 38 | void *ptr; |
| 39 | |
Simon Glass | 57dbd91 | 2015-08-14 13:26:43 -0600 | [diff] [blame] | 40 | addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align); |
Philipp Rosenberger | 4dd5bdc | 2015-09-08 12:41:24 +0200 | [diff] [blame] | 41 | new_ptr = addr + bytes - gd->malloc_base; |
Simon Glass | de9d70f | 2015-05-12 14:55:06 -0600 | [diff] [blame] | 42 | if (new_ptr > gd->malloc_limit) |
| 43 | return NULL; |
| 44 | ptr = map_sysmem(addr, bytes); |
| 45 | gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr)); |
Simon Glass | 65ba412 | 2015-09-08 17:52:46 -0600 | [diff] [blame] | 46 | |
Simon Glass | de9d70f | 2015-05-12 14:55:06 -0600 | [diff] [blame] | 47 | return ptr; |
| 48 | } |
| 49 | |
Hans de Goede | 9f9df6f | 2015-09-13 14:45:15 +0200 | [diff] [blame] | 50 | #if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE) |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 51 | void *calloc(size_t nmemb, size_t elem_size) |
| 52 | { |
| 53 | size_t size = nmemb * elem_size; |
| 54 | void *ptr; |
| 55 | |
| 56 | ptr = malloc(size); |
| 57 | memset(ptr, '\0', size); |
| 58 | |
| 59 | return ptr; |
| 60 | } |
| 61 | #endif |