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