rbtree: add rb_search_exact()
[nasm.git] / include / nasmlib.h
blobe9bfbccfed5096d5387c6fe3fa59a94ed218da9a
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2020 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * nasmlib.h header file for nasmlib.c
38 #ifndef NASM_NASMLIB_H
39 #define NASM_NASMLIB_H
41 #include "compiler.h"
42 #include "bytesex.h"
45 * Useful construct for private values
47 union intorptr {
48 int64_t i;
49 uint64_t u;
50 size_t s;
51 void *p;
52 const void *cp;
53 uintptr_t up;
55 typedef union intorptr intorptr;
58 * Wrappers around malloc, realloc, free and a few more. nasm_malloc
59 * will fatal-error and die rather than return NULL; nasm_realloc will
60 * do likewise, and will also guarantee to work right on being passed
61 * a NULL pointer; nasm_free will do nothing if it is passed a NULL
62 * pointer.
64 void * safe_malloc(1) nasm_malloc(size_t);
65 void * safe_malloc(1) nasm_zalloc(size_t);
66 void * safe_malloc2(1,2) nasm_calloc(size_t, size_t);
67 void * safe_realloc(2) nasm_realloc(void *, size_t);
68 void nasm_free(void *);
69 char * safe_alloc nasm_strdup(const char *);
70 char * safe_alloc nasm_strndup(const char *, size_t);
71 char * safe_alloc nasm_strcat(const char *one, const char *two);
72 char * safe_alloc end_with_null nasm_strcatn(const char *one, ...);
75 * nasm_[v]asprintf() are variants of the semi-standard [v]asprintf()
76 * functions, except that we return the pointer instead of a count.
77 * The size of the string (including the final NUL!) is available
78 * by calling nasm_aprintf_size() afterwards.
80 * nasm_[v]axprintf() are similar, but allocates a user-defined amount
81 * of storage before the string, and returns a pointer to the
82 * allocated buffer. The value of nasm_aprintf_size() does *not* include
83 * this additional storage.
85 char * safe_alloc printf_func(1, 2) nasm_asprintf(const char *fmt, ...);
86 char * safe_alloc nasm_vasprintf(const char *fmt, va_list ap);
87 void * safe_alloc printf_func(2, 3) nasm_axprintf(size_t extra, const char *fmt, ...);
88 void * safe_alloc nasm_vaxprintf(size_t extra, const char *fmt, va_list ap);
91 * nasm_last_string_len() returns the length of the last string allocated
92 * by [v]asprintf, nasm_strdup, nasm_strcat, or nasm_strcatn.
94 * nasm_last_string_size() returns the equivalent size including the
95 * final NUL.
97 static inline size_t nasm_last_string_len(void)
99 extern size_t _nasm_last_string_size;
100 return _nasm_last_string_size - 1;
102 static inline size_t nasm_last_string_size(void)
104 extern size_t _nasm_last_string_size;
105 return _nasm_last_string_size;
108 /* Assert the argument is a pointer without evaluating it */
109 #define nasm_assert_pointer(p) ((void)sizeof(*(p)))
111 #define nasm_new(p) ((p) = nasm_zalloc(sizeof(*(p))))
112 #define nasm_newn(p,n) ((p) = nasm_calloc((n), sizeof(*(p))))
114 * This is broken on platforms where there are pointers which don't
115 * match void * in their internal layout. It unfortunately also
116 * loses any "const" part of the argument, although hopefully the
117 * compiler will warn in that case.
119 #define nasm_delete(p) \
120 do { \
121 void **_pp = (void **)&(p); \
122 nasm_assert_pointer(p); \
123 nasm_free(*_pp); \
124 *_pp = NULL; \
125 } while (0)
126 #define nasm_zero(x) (memset(&(x), 0, sizeof(x)))
127 #define nasm_zeron(p,n) (memset((p), 0, (n)*sizeof(*(p))))
130 * Wrappers around fread()/fwrite() which fatal-errors on failure.
131 * For fread(), only use this if EOF is supposed to be a fatal error!
133 void nasm_read(void *, size_t, FILE *);
134 void nasm_write(const void *, size_t, FILE *);
137 * NASM failure at build time if the argument is false
139 #ifdef static_assert
140 # define nasm_static_assert(x) static_assert((x), #x)
141 #elif defined(HAVE_FUNC_ATTRIBUTE_ERROR) && defined(__OPTIMIZE__)
142 # define nasm_static_assert(x) \
143 do { \
144 if (!(x)) { \
145 extern void __attribute__((error("assertion " #x " failed"))) \
146 _nasm_static_fail(void); \
147 _nasm_static_fail(); \
149 } while (0)
150 #else
151 /* See http://www.drdobbs.com/compile-time-assertions/184401873 */
152 # define nasm_static_assert(x) \
153 do { enum { _static_assert_failed = 1/(!!(x)) }; } while (0)
154 #endif
157 * conditional static assert, if we know it is possible to determine
158 * the assert value at compile time. Since if_constant triggers
159 * pedantic warnings on gcc, turn them off explicitly around this code.
161 #ifdef static_assert
162 # define nasm_try_static_assert(x) \
163 do { \
164 not_pedantic_start \
165 static_assert(if_constant(x, true), #x); \
166 not_pedantic_end \
167 } while (0)
168 #elif defined(HAVE_FUNC_ATTRIBUTE_ERROR) && defined(__OPTIMIZE__)
169 # define nasm_try_static_assert(x) \
170 do { \
171 if (!if_constant(x, true)) { \
172 extern void __attribute__((error("assertion " #x " failed"))) \
173 _nasm_static_fail(void); \
174 _nasm_static_fail(); \
176 } while (0)
177 #else
178 # define nasm_try_static_assert(x) ((void)0)
179 #endif
182 * NASM assert failure
184 fatal_func nasm_assert_failed(const char *, int, const char *);
185 #define nasm_assert(x) \
186 do { \
187 nasm_try_static_assert(x); \
188 if (unlikely(!(x))) \
189 nasm_assert_failed(__FILE__,__LINE__,#x); \
190 } while (0)
192 /* Utility function to generate a string for an invalid enum */
193 const char *invalid_enum_str(int);
196 * ANSI doesn't guarantee the presence of `stricmp' or
197 * `strcasecmp'.
199 #if defined(HAVE_STRCASECMP)
200 #define nasm_stricmp strcasecmp
201 #elif defined(HAVE_STRICMP)
202 #define nasm_stricmp stricmp
203 #else
204 int pure_func nasm_stricmp(const char *, const char *);
205 #endif
207 #if defined(HAVE_STRNCASECMP)
208 #define nasm_strnicmp strncasecmp
209 #elif defined(HAVE_STRNICMP)
210 #define nasm_strnicmp strnicmp
211 #else
212 int pure_func nasm_strnicmp(const char *, const char *, size_t);
213 #endif
215 int pure_func nasm_memicmp(const char *, const char *, size_t);
217 #if defined(HAVE_STRSEP)
218 #define nasm_strsep strsep
219 #else
220 char *nasm_strsep(char **stringp, const char *delim);
221 #endif
223 #ifndef HAVE_DECL_STRNLEN
224 size_t pure_func strnlen(const char *, size_t);
225 #endif
227 /* This returns the numeric value of a given 'digit'; no check for validity */
228 static inline unsigned int numvalue(unsigned char c)
230 c |= 0x20;
231 return c >= 'a' ? c - 'a' + 10 : c - '0';
235 * Convert a string into a number, using NASM number rules. Sets
236 * `*error' to true if an error occurs, and false otherwise.
238 int64_t readnum(const char *str, bool *error);
241 * Convert a character constant into a number. Sets
242 * `*warn' to true if an overflow occurs, and false otherwise.
243 * str points to and length covers the middle of the string,
244 * without the quotes.
246 int64_t readstrnum(char *str, int length, bool *warn);
249 * seg_alloc: allocate a hitherto unused segment number.
251 int32_t seg_alloc(void);
254 * Add/replace or remove an extension to the end of a filename
256 const char *filename_set_extension(const char *inname, const char *extension);
259 * Utility macros...
261 * This is a useful #define which I keep meaning to use more often:
262 * the number of elements of a statically defined array.
264 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
267 * List handling
269 * list_for_each - regular iterator over list
270 * list_for_each_safe - the same but safe against list items removal
271 * list_last - find the last element in a list
273 #define list_for_each(pos, head) \
274 for (pos = head; pos; pos = pos->next)
275 #define list_for_each_safe(pos, n, head) \
276 for (pos = head, n = (pos ? pos->next : NULL); pos; \
277 pos = n, n = (n ? n->next : NULL))
278 #define list_last(pos, head) \
279 for (pos = head; pos && pos->next; pos = pos->next) \
281 #define list_reverse(head, prev, next) \
282 do { \
283 if (!head || !head->next) \
284 break; \
285 prev = NULL; \
286 while (head) { \
287 next = head->next; \
288 head->next = prev; \
289 prev = head; \
290 head = next; \
292 head = prev; \
293 } while (0)
296 * Power of 2 align helpers
298 #undef ALIGN_MASK /* Some BSD flavors define these in system headers */
299 #undef ALIGN
300 #define ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
301 #define ALIGN(v, a) ALIGN_MASK(v, (a) - 1)
302 #define IS_ALIGNED(v, a) (((v) & ((a) - 1)) == 0)
305 * Routines to write littleendian data to a file
307 #define fwriteint8_t(d,f) putc(d,f)
308 void fwriteint16_t(uint16_t data, FILE * fp);
309 void fwriteint32_t(uint32_t data, FILE * fp);
310 void fwriteint64_t(uint64_t data, FILE * fp);
311 void fwriteaddr(uint64_t data, int size, FILE * fp);
314 * Binary search routine. Returns index into `array' of an entry
315 * matching `string', or <0 if no match. `array' is taken to
316 * contain `size' elements.
318 * bsi() is case sensitive, bsii() is case insensitive.
320 int bsi(const char *string, const char **array, int size);
321 int bsii(const char *string, const char **array, int size);
324 * Convenient string processing helper routines
326 char *nasm_skip_spaces(const char *p);
327 char *nasm_skip_word(const char *p);
328 char *nasm_zap_spaces_fwd(char *p);
329 char *nasm_zap_spaces_rev(char *p);
330 char *nasm_trim_spaces(char *p);
331 char *nasm_get_word(char *p, char **tail);
332 char *nasm_opt_val(char *p, char **opt, char **val);
335 * Converts a relative pathname rel_path into an absolute path name.
337 * The buffer returned must be freed by the caller
339 char * safe_alloc nasm_realpath(const char *rel_path);
342 * Path-splitting and merging functions
344 char * safe_alloc nasm_dirname(const char *path);
345 char * safe_alloc nasm_basename(const char *path);
346 char * safe_alloc nasm_catfile(const char *dir, const char *path);
348 const char * pure_func prefix_name(int);
351 * Wrappers around fopen()... for future change to a dedicated structure
353 enum file_flags {
354 NF_BINARY = 0x00000000, /* Binary file (default) */
355 NF_TEXT = 0x00000001, /* Text file */
356 NF_NONFATAL = 0x00000000, /* Don't die on open failure (default) */
357 NF_FATAL = 0x00000002, /* Die on open failure */
358 NF_FORMAP = 0x00000004, /* Intended to use nasm_map_file() */
359 NF_IONBF = 0x00000010, /* Force unbuffered stdio */
360 NF_IOLBF = 0x00000020, /* Force line buffered stdio */
361 NF_IOFBF = 0000000030 /* Force fully buffered stdio */
363 #define NF_BUF_MASK 0x30
365 FILE *nasm_open_read(const char *filename, enum file_flags flags);
366 FILE *nasm_open_write(const char *filename, enum file_flags flags);
368 void nasm_set_binary_mode(FILE *f);
370 /* Probe for existence of a file */
371 bool nasm_file_exists(const char *filename);
373 #define ZERO_BUF_SIZE 65536 /* Default value */
374 #if defined(BUFSIZ) && (BUFSIZ > ZERO_BUF_SIZE)
375 # undef ZERO_BUF_SIZE
376 # define ZERO_BUF_SIZE BUFSIZ
377 #endif
378 extern const uint8_t zero_buffer[ZERO_BUF_SIZE];
380 /* Missing fseeko/ftello */
381 #ifndef HAVE_FSEEKO
382 # undef off_t /* Just in case it is a macro */
383 # ifdef HAVE__FSEEKI64
384 # define fseeko _fseeki64
385 # define ftello _ftelli64
386 # define off_t int64_t
387 # else
388 # define fseeko fseek
389 # define ftello ftell
390 # define off_t long
391 # endif
392 #endif
394 const void *nasm_map_file(FILE *fp, off_t start, off_t len);
395 void nasm_unmap_file(const void *p, size_t len);
396 off_t nasm_file_size(FILE *f);
397 off_t nasm_file_size_by_path(const char *pathname);
398 bool nasm_file_time(time_t *t, const char *pathname);
399 void fwritezero(off_t bytes, FILE *fp);
401 static inline bool const_func overflow_general(int64_t value, int bytes)
403 int sbit;
404 int64_t vmax, vmin;
406 if (bytes >= 8)
407 return false;
409 sbit = (bytes << 3) - 1;
410 vmax = ((int64_t)2 << sbit) - 1;
411 vmin = -((int64_t)2 << sbit);
413 return value < vmin || value > vmax;
416 static inline bool const_func overflow_signed(int64_t value, int bytes)
418 int sbit;
419 int64_t vmax, vmin;
421 if (bytes >= 8)
422 return false;
424 sbit = (bytes << 3) - 1;
425 vmax = ((int64_t)1 << sbit) - 1;
426 vmin = -((int64_t)1 << sbit);
428 return value < vmin || value > vmax;
431 static inline bool const_func overflow_unsigned(int64_t value, int bytes)
433 int sbit;
434 int64_t vmax, vmin;
436 if (bytes >= 8)
437 return false;
439 sbit = (bytes << 3) - 1;
440 vmax = ((int64_t)2 << sbit) - 1;
441 vmin = 0;
443 return value < vmin || value > vmax;
446 static inline int64_t const_func signed_bits(int64_t value, int bits)
448 if (bits < 64) {
449 value &= ((int64_t)1 << bits) - 1;
450 if (value & (int64_t)1 << (bits - 1))
451 value |= (int64_t)((uint64_t)-1 << bits);
453 return value;
456 /* check if value is power of 2 */
457 #define is_power2(v) ((v) && ((v) & ((v) - 1)) == 0)
459 /* try to get the system stack size */
460 extern size_t nasm_get_stack_size_limit(void);
462 #endif