diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2014-04-24 05:48:19 +0300 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2014-04-24 05:48:19 +0300 |
commit | 07fb211522418f8537c24b28dc1ec79da0044658 (patch) | |
tree | c791ef660dceb41738c23f5fab3799f22db75275 /xalloc.h | |
parent | ab8ea56780ec1ee0d82c43e7ab7b7204ccab3ae2 (diff) | |
parent | 6c29291112b6b767e98ce3fb93dd4752d0ef1469 (diff) | |
download | egawk-07fb211522418f8537c24b28dc1ec79da0044658.tar.gz egawk-07fb211522418f8537c24b28dc1ec79da0044658.tar.bz2 egawk-07fb211522418f8537c24b28dc1ec79da0044658.zip |
Merge branch 'gawk-4.1-stable'
Diffstat (limited to 'xalloc.h')
-rw-r--r-- | xalloc.h | 24 |
1 files changed, 20 insertions, 4 deletions
@@ -136,6 +136,8 @@ xnmalloc (size_t n, size_t s) #ifdef GAWK #include <errno.h> +extern void r_fatal(const char *msg, ...) ATTRIBUTE_NORETURN ; + /* Allocate an array of N objects, each with S bytes of memory, dynamically, with error checking. S must be nonzero. Clear the contents afterwards. */ @@ -165,8 +167,6 @@ xrealloc(void *p, size_t size) void xalloc_die (void) { - extern void r_fatal(const char *msg, ...) ATTRIBUTE_NORETURN ; - r_fatal(_("xalloc: malloc failed: %s"), strerror(errno)); } @@ -179,6 +179,22 @@ xmemdup (void const *p, size_t s) { return memcpy (xmalloc (s), p, s); } + +/* xstrdup --- strdup and die if fails */ +char *xstrdup(const char *s) +{ + char *p; + int l; + + if (s == NULL) + r_fatal(_("xstrdup: null parameter")); + + l = strlen(s); + p = xmemdup(s, l + 1); + p[l] = '\0'; + + return p; +} #endif /* Change the size of an allocated block of memory P to an array of N @@ -260,7 +276,7 @@ x2nrealloc (void *p, size_t *pn, size_t s) requests, when the invoking code specifies an old size of zero. 64 bytes is the largest "small" request for the GNU C library malloc. */ - enum { DEFAULT_MXFAST = 64 }; + enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 }; n = DEFAULT_MXFAST / s; n += !n; @@ -274,7 +290,7 @@ x2nrealloc (void *p, size_t *pn, size_t s) worth the trouble. */ if ((size_t) -1 / 3 * 2 / s <= n) xalloc_die (); - n += (n + 1) / 2; + n += n / 2 + 1; } *pn = n; |