-
+ 2146359188E16E7186846AE0B1026DC41BB7EC2C58FCE67D3F02F99BDB851F9325483084B55FFD5BCB4B0D36C08799BF3198BAAEDF7595B6E0EE85A042806446
vtools/lib/xalloc.h
(0 . 0)(1 . 170)
2436 #ifndef XALLOC_H_
2437 #define XALLOC_H_
2438
2439 #include <stddef.h>
2440 #include <stdint.h>
2441 #include "system.h"
2442
2443 /* This function is always triggered when memory is exhausted. It
2444 must be defined by the application, either explicitly or by using
2445 gnulib's xalloc-die module. This is the function to call when one
2446 wants the program to die because of a memory allocation
2447 failure. */
2448 extern _Noreturn void xalloc_die(void);
2449
2450 void *xmalloc(size_t s);
2451 void *xzalloc(size_t s);
2452 void *xcalloc(size_t n, size_t s);
2453 void *xrealloc(void *p, size_t s);
2454 void *x2realloc(void *p, size_t *pn);
2455 void *xmemdup(void const *p, size_t s);
2456 char *xstrdup(char const *str);
2457
2458 /* In the following macros, |t| must be an elementary or
2459 structure/union or typedef'ed type, or a pointer to such a type.
2460 To apply one of the following macros to a function pointer or array
2461 type, you need to typedef it first and use the typedef name. */
2462
2463 /* Allocate an object of type |t| dynamically, with error checking. */
2464 #define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
2465
2466 /* Allocate memory for |n| elements of type |t|, with error
2467 checking. */
2468 #define XNMALLOC(n, t) \
2469 ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
2470
2471 /* Allocate an object of type |t| dynamically, with error checking,
2472 and zero it. */
2473 #define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
2474
2475 /* Allocate memory for |n| elements of type |t|, with error checking,
2476 and zero it. */
2477 #define XCALLOC(n, t) \
2478 ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
2479
2480
2481 /* Allocate an array of |n| objects, each with |s| bytes of memory,
2482 dynamically, with error checking. |s| must be nonzero. */
2483
2484 inline void *xnmalloc(size_t n, size_t s);
2485
2486 inline void *
2487 xnmalloc(size_t n, size_t s) {
2488 if (xalloc_oversized (n, s))
2489 xalloc_die();
2490 return xmalloc(n * s);
2491 }
2492
2493 /* Change the size of an allocated block of memory |p| to an array of
2494 |n| objects each of |s| bytes, with error checking. |s| must be
2495 nonzero. */
2496
2497 inline void *xnrealloc(void *p, size_t n, size_t s);
2498
2499 inline void *
2500 xnrealloc(void *p, size_t n, size_t s) {
2501 if (xalloc_oversized (n, s))
2502 xalloc_die();
2503 return xrealloc(p, n * s);
2504 }
2505
2506 /* If |p| is |NULL|, allocate a block of at least |*pn| such objects;
2507 otherwise, reallocate |p| so that it contains more than |*pn|
2508 objects each of |s| bytes. |s| must be nonzero. Set |*pn| to the
2509 new number of objects, and return the pointer to the new block.
2510 |*PN| is never set to zero, and the returned pointer is never |NULL|.
2511
2512 Repeated reallocations are guaranteed to make progress, either by
2513 allocating an initial block with a nonzero size, or by allocating a
2514 larger block.
2515
2516 In the following implementation, nonzero sizes are increased by a
2517 factor of approximately 1.5 so that repeated reallocations have
2518 $O(N)$ overall cost rather than $O(N^2)$ cost, but the
2519 specification for this function does not guarantee that rate.
2520
2521 Here is an example of use:
2522
2523 |int *p = NULL;
2524 size_t used = 0;
2525 size_t allocated = 0;
2526
2527 void
2528 append_int (int value)
2529 {
2530 if (used == allocated)
2531 p = x2nrealloc (p, &allocated, sizeof *p);
2532 p[used++] = value;
2533 }|
2534
2535 This causes |x2nrealloc| to allocate a block of some nonzero size
2536 the first time it is called.
2537
2538 To have finer-grained control over the initial size, set |*pn| to a
2539 nonzero value before calling this function with |p == NULL|. For
2540 example:
2541
2542 |int *p = NULL;
2543 size_t used = 0;
2544 size_t allocated = 0;
2545 size_t allocated1 = 1000;
2546
2547 void
2548 append_int (int value)
2549 {
2550 if (used == allocated)
2551 {
2552 p = x2nrealloc (p, &allocated1, sizeof *p);
2553 allocated = allocated1;
2554 }
2555 p[used++] = value;
2556 }|
2557
2558 */
2559
2560 static inline void *
2561 x2nrealloc(void *p, size_t *pn, size_t s) {
2562 size_t n = *pn;
2563
2564 if (!p) {
2565 if (!n) {
2566 /* The approximate size to use for initial small
2567 allocation requests, when the invoking code specifies
2568 an old size of zero. This is the largest "small"
2569 request for the GNU C library malloc. */
2570 enum {
2571 DEFAULT_MXFAST = 64 * sizeof(size_t) / 4
2572 };
2573
2574 n = DEFAULT_MXFAST / s;
2575 n += !n;
2576 }
2577 if (xalloc_oversized (n, s))
2578 xalloc_die();
2579 } else {
2580 /* Set $n = \lfloor 1.5 * n \rfloor + 1$ so that progress is
2581 made even if $n = 0$. Check for overflow, so that $n * s$
2582 stays in both |ptrdiff_t| and |size_t| range. The check
2583 may be slightly conservative, but an exact check isn't
2584 worth the trouble. */
2585 if ((PTRDIFF_MAX < SIZE_MAX ? PTRDIFF_MAX : SIZE_MAX) / 3 * 2 / s
2586 <= n)
2587 xalloc_die();
2588 n += n / 2 + 1;
2589 }
2590
2591 *pn = n;
2592 return xrealloc(p, n * s);
2593 }
2594
2595 /* Return a pointer to a new buffer of |n| bytes. This is like
2596 xmalloc, except it returns |char *|. */
2597
2598 static inline char *xcharalloc(size_t n);
2599
2600 static inline char *
2601 xcharalloc(size_t n) {
2602 return XNMALLOC (n, char);
2603 }
2604
2605 #endif