libctf: mark various args as unused in the !HAVE_MMAP case
[deliverable/binutils-gdb.git] / libctf / ctf-subr.c
1 /* Simple subrs.
2 Copyright (C) 2019 Free Software Foundation, Inc.
3
4 This file is part of libctf.
5
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
9 version.
10
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.
15
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/>. */
19
20 #include <ctf-impl.h>
21 #ifdef HAVE_MMAP
22 #include <sys/mman.h>
23 #endif
24 #include <sys/types.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 static size_t _PAGESIZE _libctf_unused_;
30 int _libctf_version = CTF_VERSION; /* Library client version. */
31 int _libctf_debug = 0; /* Debugging messages enabled. */
32
33 _libctf_malloc_ void *
34 ctf_data_alloc (size_t size)
35 {
36 void *ret;
37
38 #ifdef HAVE_MMAP
39 if (_PAGESIZE == 0)
40 _PAGESIZE = sysconf(_SC_PAGESIZE);
41
42 if (size > _PAGESIZE)
43 {
44 ret = mmap (NULL, size, PROT_READ | PROT_WRITE,
45 MAP_PRIVATE | MAP_ANON, -1, 0);
46 if (ret == MAP_FAILED)
47 ret = NULL;
48 }
49 else
50 ret = calloc (1, size);
51 #else
52 ret = calloc (1, size);
53 #endif
54 return ret;
55 }
56
57 void
58 ctf_data_free (void *buf, size_t size _libctf_unused_)
59 {
60 #ifdef HAVE_MMAP
61 /* Must be the same as the check in ctf_data_alloc(). */
62
63 if (size > _PAGESIZE)
64 (void) munmap (buf, size);
65 else
66 free (buf);
67 #else
68 free (buf);
69 #endif
70 }
71
72 /* Private, read-only mmap from a file, with fallback to copying.
73
74 No handling of page-offset issues at all: the caller must allow for that. */
75
76 _libctf_malloc_ void *
77 ctf_mmap (size_t length, size_t offset, int fd)
78 {
79 void *data;
80
81 #ifdef HAVE_MMAP
82 data = mmap (NULL, length, PROT_READ, MAP_PRIVATE, fd, offset);
83 if (data == MAP_FAILED)
84 data = NULL;
85 #else
86 if ((data = malloc (length)) != NULL)
87 {
88 if (ctf_pread (fd, data, length, offset) <= 0)
89 {
90 free (data);
91 data = NULL;
92 }
93 }
94 #endif
95 return data;
96 }
97
98 void
99 ctf_munmap (void *buf, size_t length _libctf_unused_)
100 {
101 #ifdef HAVE_MMAP
102 (void) munmap (buf, length);
103 #else
104 free (buf);
105 #endif
106 }
107
108 void
109 ctf_data_protect (void *buf _libctf_unused_, size_t size _libctf_unused_)
110 {
111 #ifdef HAVE_MMAP
112 /* Must be the same as the check in ctf_data_alloc(). */
113
114 if (size > _PAGESIZE)
115 (void) mprotect (buf, size, PROT_READ);
116 #endif
117 }
118
119 _libctf_malloc_ void *
120 ctf_alloc (size_t size)
121 {
122 return (malloc (size));
123 }
124
125 void
126 ctf_free (void *buf)
127 {
128 free (buf);
129 }
130
131 ssize_t
132 ctf_pread (int fd, void *buf, ssize_t count, off_t offset)
133 {
134 ssize_t len;
135 size_t acc = 0;
136 char *data = (char *) buf;
137
138 #ifdef HAVE_PREAD
139 while (count > 0)
140 {
141 errno = 0;
142 if (((len = pread (fd, data, count, offset)) < 0) &&
143 errno != EINTR)
144 return len;
145 if (errno == EINTR)
146 continue;
147
148 acc += len;
149 if (len == 0) /* EOF. */
150 return acc;
151
152 count -= len;
153 offset += len;
154 data += len;
155 }
156 return acc;
157 #else
158 off_t orig_off;
159
160 if ((orig_off = lseek (fd, 0, SEEK_CUR)) < 0)
161 return -1;
162 if ((lseek (fd, offset, SEEK_SET)) < 0)
163 return -1;
164
165 while (count > 0)
166 {
167 errno = 0;
168 if (((len = read (fd, data, count)) < 0) &&
169 errno != EINTR)
170 return len;
171 if (errno == EINTR)
172 continue;
173
174 acc += len;
175 if (len == 0) /* EOF. */
176 break;
177
178 count -= len;
179 data += len;
180 }
181 if ((lseek (fd, orig_off, SEEK_SET)) < 0)
182 return -1; /* offset is smashed. */
183 #endif
184
185 return acc;
186 }
187
188 const char *
189 ctf_strerror (int err)
190 {
191 return (const char *) (strerror (err));
192 }
193
194 /* Set the CTF library client version to the specified version. If version is
195 zero, we just return the default library version number. */
196 int
197 ctf_version (int version)
198 {
199 if (version < 0)
200 {
201 errno = EINVAL;
202 return -1;
203 }
204
205 if (version > 0)
206 {
207 /* Dynamic version switching is not presently supported. */
208 if (version != CTF_VERSION)
209 {
210 errno = ENOTSUP;
211 return -1;
212 }
213 ctf_dprintf ("ctf_version: client using version %d\n", version);
214 _libctf_version = version;
215 }
216
217 return _libctf_version;
218 }
219
220 void
221 libctf_init_debug (void)
222 {
223 static int inited;
224 if (!inited)
225 {
226 _libctf_debug = getenv ("LIBCTF_DEBUG") != NULL;
227 inited = 1;
228 }
229 }
230
231 void ctf_setdebug (int debug)
232 {
233 /* Ensure that libctf_init_debug() has been called, so that we don't get our
234 debugging-on-or-off smashed by the next call. */
235
236 libctf_init_debug();
237 _libctf_debug = debug;
238 ctf_dprintf ("CTF debugging set to %i\n", debug);
239 }
240
241 int ctf_getdebug (void)
242 {
243 return _libctf_debug;
244 }
245
246 _libctf_printflike_ (1, 2)
247 void ctf_dprintf (const char *format, ...)
248 {
249 if (_libctf_debug)
250 {
251 va_list alist;
252
253 va_start (alist, format);
254 fflush (stdout);
255 (void) fputs ("libctf DEBUG: ", stderr);
256 (void) vfprintf (stderr, format, alist);
257 va_end (alist);
258 }
259 }
This page took 0.042684 seconds and 5 git commands to generate.