3 * Copyright (C) 2008 Advanced Micro Devices, Inc.
4 * Copyright 2013 Google Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
38 * @defgroup malloc Memory allocation functions
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
);
46 void *dma_malloc(size_t size
);
47 void *dma_memalign(size_t align
, size_t size
);
49 #if CONFIG(LP_DEBUG_MALLOC) && !defined(IN_MALLOC_C)
51 void print_malloc_map(void);
54 printf("free(%p) called from %s:%s:%d...\n", __p, __FILE__, __func__, \
56 printf("PRE free()\n"); \
59 printf("POST free()\n"); \
62 #define malloc(s) ({ \
65 printf("malloc(%zu) called from %s:%s:%d...\n", __s, __FILE__, \
67 printf("PRE malloc\n"); \
70 printf("POST malloc (ptr = %p)\n", ptr); \
74 #define calloc(n, s) ({ \
75 size_t __n = n, __s = s; \
77 printf("calloc(%zu, %zu) called from %s:%s:%d...\n", __n, __s, \
78 __FILE__, __func__, __LINE__);\
79 printf("PRE calloc\n"); \
81 ptr = calloc(__n, __s); \
82 printf("POST calloc (ptr = %p)\n", ptr); \
86 #define realloc(p, s) ({ \
90 printf("realloc(%p, %zu) called from %s:%s:%d...\n", __p, __s, \
91 __FILE__, __func__, __LINE__);\
92 printf("PRE realloc\n"); \
94 ptr = realloc(__p, __s); \
95 printf("POST realloc (ptr = %p)\n", ptr); \
99 #define memalign(a, s) ({ \
100 size_t __a = a, __s = s; \
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(); \
111 #define dma_malloc(s) ({ \
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(); \
123 #define dma_memalign(a, s) ({ \
124 size_t __a = a, __s = s; \
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(); \
137 void init_dma_memory(void *start
, u32 size
);
138 int dma_initialized(void);
139 int dma_coherent(const void *ptr
);
140 void dma_allocator_range(void **start_out
, size_t *size_out
);
142 static inline void *xmalloc_work(size_t size
, const char *file
,
143 const char *func
, int line
)
145 void *ret
= malloc(size
);
147 die_work(file
, func
, line
, "Failed to malloc %zu bytes.\n",
152 #define xmalloc(size) xmalloc_work((size), __FILE__, __func__, __LINE__)
154 static inline void *xzalloc_work(size_t size
, const char *file
,
155 const char *func
, int line
)
157 void *ret
= xmalloc_work(size
, file
, func
, line
);
158 memset(ret
, 0, size
);
161 #define xzalloc(size) xzalloc_work((size), __FILE__, __func__, __LINE__)
163 static inline void *xmemalign_work(size_t align
, size_t size
, const char *file
,
164 const char *func
, int line
)
166 void *ret
= memalign(align
, size
);
168 die_work(file
, func
, line
,
169 "Failed to memalign %zu bytes with %zu alignment.\n",
174 #define xmemalign(align, size) \
175 xmemalign_work((align), (size), __FILE__, __func__, __LINE__)
179 * @defgroup stdlib String conversion functions
182 long int strtol(const char *s
, char **nptr
, int base
);
183 long long int strtoll(const char *s
, char **nptr
, int base
);
184 unsigned long int strtoul(const char *s
, char **nptr
, int base
);
185 unsigned long long int strtoull(const char *s
, char **nptr
, int base
);
186 long atol(const char *nptr
);
191 * @defgroup rand Random number generator functions
194 int rand_r(unsigned int *seed
);
196 void srand(unsigned int seed
);
200 * @defgroup misc Misc functions
204 long int labs(long int j
);
205 long long int llabs(long long int j
);
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
);
214 * Stop execution and halt the processor (this function does not return).
216 void halt(void) __attribute__((noreturn
));
217 void exit(int status
) __attribute__((noreturn
));
218 #define abort() halt() /**< Alias for the halt() function */
219 #if CONFIG(LP_REMOTEGDB)
220 /* Override abort()/halt() to trap into GDB if it is enabled. */
221 #define halt() do { gdb_enter(); halt(); } while (0)
224 void qsort(void *aa
, size_t n
, size_t es
, int (*cmp
)(const void *, const void *));
225 char *getenv(const char*);
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
);