xref: /openbsd/usr.bin/nm/util.h (revision bf198cc6)
1*bf198cc6Smillert /*	$OpenBSD: util.h,v 1.6 2019/01/25 00:19:26 millert Exp $	*/
2e9fb207dSmillert 
3e9fb207dSmillert /*
4*bf198cc6Smillert  * Placed in the public domain by Todd C. Miller <millert@openbsd.org>
5e9fb207dSmillert  * on October 9, 2004.
6e9fb207dSmillert  */
75b19ca8cSmickey 
85b19ca8cSmickey #define	MMAP(ptr, len, prot, flags, fd, off)	do {		\
95b19ca8cSmickey 	if ((ptr = mmap(NULL, len, prot, flags, fd, off)) == MAP_FAILED) { \
105b19ca8cSmickey 		usemmap = 0;						\
115b19ca8cSmickey 		if (errno != EINVAL)					\
125b19ca8cSmickey 			warn("mmap");					\
135b19ca8cSmickey 		else if ((ptr = malloc(len)) == NULL) {			\
145b19ca8cSmickey 			ptr = MAP_FAILED;				\
155b19ca8cSmickey 			warn("malloc");					\
165b19ca8cSmickey 		} else if (pread(fd, ptr, len, off) != len) {		\
175b19ca8cSmickey 			free(ptr);					\
185b19ca8cSmickey 			ptr = MAP_FAILED;				\
195b19ca8cSmickey 			warn("pread");					\
205b19ca8cSmickey 		}							\
215b19ca8cSmickey 	}								\
225b19ca8cSmickey } while (0)
235b19ca8cSmickey 
245b19ca8cSmickey #define MUNMAP(addr, len)	do {					\
255b19ca8cSmickey 	if (usemmap)							\
265b19ca8cSmickey 		munmap(addr, len);					\
275b19ca8cSmickey 	else								\
285b19ca8cSmickey 		free(addr);						\
29c4fd534dSsemarie 	addr = NULL;							\
305b19ca8cSmickey } while (0)
315b19ca8cSmickey 
325b19ca8cSmickey extern int usemmap;
3396272ccbSguenther extern int dynamic_only;
341a0915b6Smiod 
351a0915b6Smiod struct xnlist {
361a0915b6Smiod 	struct nlist nl;
371a0915b6Smiod 	unsigned long n_size;
381a0915b6Smiod };
39