Patrick Georgi | 980a69b | 2010-06-24 11:16:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright (C) 2008 Advanced Micro Devices, Inc. |
Gabe Black | fa73875 | 2013-11-23 00:54:40 -0800 | [diff] [blame] | 5 | * Copyright 2013 Google Inc. |
Patrick Georgi | 980a69b | 2010-06-24 11:16:10 +0000 | [diff] [blame] | 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #ifndef _STDLIB_H |
| 32 | #define _STDLIB_H |
| 33 | |
Gabe Black | fa73875 | 2013-11-23 00:54:40 -0800 | [diff] [blame] | 34 | #include <die.h> |
Patrick Georgi | 980a69b | 2010-06-24 11:16:10 +0000 | [diff] [blame] | 35 | #include <stddef.h> |
Gabe Black | fa73875 | 2013-11-23 00:54:40 -0800 | [diff] [blame] | 36 | #include <string.h> |
Patrick Georgi | 980a69b | 2010-06-24 11:16:10 +0000 | [diff] [blame] | 37 | |
Daisuke Nojiri | 0341169 | 2014-03-05 15:46:28 -0800 | [diff] [blame] | 38 | #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1UL) |
| 39 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) |
| 40 | #define ALIGN_UP(x,a) ALIGN((x),(a)) |
| 41 | #define ALIGN_DOWN(x,a) ((x) & ~((typeof(x))(a)-1UL)) |
Jimmy Huang | 0fd3e79 | 2015-04-13 20:28:38 +0800 | [diff] [blame] | 42 | #define IS_ALIGNED(x,a) (((x) & ((typeof(x))(a)-1UL)) == 0) |
Daisuke Nojiri | 0341169 | 2014-03-05 15:46:28 -0800 | [diff] [blame] | 43 | |
Patrick Georgi | 980a69b | 2010-06-24 11:16:10 +0000 | [diff] [blame] | 44 | /** |
| 45 | * @defgroup malloc Memory allocation functions |
| 46 | * @{ |
| 47 | */ |
Stefan Reinauer | 1b4d394 | 2015-06-29 15:47:34 -0700 | [diff] [blame] | 48 | #if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC) && !defined(IN_MALLOC_C) |
Patrick Georgi | 980a69b | 2010-06-24 11:16:10 +0000 | [diff] [blame] | 49 | #define free(p) \ |
| 50 | ({ \ |
| 51 | extern void print_malloc_map(void); \ |
| 52 | extern void free(void *); \ |
| 53 | printf("free(%p) called from %s:%s:%d...\n", p, __FILE__, __func__, \ |
| 54 | __LINE__);\ |
| 55 | printf("PRE free()\n"); \ |
| 56 | print_malloc_map(); \ |
| 57 | free(p); \ |
| 58 | printf("POST free()\n"); \ |
| 59 | print_malloc_map(); \ |
| 60 | }) |
| 61 | #define malloc(s) \ |
| 62 | ({ \ |
| 63 | extern void print_malloc_map(void); \ |
| 64 | extern void *malloc(size_t); \ |
| 65 | void *ptr; \ |
| 66 | printf("malloc(%u) called from %s:%s:%d...\n", s, __FILE__, __func__, \ |
| 67 | __LINE__);\ |
| 68 | printf("PRE malloc\n"); \ |
| 69 | print_malloc_map(); \ |
| 70 | ptr = malloc(s); \ |
| 71 | printf("POST malloc (ptr = %p)\n", ptr); \ |
| 72 | print_malloc_map(); \ |
| 73 | ptr; \ |
| 74 | }) |
| 75 | #define calloc(n,s) \ |
| 76 | ({ \ |
| 77 | extern void print_malloc_map(void); \ |
| 78 | extern void *calloc(size_t,size_t); \ |
| 79 | void *ptr; \ |
| 80 | printf("calloc(%u, %u) called from %s:%s:%d...\n", n, s, __FILE__, \ |
| 81 | __func__, __LINE__);\ |
| 82 | printf("PRE calloc\n"); \ |
| 83 | print_malloc_map(); \ |
| 84 | ptr = calloc(n,s); \ |
| 85 | printf("POST calloc (ptr = %p)\n", ptr); \ |
| 86 | print_malloc_map(); \ |
| 87 | ptr; \ |
| 88 | }) |
| 89 | #define realloc(p,s) \ |
| 90 | ({ \ |
| 91 | extern void print_malloc_map(void); \ |
| 92 | extern void *realloc(void*,size_t); \ |
| 93 | void *ptr; \ |
| 94 | printf("realloc(%p, %u) called from %s:%s:%d...\n", p, s, __FILE__, \ |
| 95 | __func__, __LINE__);\ |
| 96 | printf("PRE realloc\n"); \ |
| 97 | print_malloc_map(); \ |
| 98 | ptr = realloc(p,s); \ |
| 99 | printf("POST realloc (ptr = %p)\n", ptr); \ |
| 100 | print_malloc_map(); \ |
| 101 | ptr; \ |
| 102 | }) |
| 103 | #define memalign(a,s) \ |
| 104 | ({ \ |
| 105 | extern void print_malloc_map(void); \ |
| 106 | extern void *memalign(size_t, size_t); \ |
| 107 | void *ptr; \ |
| 108 | printf("memalign(%u, %u) called from %s:%s:%d...\n", a, s, __FILE__, \ |
| 109 | __func__, __LINE__);\ |
| 110 | printf("PRE memalign\n"); \ |
| 111 | print_malloc_map(); \ |
| 112 | ptr = memalign(a,s); \ |
Julius Werner | 9665d38 | 2013-09-13 18:21:46 -0700 | [diff] [blame] | 113 | printf("POST memalign (ptr = %p)\n", ptr); \ |
| 114 | print_malloc_map(); \ |
| 115 | ptr; \ |
| 116 | }) |
| 117 | #define dma_malloc(s) \ |
| 118 | ({ \ |
| 119 | extern void print_malloc_map(void); \ |
| 120 | extern void *dma_malloc(size_t); \ |
| 121 | void *ptr; \ |
| 122 | printf("dma_malloc(%u) called from %s:%s:%d...\n", s, __FILE__, \ |
| 123 | __func__, __LINE__);\ |
| 124 | printf("PRE dma_malloc\n"); \ |
| 125 | print_malloc_map(); \ |
| 126 | ptr = dma_malloc(s); \ |
| 127 | printf("POST dma_malloc (ptr = %p)\n", ptr); \ |
| 128 | print_malloc_map(); \ |
| 129 | ptr; \ |
| 130 | }) |
| 131 | #define dma_memalign(a,s) \ |
| 132 | ({ \ |
| 133 | extern void print_malloc_map(void); \ |
| 134 | extern void *dma_memalign(size_t, size_t); \ |
| 135 | void *ptr; \ |
| 136 | printf("dma_memalign(%u, %u) called from %s:%s:%d...\n", a, s, \ |
| 137 | __FILE__, __func__, __LINE__);\ |
| 138 | printf("PRE dma_memalign\n"); \ |
| 139 | print_malloc_map(); \ |
| 140 | ptr = dma_memalign(a,s); \ |
| 141 | printf("POST dma_memalign (ptr = %p)\n", ptr); \ |
Patrick Georgi | 980a69b | 2010-06-24 11:16:10 +0000 | [diff] [blame] | 142 | print_malloc_map(); \ |
| 143 | ptr; \ |
| 144 | }) |
| 145 | #else |
| 146 | void free(void *ptr); |
| 147 | void *malloc(size_t size); |
| 148 | void *calloc(size_t nmemb, size_t size); |
| 149 | void *realloc(void *ptr, size_t size); |
| 150 | void *memalign(size_t align, size_t size); |
Julius Werner | b8fad3d | 2013-08-27 15:48:32 -0700 | [diff] [blame] | 151 | void *dma_malloc(size_t size); |
| 152 | void *dma_memalign(size_t align, size_t size); |
Julius Werner | 9665d38 | 2013-09-13 18:21:46 -0700 | [diff] [blame] | 153 | #endif |
| 154 | void init_dma_memory(void *start, u32 size); |
Julius Werner | 509c37e | 2013-08-28 12:29:28 -0700 | [diff] [blame] | 155 | int dma_initialized(void); |
| 156 | int dma_coherent(void *ptr); |
Gabe Black | fa73875 | 2013-11-23 00:54:40 -0800 | [diff] [blame] | 157 | |
| 158 | static inline void *xmalloc_work(size_t size, const char *file, |
| 159 | const char *func, int line) |
| 160 | { |
| 161 | void *ret = malloc(size); |
| 162 | if (!ret && size) { |
| 163 | die_work(file, func, line, "Failed to malloc %zu bytes.\n", |
| 164 | size); |
| 165 | } |
| 166 | return ret; |
| 167 | } |
| 168 | #define xmalloc(size) xmalloc_work((size), __FILE__, __FUNCTION__, __LINE__) |
| 169 | |
| 170 | static inline void *xzalloc_work(size_t size, const char *file, |
| 171 | const char *func, int line) |
| 172 | { |
| 173 | void *ret = xmalloc_work(size, file, func, line); |
| 174 | memset(ret, 0, size); |
| 175 | return ret; |
| 176 | } |
| 177 | #define xzalloc(size) xzalloc_work((size), __FILE__, __FUNCTION__, __LINE__) |
Aaron Durbin | c88cca1 | 2015-01-22 08:53:02 -0600 | [diff] [blame] | 178 | |
| 179 | static inline void *xmemalign_work(size_t align, size_t size, const char *file, |
| 180 | const char *func, int line) |
| 181 | { |
| 182 | void *ret = memalign(align, size); |
| 183 | if (!ret && size) { |
| 184 | die_work(file, func, line, |
| 185 | "Failed to memalign %zu bytes with %zu alignment.\n", |
| 186 | size, align); |
| 187 | } |
| 188 | return ret; |
| 189 | } |
| 190 | #define xmemalign(align, size) \ |
| 191 | xmemalign_work((align), (size), __FILE__, __func__, __LINE__) |
Patrick Georgi | 980a69b | 2010-06-24 11:16:10 +0000 | [diff] [blame] | 192 | /** @} */ |
| 193 | |
| 194 | /** |
| 195 | * @defgroup stdlib String conversion functions |
| 196 | * @{ |
| 197 | */ |
| 198 | long int strtol(const char *s, char **nptr, int base); |
| 199 | unsigned long int strtoul(const char *s, char **nptr, int base); |
Patrick Georgi | c643fdd | 2011-07-26 12:51:59 +0200 | [diff] [blame] | 200 | unsigned long long int strtoull(const char *s, char **nptr, int base); |
Patrick Georgi | c3c827c | 2011-05-27 15:31:52 +0200 | [diff] [blame] | 201 | long atol(const char *nptr); |
Patrick Georgi | 980a69b | 2010-06-24 11:16:10 +0000 | [diff] [blame] | 202 | |
| 203 | /** @} */ |
| 204 | |
| 205 | /** |
| 206 | * @defgroup rand Random number generator functions |
| 207 | * @{ |
| 208 | */ |
| 209 | int rand_r(unsigned int *seed); |
| 210 | int rand(void); |
| 211 | void srand(unsigned int seed); |
| 212 | /** @} */ |
| 213 | |
| 214 | /** |
| 215 | * Stop execution and halt the processor (this function does not return). |
| 216 | */ |
| 217 | void halt(void) __attribute__ ((noreturn)); |
| 218 | void exit(int status) __attribute__ ((noreturn)); |
| 219 | #define abort() halt() /**< Alias for the halt() function */ |
Stefan Reinauer | 1b4d394 | 2015-06-29 15:47:34 -0700 | [diff] [blame] | 220 | #if IS_ENABLED(CONFIG_LP_REMOTEGDB) |
Julius Werner | 50a8174 | 2014-05-15 11:57:38 -0700 | [diff] [blame] | 221 | /* Override abort()/halt() to trap into GDB if it is enabled. */ |
| 222 | #define halt() do { gdb_enter(); halt(); } while (0) |
| 223 | #endif |
Patrick Georgi | 980a69b | 2010-06-24 11:16:10 +0000 | [diff] [blame] | 224 | |
| 225 | /** @} */ |
| 226 | |
Patrick Georgi | 537cacf | 2011-07-07 12:02:10 +0200 | [diff] [blame] | 227 | void qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *)); |
Patrick Georgi | dd3e690 | 2011-07-12 15:50:54 +0200 | [diff] [blame] | 228 | char *getenv(const char*); |
Vadim Bendebury | e63990e | 2014-11-27 18:50:14 -0800 | [diff] [blame] | 229 | uint64_t __umoddi3(uint64_t num, uint64_t den); |
| 230 | uint64_t __udivdi3(uint64_t num, uint64_t den); |
| 231 | uint64_t __ashldi3(uint64_t num, unsigned shift); |
| 232 | uint64_t __lshrdi3(uint64_t num, unsigned shift); |
| 233 | |
Patrick Georgi | 980a69b | 2010-06-24 11:16:10 +0000 | [diff] [blame] | 234 | #endif |