-
+ 8361650753226E8F2780AA6A9AFF24223B456FF648590333B6AE2D37281FAE485C572119DD2B66B4DC1B2DB46DFBE8168443CA72ABF1072C0446265CC576EFAD
vtools/lib/xalloc.c
(0 . 0)(1 . 107)
2325 #include <stdlib.h>
2326 #include <string.h>
2327 #include "xalloc.h"
2328 #include "error.h"
2329
2330 /* 1 if |calloc| is known to be compatible with GNU |calloc|. This
2331 matters if we are not also using the |calloc| module, which defines
2332 |HAVE_CALLOC_GNU| and supports the GNU API even on non-GNU
2333 platforms. */
2334 #if defined HAVE_CALLOC_GNU || (defined __GLIBC__ && !defined __UCLIBC__)
2335 enum { HAVE_GNU_CALLOC = 1 };
2336 #else
2337 enum { HAVE_GNU_CALLOC = 0 };
2338 #endif
2339
2340 void
2341 xalloc_die(void) {
2342 error(2, 0, "%s", "memory exhausted");
2343
2344 /* |_Noreturn| cannot be given to error, since it may return if
2345 its first argument is 0. To help compilers understand the
2346 |xalloc_die| does not return, call abort. Also, the abort is a
2347 safety feature if |exit_failure| is 0 (which shouldn't
2348 happen). */
2349 abort();
2350 }
2351
2352 /* Allocate |n| bytes of memory dynamically, with error checking. */
2353
2354 void *
2355 xmalloc(size_t n) {
2356 void *p = malloc(n);
2357 if (!p && n != 0)
2358 xalloc_die();
2359 return p;
2360 }
2361
2362 /* Change the size of an allocated block of memory |p| to |n| bytes,
2363 with error checking. */
2364
2365 void *
2366 xrealloc(void *p, size_t n) {
2367 if (!n && p) {
2368 /* The GNU and C99 realloc behaviors disagree here. Act like
2369 GNU, even if the underlying realloc is C99. */
2370 free(p);
2371 return NULL;
2372 }
2373
2374 p = realloc(p, n);
2375 if (!p && n)
2376 xalloc_die();
2377 return p;
2378 }
2379
2380 /* If |p| is null, allocate a block of at least |*pn| bytes;
2381 otherwise, reallocate |p| so that it contains more than |*pn|
2382 bytes. |*PN| must be nonzero unless |p| is |NULL|. Set |*pn| to
2383 the new block's size, and return the pointer to the new block.
2384 |*pn| is never set to zero, and the returned pointer is never
2385 |NULL|. */
2386
2387 void *
2388 x2realloc(void *p, size_t *pn) {
2389 return x2nrealloc(p, pn, 1);
2390 }
2391
2392 /* Allocate |s| bytes of zeroed memory dynamically, with error
2393 checking. There's no need for |xnzalloc (n, s)|, since it would be
2394 equivalent to |xcalloc (n, s)|. */
2395
2396 void *
2397 xzalloc(size_t s) {
2398 return memset (xmalloc(s), 0, s);
2399 }
2400
2401 /* Allocate zeroed memory for |n| elements of |s| bytes, with error
2402 checking. |s| must be nonzero. */
2403
2404 void *
2405 xcalloc(size_t n, size_t s) {
2406 void *p;
2407 /* Test for overflow, since objects with size greater than
2408 |PTRDIFF_MAX| cause pointer subtraction to go awry. Omit
2409 size-zero tests if |HAVE_GNU_CALLOC|, since GNU calloc never
2410 returns |NULL| if successful. */
2411 if (xalloc_oversized (n, s)
2412 || (!(p = calloc(n, s)) && (HAVE_GNU_CALLOC || n != 0)))
2413 xalloc_die();
2414 return p;
2415 }
2416
2417 /* Clone an object |p| of size |s|, with error checking. There's no
2418 need for |xnmemdup (p, n, s)|, since |xmemdup (p, n * s)| works
2419 without any need for an arithmetic overflow check. */
2420
2421 void *
2422 xmemdup(void const *p, size_t s) {
2423 return memcpy (xmalloc(s), p, s);
2424 }
2425
2426 /* Clone |string|. */
2427
2428 char *
2429 xstrdup(char const *string) {
2430 return xmemdup(string, strlen(string) + 1);
2431 }