240
241
extern void release_pack_memory(size_t, int);
242
243
-static inline char* xstrdup(const char *str)
244
-{
245
- char *ret = strdup(str);
246
- if (!ret) {
247
- release_pack_memory(strlen(str) + 1, -1);
248
- ret = strdup(str);
249
- if (!ret)
250
- die("Out of memory, strdup failed");
251
- }
252
- return ret;
253
-}
254
-
255
-static inline void *xmalloc(size_t size)
256
-{
257
- void *ret = malloc(size);
258
- if (!ret && !size)
259
- ret = malloc(1);
260
- if (!ret) {
261
- release_pack_memory(size, -1);
262
- ret = malloc(size);
263
- if (!ret && !size)
264
- ret = malloc(1);
265
- if (!ret)
266
- die("Out of memory, malloc failed");
267
- }
268
-#ifdef XMALLOC_POISON
269
- memset(ret, 0xA5, size);
270
-#endif
271
- return ret;
272
-}
273
-
274
-/*
275
- * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
276
- * "data" to the allocated memory, zero terminates the allocated memory,
277
- * and returns a pointer to the allocated memory. If the allocation fails,
278
- * the program dies.
279
- */
280
-static inline void *xmemdupz(const void *data, size_t len)
281
-{
282
- char *p = xmalloc(len + 1);
283
- memcpy(p, data, len);
284
- p[len] = '\0';
285
- return p;
286
-}
287
-
288
-static inline char *xstrndup(const char *str, size_t len)
289
-{
290
- char *p = memchr(str, '\0', len);
291
- return xmemdupz(str, p ? p - str : len);
292
-}
293
-
294
-static inline void *xrealloc(void *ptr, size_t size)
295
-{
296
- void *ret = realloc(ptr, size);
297
- if (!ret && !size)
298
- ret = realloc(ptr, 1);
299
- if (!ret) {
300
- release_pack_memory(size, -1);
301
- ret = realloc(ptr, size);
302
- if (!ret && !size)
303
- ret = realloc(ptr, 1);
304
- if (!ret)
305
- die("Out of memory, realloc failed");
306
- }
307
- return ret;
308
-}
309
-
310
-static inline void *xcalloc(size_t nmemb, size_t size)
311
-{
312
- void *ret = calloc(nmemb, size);
313
- if (!ret && (!nmemb || !size))
314
- ret = calloc(1, 1);
315
- if (!ret) {
316
- release_pack_memory(nmemb * size, -1);
317
- ret = calloc(nmemb, size);
318
- if (!ret && (!nmemb || !size))
319
- ret = calloc(1, 1);
320
- if (!ret)
321
- die("Out of memory, calloc failed");
322
- }
323
- return ret;
324
-}
325
-
326
-static inline void *xmmap(void *start, size_t length,
327
- int prot, int flags, int fd, off_t offset)
328
-{
329
- void *ret = mmap(start, length, prot, flags, fd, offset);
330
- if (ret == MAP_FAILED) {
331
- if (!length)
332
- return NULL;
333
- release_pack_memory(length, fd);
334
- ret = mmap(start, length, prot, flags, fd, offset);
335
- if (ret == MAP_FAILED)
336
- die("Out of memory? mmap failed: %s", strerror(errno));
337
- }
338
- return ret;
339
-}
340
-
341
-/*
342
- * xread() is the same a read(), but it automatically restarts read()
343
- * operations with a recoverable error (EAGAIN and EINTR). xread()
344
- * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
345
- */
346
-static inline ssize_t xread(int fd, void *buf, size_t len)
347
-{
348
- ssize_t nr;
349
- while (1) {
350
- nr = read(fd, buf, len);
351
- if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
352
- continue;
353
- return nr;
354
- }
355
-}
356
-
357
-/*
358
- * xwrite() is the same a write(), but it automatically restarts write()
359
- * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
360
- * GUARANTEE that "len" bytes is written even if the operation is successful.
361
- */
362
-static inline ssize_t xwrite(int fd, const void *buf, size_t len)
363
-{
364
- ssize_t nr;
365
- while (1) {
366
- nr = write(fd, buf, len);
367
- if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
368
- continue;
369
- return nr;
370
- }
371
-}
372
-
373
-static inline int xdup(int fd)
374
-{
375
- int ret = dup(fd);
376
- if (ret < 0)
377
- die("dup failed: %s", strerror(errno));
378
- return ret;
379
-}
380
-
381
-static inline FILE *xfdopen(int fd, const char *mode)
382
-{
383
- FILE *stream = fdopen(fd, mode);
384
- if (stream == NULL)
385
- die("Out of memory? fdopen failed: %s", strerror(errno));
386
- return stream;
387
-}
388
-
389
-static inline int xmkstemp(char *template)
390
-{
391
- int fd;
392
-
393
- fd = mkstemp(template);
394
- if (fd < 0)
395
- die("Unable to create temporary file: %s", strerror(errno));
396
- return fd;
397
-}
243
+extern char *xstrdup(const char *str);
244
+extern void *xmalloc(size_t size);
245
+extern void *xmemdupz(const void *data, size_t len);
246
+extern char *xstrndup(const char *str, size_t len);
247
+extern void *xrealloc(void *ptr, size_t size);
248
+extern void *xcalloc(size_t nmemb, size_t size);
249
+extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
250
+extern ssize_t xread(int fd, void *buf, size_t len);
251
+extern ssize_t xwrite(int fd, const void *buf, size_t len);
252
+extern int xdup(int fd);
253
+extern FILE *xfdopen(int fd, const char *mode);
254
+extern int xmkstemp(char *template);
255
256
static inline size_t xsize_t(off_t len)
257
{