soc/intel/cmn/cse: Deprecate CONFIG_SOC_INTEL_CSE_RW_VERSION
[coreboot2.git] / payloads / libpayload / include / stdlib.h
blobcb9addfbde42d24d9420c3973df23fc0b710d00c
1 /*
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
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.
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.
30 #ifndef _STDLIB_H
31 #define _STDLIB_H
33 #include <die.h>
34 #include <stddef.h>
35 #include <string.h>
37 /**
38 * @defgroup malloc Memory allocation functions
39 * @{
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);
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)
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(); \
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; \
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; \
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; \
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; \
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; \
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; \
135 #endif
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);
146 if (!ret && size) {
147 die_work(file, func, line, "Failed to malloc %zu bytes.\n",
148 size);
150 return ret;
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);
159 return ret;
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);
167 if (!ret && size) {
168 die_work(file, func, line,
169 "Failed to memalign %zu bytes with %zu alignment.\n",
170 size, align);
172 return ret;
174 #define xmemalign(align, size) \
175 xmemalign_work((align), (size), __FILE__, __func__, __LINE__)
176 /** @} */
179 * @defgroup stdlib String conversion functions
180 * @{
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);
188 /** @} */
191 * @defgroup rand Random number generator functions
192 * @{
194 int rand_r(unsigned int *seed);
195 int rand(void);
196 void srand(unsigned int seed);
197 /** @} */
200 * @defgroup misc Misc functions
201 * @{
203 int abs(int j);
204 long int labs(long int j);
205 long long int llabs(long long int j);
206 /** @} */
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)
222 #endif
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);
231 #endif