diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2011-04-08 14:53:30 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2011-04-08 14:53:30 -0700 |
commit | 7ec98caf7757bbf462d91a5cebd440cf12cc5816 (patch) | |
tree | a06f12700c55c0a932163ff80c6c3d01883a9c36 /lib | |
parent | 70476b54414db1be5261c71073fc991eab5cd34a (diff) |
Update from gnulib.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/allocator.c | 5 | ||||
-rw-r--r-- | lib/allocator.h | 13 | ||||
-rw-r--r-- | lib/careadlinkat.c | 21 | ||||
-rw-r--r-- | lib/gnulib.mk | 10 | ||||
-rw-r--r-- | lib/stdlib.in.h | 14 |
5 files changed, 37 insertions, 26 deletions
diff --git a/lib/allocator.c b/lib/allocator.c new file mode 100644 index 0000000000..2c1a3da03a --- /dev/null +++ b/lib/allocator.c @@ -0,0 +1,5 @@ +#define _GL_USE_STDLIB_ALLOC 1 +#include <config.h> +#include "allocator.h" +#include <stdlib.h> +struct allocator const stdlib_allocator = { malloc, realloc, free, NULL }; diff --git a/lib/allocator.h b/lib/allocator.h index 4ac863b224..a89ba32b09 100644 --- a/lib/allocator.h +++ b/lib/allocator.h @@ -30,16 +30,16 @@ struct allocator attributes do not work with pointers to functions. See <http://lists.gnu.org/archive/html/bug-gnulib/2011-04/msg00007.html>. */ - /* Call MALLOC to allocate memory, like 'malloc'. On failure MALLOC + /* Call ALLOCATE to allocate memory, like 'malloc'. On failure ALLOCATE should return NULL, though not necessarily set errno. When given a zero size it may return NULL even if successful. */ - void *(*malloc) (size_t); + void *(*allocate) (size_t); - /* If nonnull, call REALLOC to reallocate memory, like 'realloc'. - On failure REALLOC should return NULL, though not necessarily set + /* If nonnull, call REALLOCATE to reallocate memory, like 'realloc'. + On failure REALLOCATE should return NULL, though not necessarily set errno. When given a zero size it may return NULL even if successful. */ - void *(*realloc) (void *, size_t); + void *(*reallocate) (void *, size_t); /* Call FREE to free memory, like 'free'. */ void (*free) (void *); @@ -50,4 +50,7 @@ struct allocator void (*die) (void); }; +/* An allocator using the stdlib functions and a null DIE function. */ +extern struct allocator const stdlib_allocator; + #endif diff --git a/lib/careadlinkat.c b/lib/careadlinkat.c index 15ffe24c0f..7a7806d121 100644 --- a/lib/careadlinkat.c +++ b/lib/careadlinkat.c @@ -26,15 +26,9 @@ #include <errno.h> #include <limits.h> -#include <stdlib.h> #include <string.h> #include <unistd.h> -/* Use the system functions, not the gnulib overrides, because this - module does not depend on GNU or POSIX semantics. */ -#undef malloc -#undef realloc - /* Define this independently so that stdint.h is not a prerequisite. */ #ifndef SIZE_MAX # define SIZE_MAX ((size_t) -1) @@ -57,11 +51,6 @@ careadlinkatcwd (int fd, char const *filename, char *buffer, } #endif -/* A standard allocator. For now, only careadlinkat needs this, but - perhaps it should be moved to the allocator module. */ -static struct allocator const standard_allocator = - { malloc, realloc, free, NULL }; - /* Assuming the current directory is FD, get the symbolic link value of FILENAME as a null-terminated string and put it into a buffer. If FD is AT_FDCWD, FILENAME is interpreted relative to the current @@ -94,7 +83,7 @@ careadlinkat (int fd, char const *filename, char stack_buf[1024]; if (! alloc) - alloc = &standard_allocator; + alloc = &stdlib_allocator; if (! buffer_size) { @@ -138,16 +127,16 @@ careadlinkat (int fd, char const *filename, if (buf == stack_buf) { - char *b = (char *) alloc->malloc (link_size); + char *b = (char *) alloc->allocate (link_size); if (! b) break; memcpy (b, buf, link_size); buf = b; } - else if (link_size < buf_size && buf != buffer && alloc->realloc) + else if (link_size < buf_size && buf != buffer && alloc->reallocate) { /* Shrink BUF before returning it. */ - char *b = (char *) alloc->realloc (buf, link_size); + char *b = (char *) alloc->reallocate (buf, link_size); if (b) buf = b; } @@ -164,7 +153,7 @@ careadlinkat (int fd, char const *filename, buf_size = buf_size_max; else break; - buf = (char *) alloc->malloc (buf_size); + buf = (char *) alloc->allocate (buf_size); } while (buf); diff --git a/lib/gnulib.mk b/lib/gnulib.mk index d2fd669803..1938c6127a 100644 --- a/lib/gnulib.mk +++ b/lib/gnulib.mk @@ -21,6 +21,14 @@ libgnu_a_LIBADD = $(gl_LIBOBJS) libgnu_a_DEPENDENCIES = $(gl_LIBOBJS) EXTRA_libgnu_a_SOURCES = +## begin gnulib module allocator + +libgnu_a_SOURCES += allocator.c + +EXTRA_DIST += allocator.h + +## end gnulib module allocator + ## begin gnulib module arg-nonnull # The BUILT_SOURCES created by this Makefile snippet are not used via #include @@ -73,7 +81,7 @@ EXTRA_DIST += $(top_srcdir)/./c++defs.h libgnu_a_SOURCES += careadlinkat.c -EXTRA_DIST += allocator.h careadlinkat.h +EXTRA_DIST += careadlinkat.h ## end gnulib module careadlinkat diff --git a/lib/stdlib.in.h b/lib/stdlib.in.h index 2697a4bd1d..b9ada2cd1a 100644 --- a/lib/stdlib.in.h +++ b/lib/stdlib.in.h @@ -255,9 +255,14 @@ _GL_WARN_ON_USE (ptsname, "grantpt is not portable - " # endif #endif +/* If _GL_USE_STDLIB_ALLOC is nonzero, the including module does not + rely on GNU or POSIX semantics for malloc and realloc (for example, + by never specifying a zero size), so it does not need malloc or + realloc to be redefined. */ #if @GNULIB_MALLOC_POSIX@ # if @REPLACE_MALLOC@ -# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# if !((defined __cplusplus && defined GNULIB_NAMESPACE) \ + || _GL_USE_STDLIB_ALLOC) # undef malloc # define malloc rpl_malloc # endif @@ -267,7 +272,7 @@ _GL_CXXALIAS_RPL (malloc, void *, (size_t size)); _GL_CXXALIAS_SYS (malloc, void *, (size_t size)); # endif _GL_CXXALIASWARN (malloc); -#elif defined GNULIB_POSIXCHECK +#elif defined GNULIB_POSIXCHECK && !_GL_USE_STDLIB_ALLOC # undef malloc /* Assume malloc is always declared. */ _GL_WARN_ON_USE (malloc, "malloc is not POSIX compliant everywhere - " @@ -531,7 +536,8 @@ _GL_WARN_ON_USE (setstate_r, "setstate_r is unportable - " #if @GNULIB_REALLOC_POSIX@ # if @REPLACE_REALLOC@ -# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# if !((defined __cplusplus && defined GNULIB_NAMESPACE) \ + || _GL_USE_STDLIB_ALLOC) # undef realloc # define realloc rpl_realloc # endif @@ -541,7 +547,7 @@ _GL_CXXALIAS_RPL (realloc, void *, (void *ptr, size_t size)); _GL_CXXALIAS_SYS (realloc, void *, (void *ptr, size_t size)); # endif _GL_CXXALIASWARN (realloc); -#elif defined GNULIB_POSIXCHECK +#elif defined GNULIB_POSIXCHECK && !_GL_USE_STDLIB_ALLOC # undef realloc /* Assume realloc is always declared. */ _GL_WARN_ON_USE (realloc, "realloc is not POSIX compliant everywhere - " |