/ payloads / libpayload / include / stdlib.h
stdlib.h
  1  /*
  2   *
  3   * Copyright (C) 2008 Advanced Micro Devices, Inc.
  4   * Copyright 2013 Google 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 <commonlib/bsd/stdlib.h>
 34  #include <die.h>
 35  #include <stddef.h>
 36  #include <string.h>
 37  
 38  /**
 39   * @defgroup malloc Memory allocation functions
 40   * @{
 41   */
 42  void *realloc(void *ptr, size_t size);
 43  void *dma_malloc(size_t size);
 44  void *dma_memalign(size_t align, size_t size);
 45  
 46  #if CONFIG(LP_DEBUG_MALLOC) && !defined(IN_MALLOC_C)
 47  #include <stdio.h>
 48  void print_malloc_map(void);
 49  #define free(p)	({ \
 50  	void *__p = p; \
 51  	printf("free(%p) called from %s:%s:%d...\n", __p, __FILE__, __func__, \
 52  	       __LINE__);\
 53  	printf("PRE free()\n"); \
 54  	print_malloc_map(); \
 55  	free(__p); \
 56  	printf("POST free()\n"); \
 57  	print_malloc_map(); \
 58  })
 59  #define malloc(s) ({ \
 60  	size_t __s = s; \
 61  	void *ptr; \
 62  	printf("malloc(%zu) called from %s:%s:%d...\n", __s, __FILE__, \
 63  	       __func__, __LINE__);\
 64  	printf("PRE malloc\n"); \
 65  	print_malloc_map(); \
 66  	ptr = malloc(__s); \
 67  	printf("POST malloc (ptr = %p)\n", ptr); \
 68  	print_malloc_map(); \
 69  	ptr; \
 70  })
 71  #define calloc(n, s) ({ \
 72  	size_t __n = n, __s = s; \
 73  	void *ptr; \
 74  	printf("calloc(%zu, %zu) called from %s:%s:%d...\n", __n, __s, \
 75  	       __FILE__, __func__, __LINE__);\
 76  	printf("PRE calloc\n"); \
 77  	print_malloc_map(); \
 78  	ptr = calloc(__n, __s); \
 79  	printf("POST calloc (ptr = %p)\n", ptr); \
 80  	print_malloc_map(); \
 81  	ptr; \
 82  })
 83  #define realloc(p, s) ({ \
 84  	void *__p = p; \
 85  	size_t __s = s; \
 86  	void *ptr; \
 87  	printf("realloc(%p, %zu) called from %s:%s:%d...\n", __p, __s, \
 88  	       __FILE__, __func__, __LINE__);\
 89  	printf("PRE realloc\n"); \
 90  	print_malloc_map(); \
 91  	ptr = realloc(__p, __s); \
 92  	printf("POST realloc (ptr = %p)\n", ptr); \
 93  	print_malloc_map(); \
 94  	ptr; \
 95  })
 96  #define memalign(a, s) ({ \
 97  	size_t __a = a, __s = s; \
 98  	void *ptr; \
 99  	printf("memalign(%zu, %zu) called from %s:%s:%d...\n", __a, __s, \
100  		__FILE__, __func__, __LINE__);\
101  	printf("PRE memalign\n"); \
102  	print_malloc_map(); \
103  	ptr = memalign(__a, __s); \
104  	printf("POST memalign (ptr = %p)\n", ptr); \
105  	print_malloc_map(); \
106  	ptr; \
107  })
108  #define dma_malloc(s) ({ \
109  	size_t __s = s; \
110  	void *ptr; \
111  	printf("dma_malloc(%zu) called from %s:%s:%d...\n", __s, __FILE__, \
112  	       __func__, __LINE__);\
113  	printf("PRE dma_malloc\n"); \
114  	print_malloc_map(); \
115  	ptr = dma_malloc(__s); \
116  	printf("POST dma_malloc (ptr = %p)\n", ptr); \
117  	print_malloc_map(); \
118  	ptr; \
119  })
120  #define dma_memalign(a, s) ({ \
121  	size_t __a = a, __s = s; \
122  	void *ptr; \
123  	printf("dma_memalign(%zu, %zu) called from %s:%s:%d...\n", __a, __s, \
124  	       __FILE__, __func__, __LINE__);\
125  	printf("PRE dma_memalign\n"); \
126  	print_malloc_map(); \
127  	ptr = dma_memalign(__a, __s); \
128  	printf("POST dma_memalign (ptr = %p)\n", ptr); \
129  	print_malloc_map(); \
130  	ptr; \
131  })
132  #endif
133  
134  void init_dma_memory(void *start, u32 size);
135  int dma_initialized(void);
136  void dma_allocator_range(void **start_out, size_t *size_out);
137  
138  /** @} */
139  
140  /**
141   * @defgroup stdlib String conversion functions
142   * @{
143   */
144  long int strtol(const char *s, char **nptr, int base);
145  long long int strtoll(const char *s, char **nptr, int base);
146  unsigned long int strtoul(const char *s, char **nptr, int base);
147  unsigned long long int strtoull(const char *s, char **nptr, int base);
148  long atol(const char *nptr);
149  
150  /** @} */
151  
152  /**
153   * @defgroup rand Random number generator functions
154   * @{
155   */
156  int rand_r(unsigned int *seed);
157  int rand(void);
158  void srand(unsigned int seed);
159  /** @} */
160  
161  /**
162   * @defgroup misc Misc functions
163   * @{
164   */
165  int abs(int j);
166  long int labs(long int j);
167  long long int llabs(long long int j);
168  /** @} */
169  
170  void qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *));
171  char *getenv(const char*);
172  uint64_t __umoddi3(uint64_t num, uint64_t den);
173  uint64_t  __udivdi3(uint64_t num, uint64_t den);
174  uint64_t __ashldi3(uint64_t num, unsigned shift);
175  uint64_t __lshrdi3(uint64_t num, unsigned shift);
176  
177  void __noreturn exit(int status);
178  
179  #endif