On 04/08/2011 07:54 AM, Eric Blake wrote: > I'm pushing this as the simplest patch, but you may want to instead > consider renaming the field members to not shadow functions that might > have been #defined into replacement names.
Thanks, I did that. I also pulled standard_allocator out of careadlinkat.c, renamed it to stdlib_allocator, and put it into a new module "allocator". This would have avoided your problem, and it will help for the vasnprintf hacking I mentioned a day ago. Also, this business about doing a "#undef malloc" and "#define malloc rpl_malloc" in our header, and then "#undef malloc" in the .c file, gives me the willies. Suppose stdlib.h itself does a "#define malloc __better_malloc"? We've lost the better implementation. To avoid this problem I altered stdlib.in.h so that it now no longer does "#undef malloc" if the including file indicates (via "#define _GL_USE_STDLIB_ALLOC 1") that it doesn't want malloc to be replaced. Here's the gist of the change: +/* 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 - " Any file that doesn't require GNU or POSIX semantics can "#define _GL_USE_STDLIB_ALLOC 1". Similarly for realloc. I went through the code that currently does "#undef malloc" and changed it to use this new symbol instead. >From 621c6422ded148d515be1671be58de94b9fbb168 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Fri, 8 Apr 2011 10:41:30 -0700 Subject: [PATCH 01/13] careadlinkat: rename members to avoid problem * lib/allocator.h (struct allocator): Rename members from malloc/realloc to allocate/reallocate, to avoid problems if malloc and realloc are #define'd. Reported by Eric Blake in <http://lists.gnu.org/archive/html/bug-gnulib/2011-04/msg00091.html>. * lib/careadlinkat.c (careadlinkat): Adjust to renaming. careadlinkat: fix compilation error on mingw --- ChangeLog | 11 ++++++++++- lib/allocator.h | 10 +++++----- lib/careadlinkat.c | 8 ++++---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2c887a6..4dee8b4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2011-04-08 Paul Eggert <egg...@cs.ucla.edu> + + careadlinkat: rename members to avoid problem + * lib/allocator.h (struct allocator): Rename members from + malloc/realloc to allocate/reallocate, to avoid problems if malloc + and realloc are #define'd. Reported by Eric Blake in + <http://lists.gnu.org/archive/html/bug-gnulib/2011-04/msg00091.html>. + * lib/careadlinkat.c (careadlinkat): Adjust to renaming. + 2011-04-08 Eric Blake <ebl...@redhat.com> nonblocking: reduce dependency diff --git a/lib/allocator.h b/lib/allocator.h index 4ac863b..e30732b 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 *); diff --git a/lib/careadlinkat.c b/lib/careadlinkat.c index eb2e009..0b54b1d 100644 --- a/lib/careadlinkat.c +++ b/lib/careadlinkat.c @@ -133,16 +133,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; } @@ -159,7 +159,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); -- 1.7.4 >From 2f1d4690876d7d4abe56fed037a7bb932fa208be Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Fri, 8 Apr 2011 11:06:03 -0700 Subject: [PATCH 02/13] stdlib: let modules use system malloc, realloc * lib/stdlib.in.h (malloc, realloc): Don't #define or add warnings if !_GL_USE_STDLIB_ALLOC. --- ChangeLog | 4 ++++ lib/stdlib.in.h | 48 ++++++++++++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4dee8b4..ace9753 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2011-04-08 Paul Eggert <egg...@cs.ucla.edu> + stdlib: let modules use system malloc, realloc + * lib/stdlib.in.h (malloc, realloc): Don't #define or add warnings + if !_GL_USE_STDLIB_ALLOC. + careadlinkat: rename members to avoid problem * lib/allocator.h (struct allocator): Rename members from malloc/realloc to allocate/reallocate, to avoid problems if malloc diff --git a/lib/stdlib.in.h b/lib/stdlib.in.h index 2697a4b..c857de4 100644 --- a/lib/stdlib.in.h +++ b/lib/stdlib.in.h @@ -255,23 +255,29 @@ _GL_WARN_ON_USE (ptsname, "grantpt is not portable - " # endif #endif -#if @GNULIB_MALLOC_POSIX@ -# if @REPLACE_MALLOC@ -# if !(defined __cplusplus && defined GNULIB_NAMESPACE) -# undef malloc -# define malloc rpl_malloc -# 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 !_GL_USE_STDLIB_ALLOC +# if @GNULIB_MALLOC_POSIX@ +# if @REPLACE_MALLOC@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef malloc +# define malloc rpl_malloc +# endif _GL_FUNCDECL_RPL (malloc, void *, (size_t size)); _GL_CXXALIAS_RPL (malloc, void *, (size_t size)); -# else +# else _GL_CXXALIAS_SYS (malloc, void *, (size_t size)); -# endif +# endif _GL_CXXALIASWARN (malloc); -#elif defined GNULIB_POSIXCHECK -# undef malloc +# elif defined GNULIB_POSIXCHECK +# undef malloc /* Assume malloc is always declared. */ _GL_WARN_ON_USE (malloc, "malloc is not POSIX compliant everywhere - " "use gnulib module malloc-posix for portability"); +# endif #endif /* Convert a multibyte character to a wide character. */ @@ -529,23 +535,25 @@ _GL_WARN_ON_USE (setstate_r, "setstate_r is unportable - " #endif -#if @GNULIB_REALLOC_POSIX@ -# if @REPLACE_REALLOC@ -# if !(defined __cplusplus && defined GNULIB_NAMESPACE) -# undef realloc -# define realloc rpl_realloc -# endif +#if !_GL_USE_STDLIB_ALLOC +# if @GNULIB_REALLOC_POSIX@ +# if @REPLACE_REALLOC@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef realloc +# define realloc rpl_realloc +# endif _GL_FUNCDECL_RPL (realloc, void *, (void *ptr, size_t size)); _GL_CXXALIAS_RPL (realloc, void *, (void *ptr, size_t size)); -# else +# else _GL_CXXALIAS_SYS (realloc, void *, (void *ptr, size_t size)); -# endif +# endif _GL_CXXALIASWARN (realloc); -#elif defined GNULIB_POSIXCHECK -# undef realloc +# elif defined GNULIB_POSIXCHECK +# undef realloc /* Assume realloc is always declared. */ _GL_WARN_ON_USE (realloc, "realloc is not POSIX compliant everywhere - " "use gnulib module realloc-posix for portability"); +# endif #endif #if @GNULIB_REALPATH@ -- 1.7.4 >From 244deae70cdfa203bdf6ec45d081b2e5e3d872a2 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Fri, 8 Apr 2011 11:11:49 -0700 Subject: [PATCH 03/13] * lib/careadlinkat.c (_GL_USE_STDLIB_ALLOC): Define. (malloc, realloc): Don't #undef; no longer needed. --- ChangeLog | 2 ++ lib/careadlinkat.c | 20 +++++--------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index ace9753..dc4c33c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,8 @@ stdlib: let modules use system malloc, realloc * lib/stdlib.in.h (malloc, realloc): Don't #define or add warnings if !_GL_USE_STDLIB_ALLOC. + * lib/careadlinkat.c (_GL_USE_STDLIB_ALLOC): Define. + (malloc, realloc): Don't #undef; no longer needed. careadlinkat: rename members to avoid problem * lib/allocator.h (struct allocator): Rename members from diff --git a/lib/careadlinkat.c b/lib/careadlinkat.c index 0b54b1d..5f0d43c 100644 --- a/lib/careadlinkat.c +++ b/lib/careadlinkat.c @@ -18,6 +18,7 @@ /* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */ +#define _GL_USE_STDLIB_ALLOC 1 #include <config.h> #include "careadlinkat.h" @@ -52,10 +53,10 @@ careadlinkatcwd (int fd, char const *filename, char *buffer, } #endif -/* Forward declaration. We want to #undef malloc before initializing - this struct, but cannot do so until after all code that uses named - fields from "allocator.h" has been compiled. */ -static struct allocator const standard_allocator; +/* 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. @@ -168,14 +169,3 @@ careadlinkat (int fd, char const *filename, errno = ENOMEM; return NULL; } - -/* Use the system functions, not the gnulib overrides, because this - module does not depend on GNU or POSIX semantics. See comments - above why this must occur here. */ -#undef malloc -#undef realloc - -/* 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 }; -- 1.7.4 >From 46920adb4b365ed76e67ab4bbc6f8c5d323f0941 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Fri, 8 Apr 2011 11:14:38 -0700 Subject: [PATCH 04/13] * lib/malloca.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. --- ChangeLog | 1 + lib/malloca.c | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index dc4c33c..db63f77 100644 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,7 @@ if !_GL_USE_STDLIB_ALLOC. * lib/careadlinkat.c (_GL_USE_STDLIB_ALLOC): Define. (malloc, realloc): Don't #undef; no longer needed. + * lib/malloca.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. careadlinkat: rename members to avoid problem * lib/allocator.h (struct allocator): Rename members from diff --git a/lib/malloca.c b/lib/malloca.c index be3783e..45d3075 100644 --- a/lib/malloca.c +++ b/lib/malloca.c @@ -16,6 +16,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#define _GL_USE_STDLIB_ALLOC 1 #include <config.h> /* Specification. */ @@ -23,9 +24,6 @@ #include "verify.h" -/* Use the system functions, not the gnulib overrides in this file. */ -#undef malloc - /* The speed critical point in this file is freea() applied to an alloca() result: it must be fast, to match the speed of alloca(). The speed of mmalloca() and freea() in the other case are not critical, because they -- 1.7.4 >From b326f9a51226330e2fe8e2fd8166b98892466261 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Fri, 8 Apr 2011 11:30:47 -0700 Subject: [PATCH 05/13] * lib/progreloc.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. --- ChangeLog | 1 + lib/progreloc.c | 2 +- 2 files changed, 2 insertions(+), 1 deletions(-) diff --git a/ChangeLog b/ChangeLog index db63f77..2782be6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,7 @@ * lib/careadlinkat.c (_GL_USE_STDLIB_ALLOC): Define. (malloc, realloc): Don't #undef; no longer needed. * lib/malloca.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. + * lib/progreloc.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. careadlinkat: rename members to avoid problem * lib/allocator.h (struct allocator): Rename members from diff --git a/lib/progreloc.c b/lib/progreloc.c index 0769c5e..4a3fa48 100644 --- a/lib/progreloc.c +++ b/lib/progreloc.c @@ -16,6 +16,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#define _GL_USE_STDLIB_ALLOC 1 #include <config.h> /* Specification. */ @@ -94,7 +95,6 @@ extern char * canonicalize_file_name (const char *name); #undef close /* Use the system functions, not the gnulib overrides in this file. */ -#undef malloc #undef sprintf #undef set_program_name -- 1.7.4 >From 1c0adf6eaa833f9a4e135d4e0971e3cdaaf16e5a Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Fri, 8 Apr 2011 11:31:54 -0700 Subject: [PATCH 06/13] * lib/setenv.c (_GL_USE_STDLIB_ALLOC, malloc, realloc): Likewise. --- ChangeLog | 1 + lib/setenv.c | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2782be6..c6c0aea 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,7 @@ (malloc, realloc): Don't #undef; no longer needed. * lib/malloca.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. * lib/progreloc.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. + * lib/setenv.c (_GL_USE_STDLIB_ALLOC, malloc, realloc): Likewise. careadlinkat: rename members to avoid problem * lib/allocator.h (struct allocator): Rename members from diff --git a/lib/setenv.c b/lib/setenv.c index 7c06192..173d95f 100644 --- a/lib/setenv.c +++ b/lib/setenv.c @@ -15,6 +15,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #if !_LIBC +# define _GL_USE_STDLIB_ALLOC 1 # include <config.h> #endif @@ -64,10 +65,6 @@ __libc_lock_define_initialized (static, envlock) # define clearenv __clearenv # define tfind __tfind # define tsearch __tsearch -#else -/* Use the system functions, not the gnulib overrides in this file. */ -# undef malloc -# undef realloc #endif /* In the GNU C library implementation we try to be more clever and -- 1.7.4 >From cec5fdf10e7a5cefedf2fe7993dbfd1eba7407c5 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Fri, 8 Apr 2011 11:34:08 -0700 Subject: [PATCH 07/13] * lib/canonicalize-lgpl.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. --- ChangeLog | 1 + lib/canonicalize-lgpl.c | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index c6c0aea..a138add 100644 --- a/ChangeLog +++ b/ChangeLog @@ -8,6 +8,7 @@ * lib/malloca.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. * lib/progreloc.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. * lib/setenv.c (_GL_USE_STDLIB_ALLOC, malloc, realloc): Likewise. + * lib/canonicalize-lgpl.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. careadlinkat: rename members to avoid problem * lib/allocator.h (struct allocator): Rename members from diff --git a/lib/canonicalize-lgpl.c b/lib/canonicalize-lgpl.c index 9bfb44f..1574ec1 100644 --- a/lib/canonicalize-lgpl.c +++ b/lib/canonicalize-lgpl.c @@ -16,6 +16,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #ifndef _LIBC +# define _GL_USE_STDLIB_ALLOC 1 # include <config.h> #endif @@ -68,8 +69,6 @@ # endif # define __readlink readlink # define __set_errno(e) errno = (e) -/* Use the system functions, not the gnulib overrides in this file. */ -# undef malloc # ifndef MAXSYMLINKS # ifdef SYMLOOP_MAX # define MAXSYMLINKS SYMLOOP_MAX -- 1.7.4 >From b273239729dbfa0685aa8f628d79c299a253228c Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Fri, 8 Apr 2011 11:35:40 -0700 Subject: [PATCH 08/13] * lib/relocatable.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. --- ChangeLog | 1 + lib/relocatable.c | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index a138add..b1c9356 100644 --- a/ChangeLog +++ b/ChangeLog @@ -9,6 +9,7 @@ * lib/progreloc.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. * lib/setenv.c (_GL_USE_STDLIB_ALLOC, malloc, realloc): Likewise. * lib/canonicalize-lgpl.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. + * lib/relocatable.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. careadlinkat: rename members to avoid problem * lib/allocator.h (struct allocator): Rename members from diff --git a/lib/relocatable.c b/lib/relocatable.c index cbff85f..a7bbd99 100644 --- a/lib/relocatable.c +++ b/lib/relocatable.c @@ -25,6 +25,7 @@ # define _GNU_SOURCE 1 #endif +#define _GL_USE_STDLIB_ALLOC 1 #include <config.h> /* Specification. */ @@ -86,9 +87,6 @@ # define FILE_SYSTEM_PREFIX_LEN(P) 0 #endif -/* Use the system functions, not the gnulib overrides in this file. */ -#undef malloc - /* Original installation prefix. */ static char *orig_prefix; static size_t orig_prefix_len; -- 1.7.4 >From 114e7761fb1faafd08a5e80b79e4bcee015c4692 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Fri, 8 Apr 2011 11:37:24 -0700 Subject: [PATCH 09/13] * lib/relocwrapper.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. --- ChangeLog | 1 + lib/relocwrapper.c | 2 +- 2 files changed, 2 insertions(+), 1 deletions(-) diff --git a/ChangeLog b/ChangeLog index b1c9356..3d6ba9e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -10,6 +10,7 @@ * lib/setenv.c (_GL_USE_STDLIB_ALLOC, malloc, realloc): Likewise. * lib/canonicalize-lgpl.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. * lib/relocatable.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. + * lib/relocwrapper.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. careadlinkat: rename members to avoid problem * lib/allocator.h (struct allocator): Rename members from diff --git a/lib/relocwrapper.c b/lib/relocwrapper.c index 2f4e845..737ac94 100644 --- a/lib/relocwrapper.c +++ b/lib/relocwrapper.c @@ -43,6 +43,7 @@ libc functions, no gettext(), no error(), no xmalloc(), no xsetenv(). */ +#define _GL_USE_STDLIB_ALLOC 1 #include <config.h> #include <stdio.h> @@ -58,7 +59,6 @@ /* Use the system functions, not the gnulib overrides in this file. */ #undef fprintf -#undef malloc /* Return a copy of the filename, with an extra ".bin" at the end. More generally, it replaces "${EXEEXT}" at the end with ".bin${EXEEXT}". */ -- 1.7.4 >From 8b95bb407c7b35f14d49468091aeeacff5b86805 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Fri, 8 Apr 2011 11:38:38 -0700 Subject: [PATCH 10/13] * lib/malloc.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. --- ChangeLog | 1 + lib/malloc.c | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3d6ba9e..d7c0283 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,6 +11,7 @@ * lib/canonicalize-lgpl.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. * lib/relocatable.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. * lib/relocwrapper.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. + * lib/malloc.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. careadlinkat: rename members to avoid problem * lib/allocator.h (struct allocator): Rename members from diff --git a/lib/malloc.c b/lib/malloc.c index 73c503f..ef07f6c 100644 --- a/lib/malloc.c +++ b/lib/malloc.c @@ -18,6 +18,7 @@ /* written by Jim Meyering and Bruno Haible */ +#define _GL_USE_STDLIB_ALLOC 1 #include <config.h> /* Only the AC_FUNC_MALLOC macro defines 'malloc' already in config.h. */ #ifdef malloc @@ -28,14 +29,10 @@ # define NEED_MALLOC_GNU 1 #endif -/* Specification. */ #include <stdlib.h> #include <errno.h> -/* Call the system's malloc below. */ -#undef malloc - /* Allocate an N-byte block of memory from the heap. If N is zero, allocate a 1-byte block. */ -- 1.7.4 >From 0367644e656876587f6a9806c153bba3e0845ac2 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Fri, 8 Apr 2011 11:39:01 -0700 Subject: [PATCH 11/13] * lib/realloc.c (_GL_USE_STDLIB_ALLOC, malloc, realloc): Likewise. --- ChangeLog | 1 + lib/realloc.c | 14 +------------- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index d7c0283..1423496 100644 --- a/ChangeLog +++ b/ChangeLog @@ -12,6 +12,7 @@ * lib/relocatable.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. * lib/relocwrapper.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. * lib/malloc.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. + * lib/realloc.c (_GL_USE_STDLIB_ALLOC, malloc, realloc): Likewise. careadlinkat: rename members to avoid problem * lib/allocator.h (struct allocator): Rename members from diff --git a/lib/realloc.c b/lib/realloc.c index 6ef37e7..0c96ffa 100644 --- a/lib/realloc.c +++ b/lib/realloc.c @@ -18,6 +18,7 @@ /* written by Jim Meyering and Bruno Haible */ +#define _GL_USE_STDLIB_ALLOC 1 #include <config.h> /* Only the AC_FUNC_REALLOC macro defines 'realloc' already in config.h. */ @@ -34,23 +35,10 @@ # define SYSTEM_MALLOC_GLIBC_COMPATIBLE 1 #endif -/* Below we want to call the system's malloc and realloc. - Undefine the symbols here so that including <stdlib.h> provides a - declaration of malloc(), not of rpl_malloc(), and likewise for realloc. */ -#undef malloc -#undef realloc - -/* Specification. */ #include <stdlib.h> #include <errno.h> -/* Below we want to call the system's malloc and realloc. - Undefine the symbols, if they were defined by gnulib's <stdlib.h> - replacement. */ -#undef malloc -#undef realloc - /* Change the size of an allocated block of memory P to N bytes, with error checking. If N is zero, change it to 1. If P is NULL, use malloc. */ -- 1.7.4 >From 59e478167d46160aca0318907036ee6fac118746 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Fri, 8 Apr 2011 13:05:39 -0700 Subject: [PATCH 12/13] * lib/stdlib.in.h (malloc, realloc): Limit this change to a smaller scope. --- ChangeLog | 2 ++ lib/stdlib.in.h | 46 ++++++++++++++++++++++------------------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1423496..7f66754 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,8 @@ stdlib: let modules use system malloc, realloc * lib/stdlib.in.h (malloc, realloc): Don't #define or add warnings if !_GL_USE_STDLIB_ALLOC. + (malloc, realloc): Limit this change to a smaller scope. + * lib/careadlinkat.c (_GL_USE_STDLIB_ALLOC): Define. (malloc, realloc): Don't #undef; no longer needed. * lib/malloca.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise. diff --git a/lib/stdlib.in.h b/lib/stdlib.in.h index c857de4..b9ada2c 100644 --- a/lib/stdlib.in.h +++ b/lib/stdlib.in.h @@ -259,25 +259,24 @@ _GL_WARN_ON_USE (ptsname, "grantpt is not portable - " 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 !_GL_USE_STDLIB_ALLOC -# if @GNULIB_MALLOC_POSIX@ -# if @REPLACE_MALLOC@ -# if !(defined __cplusplus && defined GNULIB_NAMESPACE) -# undef malloc -# define malloc rpl_malloc -# endif +#if @GNULIB_MALLOC_POSIX@ +# if @REPLACE_MALLOC@ +# if !((defined __cplusplus && defined GNULIB_NAMESPACE) \ + || _GL_USE_STDLIB_ALLOC) +# undef malloc +# define malloc rpl_malloc +# endif _GL_FUNCDECL_RPL (malloc, void *, (size_t size)); _GL_CXXALIAS_RPL (malloc, void *, (size_t size)); -# else +# else _GL_CXXALIAS_SYS (malloc, void *, (size_t size)); -# endif +# endif _GL_CXXALIASWARN (malloc); -# elif defined GNULIB_POSIXCHECK -# undef malloc +#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 - " "use gnulib module malloc-posix for portability"); -# endif #endif /* Convert a multibyte character to a wide character. */ @@ -535,25 +534,24 @@ _GL_WARN_ON_USE (setstate_r, "setstate_r is unportable - " #endif -#if !_GL_USE_STDLIB_ALLOC -# if @GNULIB_REALLOC_POSIX@ -# if @REPLACE_REALLOC@ -# if !(defined __cplusplus && defined GNULIB_NAMESPACE) -# undef realloc -# define realloc rpl_realloc -# endif +#if @GNULIB_REALLOC_POSIX@ +# if @REPLACE_REALLOC@ +# if !((defined __cplusplus && defined GNULIB_NAMESPACE) \ + || _GL_USE_STDLIB_ALLOC) +# undef realloc +# define realloc rpl_realloc +# endif _GL_FUNCDECL_RPL (realloc, void *, (void *ptr, size_t size)); _GL_CXXALIAS_RPL (realloc, void *, (void *ptr, size_t size)); -# else +# else _GL_CXXALIAS_SYS (realloc, void *, (void *ptr, size_t size)); -# endif +# endif _GL_CXXALIASWARN (realloc); -# elif defined GNULIB_POSIXCHECK -# undef realloc +#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 - " "use gnulib module realloc-posix for portability"); -# endif #endif #if @GNULIB_REALPATH@ -- 1.7.4 >From f5535cfab12e40dbe0898e50a0ca41ef0c1637ef Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Fri, 8 Apr 2011 13:22:51 -0700 Subject: [PATCH 13/13] allocator: New module. * modules/allocator, lib/allocator.c: New files. * lib/allocator.h (stdlib_allocator): New decl. * lib/careadlinkat.c (_GL_USE_STDLIB_ALLOC, standard_allocator): Remove. Do not include <stdlib.h>. (careadlinkat): Use stdlib_allocator instead of rolling our own. * modules/careadlinkat (Files): Remove lib/allocator.h. (Depends-on): Add allocator. --- ChangeLog | 9 +++++++++ lib/allocator.c | 5 +++++ lib/allocator.h | 3 +++ lib/careadlinkat.c | 9 +-------- modules/allocator | 22 ++++++++++++++++++++++ modules/careadlinkat | 2 +- 6 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 lib/allocator.c create mode 100644 modules/allocator diff --git a/ChangeLog b/ChangeLog index 7f66754..42dcdbd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2011-04-08 Paul Eggert <egg...@cs.ucla.edu> + allocator: New module. + * modules/allocator, lib/allocator.c: New files. + * lib/allocator.h (stdlib_allocator): New decl. + * lib/careadlinkat.c (_GL_USE_STDLIB_ALLOC, standard_allocator): + Remove. Do not include <stdlib.h>. + (careadlinkat): Use stdlib_allocator instead of rolling our own. + * modules/careadlinkat (Files): Remove lib/allocator.h. + (Depends-on): Add allocator. + stdlib: let modules use system malloc, realloc * lib/stdlib.in.h (malloc, realloc): Don't #define or add warnings if !_GL_USE_STDLIB_ALLOC. diff --git a/lib/allocator.c b/lib/allocator.c new file mode 100644 index 0000000..2c1a3da --- /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 e30732b..a89ba32 100644 --- a/lib/allocator.h +++ b/lib/allocator.h @@ -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 5f0d43c..7a7806d 100644 --- a/lib/careadlinkat.c +++ b/lib/careadlinkat.c @@ -18,7 +18,6 @@ /* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */ -#define _GL_USE_STDLIB_ALLOC 1 #include <config.h> #include "careadlinkat.h" @@ -27,7 +26,6 @@ #include <errno.h> #include <limits.h> -#include <stdlib.h> #include <string.h> #include <unistd.h> @@ -53,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 @@ -90,7 +83,7 @@ careadlinkat (int fd, char const *filename, char stack_buf[1024]; if (! alloc) - alloc = &standard_allocator; + alloc = &stdlib_allocator; if (! buffer_size) { diff --git a/modules/allocator b/modules/allocator new file mode 100644 index 0000000..6ff5552 --- /dev/null +++ b/modules/allocator @@ -0,0 +1,22 @@ +Description: +Storage allocators. + +Files: +lib/allocator.h +lib/allocator.c + +Depends-on: + +configure.ac: + +Makefile.am: +lib_SOURCES += allocator.c + +Include: +"allocator.h" + +License: +LGPLv2+ + +Maintainer: +all diff --git a/modules/careadlinkat b/modules/careadlinkat index 670fde0..b4bc4a4 100644 --- a/modules/careadlinkat +++ b/modules/careadlinkat @@ -4,9 +4,9 @@ Read symbolic links into a buffer without size limitation, relative to fd. Files: lib/careadlinkat.c lib/careadlinkat.h -lib/allocator.h Depends-on: +allocator readlink ssize_t unistd -- 1.7.4