52162fe74eee3476a93b2d1acdfdcc5b6177706d
[coreboot.git] / payloads / libpayload / include / stdlib.h
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
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
33 #include <stddef.h>
34
35 /**
36  * @defgroup malloc Memory allocation functions
37  * @{
38  */
39 #if defined(CONFIG_DEBUG_MALLOC) && !defined(IN_MALLOC_C)
40 #define free(p) \
41         ({ \
42          extern void print_malloc_map(void); \
43          extern void free(void *); \
44          printf("free(%p) called from %s:%s:%d...\n", p, __FILE__, __func__, \
45                 __LINE__);\
46          printf("PRE free()\n"); \
47          print_malloc_map(); \
48          free(p); \
49          printf("POST free()\n"); \
50          print_malloc_map(); \
51          })
52 #define malloc(s) \
53         ({ \
54          extern void print_malloc_map(void); \
55          extern void *malloc(size_t); \
56          void *ptr; \
57          printf("malloc(%u) called from %s:%s:%d...\n", s, __FILE__, __func__, \
58                 __LINE__);\
59          printf("PRE malloc\n"); \
60          print_malloc_map(); \
61          ptr = malloc(s); \
62          printf("POST malloc (ptr = %p)\n", ptr); \
63          print_malloc_map(); \
64          ptr; \
65          })
66 #define calloc(n,s) \
67         ({ \
68          extern void print_malloc_map(void); \
69          extern void *calloc(size_t,size_t); \
70          void *ptr; \
71          printf("calloc(%u, %u) called from %s:%s:%d...\n", n, s, __FILE__, \
72                 __func__, __LINE__);\
73          printf("PRE calloc\n"); \
74          print_malloc_map(); \
75          ptr = calloc(n,s); \
76          printf("POST calloc (ptr = %p)\n", ptr); \
77          print_malloc_map(); \
78          ptr; \
79          })
80 #define realloc(p,s) \
81         ({ \
82          extern void print_malloc_map(void); \
83          extern void *realloc(void*,size_t); \
84          void *ptr; \
85          printf("realloc(%p, %u) called from %s:%s:%d...\n", p, s, __FILE__, \
86                 __func__, __LINE__);\
87          printf("PRE realloc\n"); \
88          print_malloc_map(); \
89          ptr = realloc(p,s); \
90          printf("POST realloc (ptr = %p)\n", ptr); \
91          print_malloc_map(); \
92          ptr; \
93          })
94 #define memalign(a,s) \
95         ({ \
96          extern void print_malloc_map(void); \
97          extern void *memalign(size_t, size_t); \
98          void *ptr; \
99          printf("memalign(%u, %u) called from %s:%s:%d...\n", a, s, __FILE__, \
100                 __func__, __LINE__);\
101          printf("PRE memalign\n"); \
102          print_malloc_map(); \
103          ptr = memalign(a,s); \
104          printf("POST realloc (ptr = %p)\n", ptr); \
105          print_malloc_map(); \
106          ptr; \
107          })
108 #else
109 void free(void *ptr);
110 void *malloc(size_t size);
111 void *calloc(size_t nmemb, size_t size);
112 void *realloc(void *ptr, size_t size);
113 void *memalign(size_t align, size_t size);
114 #endif
115 /** @} */
116
117 /**
118  * @defgroup stdlib String conversion functions
119  * @{
120  */
121 long int strtol(const char *s, char **nptr, int base);
122 unsigned long int strtoul(const char *s, char **nptr, int base);
123 long atol(const char *nptr);
124
125 /** @} */
126
127 /**
128  * @defgroup rand Random number generator functions
129  * @{
130  */
131 int rand_r(unsigned int *seed);
132 int rand(void);
133 void srand(unsigned int seed);
134 /** @} */
135
136 /**
137  * Stop execution and halt the processor (this function does not return).
138  */
139 void halt(void) __attribute__ ((noreturn));
140 void exit(int status) __attribute__ ((noreturn));
141 #define abort() halt()    /**< Alias for the halt() function */
142
143 /** @} */
144
145 void qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *));
146
147 #endif