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