1 /*-------------------------------------------------------------------------
2 stdlib.h - General utilities (ISO C 11 7.22)
4 Copyright (C) 1998, Sandeep Dutta . sandeep.dutta@usa.net
5 Copyright (c) 2016, Philipp Klaus Krause, pkk@spth.de
7 Modifications for PIC14 by
8 Copyright (C) 2019 Gonzalo Pérez de Olaguer Córdoba <salo@gpoc.es>
10 This library is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 2, or (at your option) any
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this library; see the file COPYING. If not, write to the
22 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
25 As a special exception, if you link this library with other files,
26 some of which are compiled with SDCC, to produce an executable,
27 this library does not by itself cause the resulting executable to
28 be covered by the GNU General Public License. This exception does
29 not however invalidate any other reasons why the executable file
30 might be covered by the GNU General Public License.
31 -------------------------------------------------------------------------*/
33 #ifndef __SDCC_STDLIB_H
34 #define __SDCC_STDLIB_H 1
36 #if !defined(__SDCC_mcs51) && !defined(__SDCC_ds390) && !defined(__SDCC_ds400) && !defined(__SDCC_hc08) && !defined(__SDCC_s08) && !defined(__SDCC_pic16) && !defined(__SDCC_pdk13) && !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15)
40 #ifndef __SIZE_T_DEFINED
41 #define __SIZE_T_DEFINED
42 typedef unsigned int size_t;
45 #ifndef __WCHAR_T_DEFINED
46 #define __WCHAR_T_DEFINED
47 typedef unsigned long int wchar_t;
51 #define NULL (void *)0
54 #define RAND_MAX 32767
58 /* Numeric conversion functions (ISO C11 7.22.1) */
59 extern float atof (const char *nptr
);
60 extern int atoi (const char *nptr
);
61 extern long int atol (const char *nptr
);
62 #ifdef __SDCC_LONGLONG
63 extern long long int atoll (const char *nptr
);
65 extern long int strtol(const char *nptr
, char **endptr
, int base
);
66 extern unsigned long int strtoul(const char *nptr
, char **endptr
, int base
);
69 extern void _uitoa(unsigned int, char*, unsigned char);
70 extern void _itoa(int, char*, unsigned char);
71 extern void _ultoa(unsigned long, char*, unsigned char);
72 extern void _ltoa(long, char*, unsigned char);
74 #if defined(__SDCC_pic14)
76 /* -------------------------------------------------------------------------
77 * Non-standard C-like format specification for _ftoa, to be combined
78 * with the precision value:
80 * F format: [-]ddd.ddd rounded to 'prec' fractional digits (prec >= 0)
81 * E format: [-]d.ddde+-dd rounded to 'prec' fractional digits (prec >= 0)
82 * G format: use format E if exp < -4 or exp >= prec; format F otherwise
83 * -------------------------------------------------------------------------*/
85 #define PREC_F 0x20 /* F format */
86 #define PREC_E 0x40 /* E format */
87 #define PREC_G 0x00 /* G format */
88 #define PREC_P 0x1f /* precision value (0..31) */
89 #define PREC_D 0x06 /* default precision when none specified */
91 #define PREC(f,p) ((f)|(p)) /* Combine format and precision */
93 extern void _ftoa(float, char*, unsigned char);
95 #endif /* __SDCC_pic14 */
97 /* Pseudo-random sequence generation functions (ISO C11 7.22.2) */
99 void srand(unsigned int seed
);
101 /* Memory management functions (ISO C11 7.22.3) */
102 #if defined(__SDCC_pic14)
104 #define calloc(nmemb,size) _calloc((size_t)(nmemb) * (size_t)(size))
105 void __data
*_calloc (size_t size
);
106 void __data
*malloc (size_t size
);
107 void __data
*realloc (void __data
*ptr
, size_t size
);
108 #if __STDC_VERSION__ >= 201112L
109 #if 0 // Won't compile on pic14 TODO: Fix this!
110 inline void *aligned_alloc(size_t alignment
, size_t size
)
117 void free (void __data
* ptr
);
118 size_t memfree (void);
119 size_t memfreemax (void);
121 #else /* __SDCC_pic14 */
123 #if defined(__SDCC_mcs51) || defined(__SDCC_ds390) || defined(__SDCC_ds400)
124 void __xdata
*calloc (size_t nmemb
, size_t size
);
125 void __xdata
*malloc (size_t size
);
126 void __xdata
*realloc (void *ptr
, size_t size
);
128 void *calloc (size_t nmemb
, size_t size
);
129 void *malloc (size_t size
);
130 void *realloc (void *ptr
, size_t size
);
132 #if __STDC_VERSION__ >= 201112L
133 inline void *aligned_alloc(size_t alignment
, size_t size
)
139 extern void free (void * ptr
);
141 #endif /* __SDCC_pic14 */
143 /* Searching and sorting utilities (ISO C11 7.22.5) */
144 extern void *bsearch(const void *key
, const void *base
, size_t nmemb
, size_t size
, int (*compar
)(const void *, const void *) __reentrant
);
145 extern void qsort(void *base
, size_t nmemb
, size_t size
, int (*compar
)(const void *, const void *) __reentrant
);
147 /* Integer arithmetic functions (ISO C11 7.22.6) */
148 #if defined(__SDCC_z80) || defined(__SDCC_z180) || defined(__SDCC_r2k) || defined(__SDCC_r3ka) || defined(__SDCC_tlcs90) || defined (__SDCC_ez80_z80)
149 int abs(int j
) __preserves_regs(b
, c
, iyl
, iyh
);
153 long int labs(long int j
);
155 /* C99 Multibyte/wide character conversion functions (ISO C11 7.22.7) */
156 #if __STDC_VERSION__ >= 199901L
157 int mblen(const char *s
, size_t n
);
158 int mbtowc(wchar_t *restrict pwc
, const char *restrict s
, size_t n
);
159 int wctomb(char *s
, wchar_t wc
);
162 /* C99 Multibyte/wide string conversion functions (ISO C 11 7.22.8) */
163 #if __STDC_VERSION__ >= 199901L
164 size_t mbstowcs(wchar_t *restrict pwcs
, const char *restrict s
, size_t n
);
165 size_t wcstombs(char *restrict s
, const wchar_t *restrict pwcs
, size_t n
);
168 /* Bounds-checking interfaces from annex K of the C11 standard. */
169 #if defined (__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__
171 #ifndef __RSIZE_T_DEFINED
172 #define __RSIZE_T_DEFINED
173 typedef size_t rsize_t
;
176 #ifndef __ERRNO_T_DEFINED
177 #define __ERRNO_T_DEFINED
181 typedef void (*constraint_handler_t
)(const char *restrict msg
, void *restrict ptr
, errno_t error
);