1 /*
2  * Copyright (c) 2005 - 2010, Nils R. Weller
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef N_LIBC_H
29 #define N_LIBC_H
30 
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <sys/mman.h>
36 
37 char	*n_strdup(const char *msg);
38 void	*n_memdup(const void *data, size_t len);
39 void	n_xmalloc_set_guard(size_t nbytes);
40 void	*n_xmalloc(size_t nbytes);
41 void	*n_xrealloc(void *block, size_t nbytes);
42 char	*n_xstrdup(const char *msg);
43 void	*n_xmemdup(const void *data, size_t len);
44 void	make_room(char **p, size_t *size, size_t nbytes);
45 void	x_fprintf(FILE *fd, const char *fmt, ...);
46 void	x_fputc(int ch, FILE *fd);
47 void	x_fflush(FILE *fd);
48 void	*debug_malloc_pages(size_t nbytes);
49 void	debug_make_unwritable(void *p, size_t size);
50 void	debug_make_writable(void *p, size_t size);
51 FILE	*get_tmp_file(char *temp, char *output, char *postfix);
52 void	dounimpl(const char *f, int line);
53 void	dobuggypath(const char *f, int line);
54 
55 
56 struct ga_option {
57 	int	ch;
58 	char	*name;
59 	int	takesarg;
60 };
61 
62 #define ROUND_UP_TO_MULTIPLE(val, round_to_multiple) \
63 	((val) % (round_to_multiple)? (val) + (round_to_multiple) - (val) % (round_to_multiple): (val))
64 
65 #define APPEND_LIST(list, tail, node) do { \
66 	if ((tail) == NULL) { \
67 		(list) = (tail) = (node); \
68 	} else { \
69 		(tail)->next = (node); \
70 		(tail) = (tail)->next; \
71 	} \
72 } while (0)
73 
74 #define N_OPTIONS(ar) (sizeof (ar) / sizeof (ar[0]))
75 
76 extern int	n_optind;
77 extern char	*n_optarg;
78 
79 int	nw_get_arg(int, char **, struct ga_option *, int nopts, int *idx);
80 int	find_cmd(const char *name, char *output, size_t outsiz);
81 void	irix_abort(void);
82 
83 struct timeval;
84 
85 void	start_timer(struct timeval *tv);
86 int	stop_timer(struct timeval *tv);
87 
88 #define unimpl() dounimpl(__func__, __LINE__)
89 #define buggypath() dobuggypath(__func__, __LINE__)
90 
91 #if 0
92 #define DEBUG_REALLOC
93 #define DEBUG_FREE
94 #define DEBUG_MALLOC
95 #endif
96 
97 #ifdef DEBUG_MALLOC
98 #define n_xmalloc(x) n_xmalloc((x) + 50)
99 #endif
100 
101 #ifdef DEBUG_REALLOC
102 #define n_xrealloc(x, y) n_xrealloc((x), (y) + 128)
103 #endif
104 
105 #ifdef DEBUG_FREE
106 #undef free
107 #define free(x) ((void) x)
108 #endif
109 
110 #include "features.h"
111 
112 #endif
113 
114