xref: /openbsd/usr.bin/nm/util.h (revision 96272ccb)
1 /*	$OpenBSD: util.h,v 1.3 2015/05/17 20:19:08 guenther Exp $	*/
2 
3 /*
4  * Placed in the public domain by Todd C. Miller <Todd.Miller@courtesan.com>
5  * on October 9, 2004.
6  */
7 
8 #define	MMAP(ptr, len, prot, flags, fd, off)	do {		\
9 	if ((ptr = mmap(NULL, len, prot, flags, fd, off)) == MAP_FAILED) { \
10 		usemmap = 0;						\
11 		if (errno != EINVAL)					\
12 			warn("mmap");					\
13 		else if ((ptr = malloc(len)) == NULL) {			\
14 			ptr = MAP_FAILED;				\
15 			warn("malloc");					\
16 		} else if (pread(fd, ptr, len, off) != len) {		\
17 			free(ptr);					\
18 			ptr = MAP_FAILED;				\
19 			warn("pread");					\
20 		}							\
21 	}								\
22 } while (0)
23 
24 #define MUNMAP(addr, len)	do {					\
25 	if (usemmap)							\
26 		munmap(addr, len);					\
27 	else								\
28 		free(addr);						\
29 } while (0)
30 
31 extern int usemmap;
32 extern int dynamic_only;
33