blob: cb9addfbde42d24d9420c3973df23fc0b710d00c [file] [log] [blame]
Patrick Georgi980a69b2010-06-24 11:16:10 +00001/*
Patrick Georgi980a69b2010-06-24 11:16:10 +00002 *
3 * Copyright (C) 2008 Advanced Micro Devices, Inc.
Gabe Blackfa738752013-11-23 00:54:40 -08004 * Copyright 2013 Google Inc.
Patrick Georgi980a69b2010-06-24 11:16:10 +00005 *
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 Blackfa738752013-11-23 00:54:40 -080033#include <die.h>
Patrick Georgi980a69b2010-06-24 11:16:10 +000034#include <stddef.h>
Gabe Blackfa738752013-11-23 00:54:40 -080035#include <string.h>
Patrick Georgi980a69b2010-06-24 11:16:10 +000036
37/**
38 * @defgroup malloc Memory allocation functions
39 * @{
40 */
Patrick Georgi980a69b2010-06-24 11:16:10 +000041void free(void *ptr);
42void *malloc(size_t size);
43void *calloc(size_t nmemb, size_t size);
44void *realloc(void *ptr, size_t size);
45void *memalign(size_t align, size_t size);
Julius Wernerb8fad3d2013-08-27 15:48:32 -070046void *dma_malloc(size_t size);
47void *dma_memalign(size_t align, size_t size);
Julius Wernerf5b76fe2016-05-19 13:15:16 -070048
49#if CONFIG(LP_DEBUG_MALLOC) && !defined(IN_MALLOC_C)
50#include <stdio.h>
51void 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 Werner9665d382013-09-13 18:21:46 -0700135#endif
Julius Wernerf5b76fe2016-05-19 13:15:16 -0700136
Julius Werner9665d382013-09-13 18:21:46 -0700137void init_dma_memory(void *start, u32 size);
Julius Werner509c37e2013-08-28 12:29:28 -0700138int dma_initialized(void);
Yu-Ping Wu30d8e722022-08-23 16:40:03 +0800139int dma_coherent(const void *ptr);
Yi Chou582c2a72023-10-16 11:34:52 +0800140void dma_allocator_range(void **start_out, size_t *size_out);
Gabe Blackfa738752013-11-23 00:54:40 -0800141
142static 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 Haouas53314622023-01-22 08:55:59 +0100152#define xmalloc(size) xmalloc_work((size), __FILE__, __func__, __LINE__)
Gabe Blackfa738752013-11-23 00:54:40 -0800153
154static 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 Haouas53314622023-01-22 08:55:59 +0100161#define xzalloc(size) xzalloc_work((size), __FILE__, __func__, __LINE__)
Aaron Durbinc88cca12015-01-22 08:53:02 -0600162
163static 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 Georgi980a69b2010-06-24 11:16:10 +0000176/** @} */
177
178/**
179 * @defgroup stdlib String conversion functions
180 * @{
181 */
182long int strtol(const char *s, char **nptr, int base);
Julius Werner0de2fa02019-03-27 14:47:09 -0700183long long int strtoll(const char *s, char **nptr, int base);
Patrick Georgi980a69b2010-06-24 11:16:10 +0000184unsigned long int strtoul(const char *s, char **nptr, int base);
Patrick Georgic643fdd2011-07-26 12:51:59 +0200185unsigned long long int strtoull(const char *s, char **nptr, int base);
Patrick Georgic3c827c2011-05-27 15:31:52 +0200186long atol(const char *nptr);
Patrick Georgi980a69b2010-06-24 11:16:10 +0000187
188/** @} */
189
190/**
191 * @defgroup rand Random number generator functions
192 * @{
193 */
194int rand_r(unsigned int *seed);
195int rand(void);
196void srand(unsigned int seed);
197/** @} */
198
Nico Huber358c84a2021-06-16 17:48:40 +0000199/**
200 * @defgroup misc Misc functions
201 * @{
202 */
203int abs(int j);
204long int labs(long int j);
205long long int llabs(long long int j);
206/** @} */
207
Julius Werner39c4bb02018-04-18 14:57:33 -0700208/* Enter remote GDB mode. Will initialize connection if not already up. */
209void gdb_enter(void);
210/* Disconnect existing GDB connection if one exists. */
211void gdb_exit(s8 exit_status);
212
Patrick Georgi980a69b2010-06-24 11:16:10 +0000213/**
214 * Stop execution and halt the processor (this function does not return).
215 */
Stefan Reinauer6a001132017-07-13 02:20:27 +0200216void halt(void) __attribute__((noreturn));
217void exit(int status) __attribute__((noreturn));
Patrick Georgi980a69b2010-06-24 11:16:10 +0000218#define abort() halt() /**< Alias for the halt() function */
Julius Wernereab2a292019-03-05 16:55:15 -0800219#if CONFIG(LP_REMOTEGDB)
Julius Werner50a81742014-05-15 11:57:38 -0700220/* Override abort()/halt() to trap into GDB if it is enabled. */
221#define halt() do { gdb_enter(); halt(); } while (0)
222#endif
Patrick Georgi980a69b2010-06-24 11:16:10 +0000223
Patrick Georgi537cacf2011-07-07 12:02:10 +0200224void qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *));
Patrick Georgidd3e6902011-07-12 15:50:54 +0200225char *getenv(const char*);
Vadim Bendeburye63990e2014-11-27 18:50:14 -0800226uint64_t __umoddi3(uint64_t num, uint64_t den);
227uint64_t __udivdi3(uint64_t num, uint64_t den);
228uint64_t __ashldi3(uint64_t num, unsigned shift);
229uint64_t __lshrdi3(uint64_t num, unsigned shift);
230
Patrick Georgi980a69b2010-06-24 11:16:10 +0000231#endif