2 Copyright (C) 2019 Free Software Foundation, Inc.
4 This file is part of libctf.
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
24 #include <sys/types.h>
29 static size_t _PAGESIZE _libctf_unused_
;
30 int _libctf_debug
= 0; /* Debugging messages enabled. */
32 _libctf_malloc_
void *
33 ctf_data_alloc (size_t size
)
39 _PAGESIZE
= sysconf(_SC_PAGESIZE
);
43 ret
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
,
44 MAP_PRIVATE
| MAP_ANON
, -1, 0);
45 if (ret
== MAP_FAILED
)
49 ret
= calloc (1, size
);
51 ret
= calloc (1, size
);
57 ctf_data_free (void *buf
, size_t size _libctf_unused_
)
60 /* Must be the same as the check in ctf_data_alloc(). */
63 (void) munmap (buf
, size
);
71 /* Private, read-only mmap from a file, with fallback to copying.
73 No handling of page-offset issues at all: the caller must allow for that. */
75 _libctf_malloc_
void *
76 ctf_mmap (size_t length
, size_t offset
, int fd
)
81 data
= mmap (NULL
, length
, PROT_READ
, MAP_PRIVATE
, fd
, offset
);
82 if (data
== MAP_FAILED
)
85 if ((data
= malloc (length
)) != NULL
)
87 if (ctf_pread (fd
, data
, length
, offset
) <= 0)
98 ctf_munmap (void *buf
, size_t length _libctf_unused_
)
101 (void) munmap (buf
, length
);
108 ctf_data_protect (void *buf
, size_t size
)
111 /* Must be the same as the check in ctf_data_alloc(). */
113 if (size
> _PAGESIZE
)
114 (void) mprotect (buf
, size
, PROT_READ
);
118 _libctf_malloc_
void *
119 ctf_alloc (size_t size
)
121 return (malloc (size
));
131 ctf_pread (int fd
, void *buf
, ssize_t count
, off_t offset
)
135 char *data
= (char *) buf
;
141 if (((len
= pread (fd
, data
, count
, offset
)) < 0) &&
148 if (len
== 0) /* EOF. */
159 if ((orig_off
= lseek (fd
, 0, SEEK_CUR
)) < 0)
161 if ((lseek (fd
, offset
, SEEK_SET
)) < 0)
167 if (((len
= read (fd
, data
, count
)) < 0) &&
174 if (len
== 0) /* EOF. */
180 if ((lseek (fd
, orig_off
, SEEK_SET
)) < 0)
181 return -1; /* offset is smashed. */
188 ctf_strerror (int err
)
190 return (const char *) (strerror (err
));
194 libctf_init_debug (void)
199 _libctf_debug
= getenv ("LIBCTF_DEBUG") != NULL
;
204 void ctf_setdebug (int debug
)
206 /* Ensure that libctf_init_debug() has been called, so that we don't get our
207 debugging-on-or-off smashed by the next call. */
210 _libctf_debug
= debug
;
211 ctf_dprintf ("CTF debugging set to %i\n", debug
);
214 int ctf_getdebug (void)
216 return _libctf_debug
;
219 _libctf_printflike_ (1, 2)
220 void ctf_dprintf (const char *format
, ...)
226 va_start (alist
, format
);
228 (void) fputs ("libctf DEBUG: ", stderr
);
229 (void) vfprintf (stderr
, format
, alist
);