diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2021-06-11 17:18:57 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2021-06-11 17:20:11 -0700 |
commit | 61d866244d7ce4a89a5ed46b768bcace9247d8e8 (patch) | |
tree | 16c8098026320dda39a9b561cc1db48be53e1e61 /lib/xalloc.h | |
parent | f1698a709865b6fea53f1c417c8cb1db5dec4adc (diff) | |
download | gnulib-61d866244d7ce4a89a5ed46b768bcace9247d8e8.tar.gz |
xalloc: new idx_t-based allocators
This is for code that prefers to use idx_t for sizes.
* lib/xalloc.h (ximalloc, xizalloc, xicalloc, xirealloc)
(xireallocarray, ximemdup, ximemdup0) [GNULIB_XALLOC]:
New decls.
(x2nrealloc): Now just a decl, as the body is moved into xmalloc.c.
* lib/xmalloc.c: Include ialloc.h.
Rename some local parameters to be consistent with the .h files.
(nonnull): New static function.
(xmalloc, xcalloc): Simplify by using nonnull.
(ximalloc, xirealloc, xireallocarray, xizalloc, xicalloc)
(ximemdup, ximemdup0): New functions.
(x2nrealloc): Moved here from xalloc.h.
* modules/xalloc (Depends-on): Add ialloc.
Diffstat (limited to 'lib/xalloc.h')
-rw-r--r-- | lib/xalloc.h | 114 |
1 files changed, 17 insertions, 97 deletions
diff --git a/lib/xalloc.h b/lib/xalloc.h index 6cd7a680cd..70ef0971ff 100644 --- a/lib/xalloc.h +++ b/lib/xalloc.h @@ -53,21 +53,26 @@ extern "C" { #if GNULIB_XALLOC -void *xmalloc (size_t s) - _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); -void *xzalloc (size_t s) - _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); +void *xmalloc (size_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); +void *ximalloc (idx_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); +void *xzalloc (size_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); +void *xizalloc (idx_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); void *xcalloc (size_t n, size_t s) - _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)); -void *xrealloc (void *p, size_t s) - _GL_ATTRIBUTE_ALLOC_SIZE ((2)); + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)); +void *xicalloc (idx_t n, idx_t s) + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)); +void *xrealloc (void *p, size_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2)); +void *xirealloc (void *p, idx_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2)); void *xreallocarray (void *p, size_t n, size_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)); -void *x2realloc (void *p, size_t *pn); -void *xpalloc (void *pa, idx_t *nitems, idx_t nitems_incr_min, - ptrdiff_t nitems_max, idx_t item_size); -void *xmemdup (void const *p, size_t s) - _GL_ATTRIBUTE_ALLOC_SIZE ((2)); +void *xireallocarray (void *p, idx_t n, idx_t s) + _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)); +void *x2realloc (void *p, size_t *ps); /* superseded by xpalloc */ +void *x2nrealloc (void *p, size_t *pn, size_t s); /* superseded by xpalloc */ +void *xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s); +void *xmemdup (void const *p, size_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2)); +void *ximemdup (void const *p, idx_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2)); +char *ximemdup0 (void const *p, idx_t s) _GL_ATTRIBUTE_MALLOC; char *xstrdup (char const *str) _GL_ATTRIBUTE_MALLOC; @@ -120,91 +125,6 @@ xnrealloc (void *p, size_t n, size_t s) return xreallocarray (p, n, s); } -/* If P is null, allocate a block of at least *PN such objects; - otherwise, reallocate P so that it contains more than *PN objects - each of S bytes. S must be nonzero. Set *PN to the new number of - objects, and return the pointer to the new block. *PN is never set - to zero, and the returned pointer is never null. - - Repeated reallocations are guaranteed to make progress, either by - allocating an initial block with a nonzero size, or by allocating a - larger block. - - In the following implementation, nonzero sizes are increased by a - factor of approximately 1.5 so that repeated reallocations have - O(N) overall cost rather than O(N**2) cost, but the - specification for this function does not guarantee that rate. - - Here is an example of use: - - int *p = NULL; - size_t used = 0; - size_t allocated = 0; - - void - append_int (int value) - { - if (used == allocated) - p = x2nrealloc (p, &allocated, sizeof *p); - p[used++] = value; - } - - This causes x2nrealloc to allocate a block of some nonzero size the - first time it is called. - - To have finer-grained control over the initial size, set *PN to a - nonzero value before calling this function with P == NULL. For - example: - - int *p = NULL; - size_t used = 0; - size_t allocated = 0; - size_t allocated1 = 1000; - - void - append_int (int value) - { - if (used == allocated) - { - p = x2nrealloc (p, &allocated1, sizeof *p); - allocated = allocated1; - } - p[used++] = value; - } - - */ - -XALLOC_INLINE void * -x2nrealloc (void *p, size_t *pn, size_t s) -{ - size_t n = *pn; - - if (! p) - { - if (! n) - { - /* The approximate size to use for initial small allocation - requests, when the invoking code specifies an old size of - zero. This is the largest "small" request for the GNU C - library malloc. */ - enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 }; - - n = DEFAULT_MXFAST / s; - n += !n; - } - } - else - { - /* Set N = floor (1.5 * N) + 1 to make progress even if N == 0. */ - if (INT_ADD_WRAPV (n, (n >> 1) + 1, &n)) - xalloc_die (); - } - - p = xreallocarray (p, n, s); - *pn = n; - return p; -} - /* Return a pointer to a new buffer of N bytes. This is like xmalloc, except it returns char *. */ |