* [rfc/rfa?] Rationalize *malloc() calling *malloc()
@ 2001-03-19 18:43 Andrew Cagney
2001-03-20 1:32 ` Eli Zaretskii
0 siblings, 1 reply; 2+ messages in thread
From: Andrew Cagney @ 2001-03-19 18:43 UTC (permalink / raw)
To: GDB Patches
Hello,
The attached patch rationalizes the way the different malloc() variants
call each other. After this patch the sequence is hopefully:
for malloc in malloc realloc calloc free
x$malloc()
-> xm$malloc()
-> m$malloc()
For the sake of consistency, it also introduces:
xmcalloc()
xmfree()
I'll leave it as an exercise to the reader to figure out what the
current codes ordering convention is :-)
comments?
Andrew
2001-03-19 Andrew Cagney <ac131313@redhat.com>
* defs.h (xfree, mcalloc, mmalloc, mrealloc, mfree, xmmalloc,
xmrealloc): Move existing declarations to the one place and
re-order to be consistent.
(xmcalloc, xmfree): Declare.
(xmmalloc, xmrealoc): Assume ISO-C - use size_t size_t and void*
in declaration.
* utils.c (size_t): Delete #ifdef defining size_t.
(mmalloc, mrealloc, mcalloc, mfree): Re-order.
(mmalloc, mrealloc, mcalloc): Document as only calls in GDB
corresponding malloc, realloc, calloc.
(mfree): Call free directly.
(xmmalloc, xmrealloc): Clean up. Assume ISO-C.
(xmcalloc, xmfree): New functions. Copy old xcalloc and xfree
function bodies to here.
(xcalloc, xfree): Call xmcalloc and xmfree respectfully.
Index: defs.h
===================================================================
RCS file: /cvs/src/src/gdb/defs.h,v
retrieving revision 1.46
diff -p -r1.46 defs.h
*** defs.h 2001/03/20 01:37:09 1.46
--- defs.h 2001/03/20 02:37:11
*************** extern void free_current_contents (void
*** 367,388 ****
extern void null_cleanup (void *);
- extern void xfree (void *);
-
extern int myread (int, char *, int);
extern int query (char *, ...) ATTR_FORMAT (printf, 1, 2);
- #if !defined (USE_MMALLOC)
- /* NOTE: cagney/2000-03-04: The mmalloc functions need to use PTR
- rather than void* so that they are consistent with
- ../mmalloc/mmalloc.h. */
- extern PTR mcalloc (PTR, size_t, size_t);
- extern PTR mmalloc (PTR, size_t);
- extern PTR mrealloc (PTR, PTR, size_t);
- extern void mfree (PTR, PTR);
- #endif
-
extern void init_page_info (void);
extern CORE_ADDR host_pointer_to_address (void *ptr);
--- 367,376 ----
*************** extern char *msavestring (void *, const
*** 810,824 ****
extern char *mstrsave (void *, const char *);
! /* FIXME; was long, but this causes compile errors in msvc if already
! defined */
! #ifdef _MSC_VER
! extern PTR xmmalloc (PTR, size_t);
! extern PTR xmrealloc (PTR, PTR, size_t);
! #else
! extern PTR xmmalloc (PTR, long);
! extern PTR xmrealloc (PTR, PTR, long);
#endif
/* Like asprintf/vasprintf but get an internal_error if the call
fails. */
--- 798,823 ----
extern char *mstrsave (void *, const char *);
! #if !defined (USE_MMALLOC)
! /* NOTE: cagney/2000-03-04: The mmalloc functions need to use PTR
! rather than void* so that they are consistent with the delcaration
! in ../mmalloc/mmalloc.h. */
! extern PTR mcalloc (PTR, size_t, size_t);
! extern PTR mmalloc (PTR, size_t);
! extern PTR mrealloc (PTR, PTR, size_t);
! extern void mfree (PTR, PTR);
#endif
+
+ /* Robust versions of same. Throw an internal error when no memory,
+ guard against stray NULL arguments. */
+ extern void *xmmalloc (void *md, size_t size);
+ extern void *xmrealloc (void *md, void *ptr, size_t size);
+ extern void *xmcalloc (void *md, size_t number, size_t size);
+ extern void xmfree (void *md, void *ptr);
+
+ /* xmalloc(), xrealloc() and xcalloc() have already been declared in
+ "libiberty.h". */
+ extern void xfree (void *);
/* Like asprintf/vasprintf but get an internal_error if the call
fails. */
Index: utils.c
===================================================================
RCS file: /cvs/src/src/gdb/utils.c,v
retrieving revision 1.38
diff -p -r1.38 utils.c
*** utils.c 2001/03/20 01:37:09 1.38
--- utils.c 2001/03/20 02:37:24
*************** request_quit (int signo)
*** 917,957 ****
\f
/* Memory management stuff (malloc friends). */
- /* Make a substitute size_t for non-ANSI compilers. */
-
- #ifndef HAVE_STDDEF_H
- #ifndef size_t
- #define size_t unsigned int
- #endif
- #endif
-
#if !defined (USE_MMALLOC)
! PTR
! mcalloc (PTR md, size_t number, size_t size)
! {
! return calloc (number, size);
! }
PTR
mmalloc (PTR md, size_t size)
{
! return malloc (size);
}
PTR
mrealloc (PTR md, PTR ptr, size_t size)
{
if (ptr == 0) /* Guard against old realloc's */
! return malloc (size);
else
! return realloc (ptr, size);
}
void
mfree (PTR md, PTR ptr)
{
! xfree (ptr);
}
#endif /* USE_MMALLOC */
--- 917,952 ----
\f
/* Memory management stuff (malloc friends). */
#if !defined (USE_MMALLOC)
! /* NOTE: These must use PTR so that their definition matches the
! declaration found in mmalloc.h. */
PTR
mmalloc (PTR md, size_t size)
{
! return malloc (size); /* NOTE: GDB's only call to malloc() */
}
PTR
mrealloc (PTR md, PTR ptr, size_t size)
{
if (ptr == 0) /* Guard against old realloc's */
! return mmalloc (md, size);
else
! return realloc (ptr, size); /* NOTE: GDB's only call to ralloc() */
! }
!
! PTR
! mcalloc (PTR md, size_t number, size_t size)
! {
! return calloc (number, size); /* NOTE: GDB's only call to calloc() */
}
void
mfree (PTR md, PTR ptr)
{
! free (ptr); /* NOTE: GDB's only call to free() */
}
#endif /* USE_MMALLOC */
*************** nomem (long size)
*** 1028,1060 ****
}
}
! /* Like mmalloc but get error if no storage available, and protect against
! the caller wanting to allocate zero bytes. Whether to return NULL for
! a zero byte request, or translate the request into a request for one
! byte of zero'd storage, is a religious issue. */
! PTR
! xmmalloc (PTR md, long size)
{
! register PTR val;
if (size == 0)
{
val = NULL;
}
! else if ((val = mmalloc (md, size)) == NULL)
{
! nomem (size);
}
return (val);
}
-
- /* Like mrealloc but get error if no storage available. */
! PTR
! xmrealloc (PTR md, PTR ptr, long size)
{
! register PTR val;
if (size == 0)
{
--- 1023,1055 ----
}
}
! /* Like malloc(), realloc(), calloc() and free() but guard against
! NULL pointers. If an allocate fails, throw an internal error,
! during a free, just ignore it. Returns NULL when the allocated
! size is zero (an arbitrary choice). */
! void *
! xmmalloc (void *md, size_t size)
{
! void *val;
if (size == 0)
{
val = NULL;
}
! else
{
! val = mmalloc (md, size);
! if (val == NULL)
! nomem (size);
}
return (val);
}
! void *
! xmrealloc (void *md, void *ptr, size_t size)
{
! void *val;
if (size == 0)
{
*************** xmrealloc (PTR md, PTR ptr, long size)
*** 1079,1128 ****
}
return (val);
}
-
- /* Like malloc but get error if no storage available, and protect against
- the caller wanting to allocate zero bytes. */
! PTR
! xmalloc (size_t size)
{
- return (xmmalloc ((PTR) NULL, size));
- }
-
- /* Like calloc but get error if no storage available */
-
- PTR
- xcalloc (size_t number, size_t size)
- {
void *mem;
-
if (number == 0 || size == 0)
mem = NULL;
else
{
! mem = mcalloc (NULL, number, size);
if (mem == NULL)
nomem (number * size);
}
return mem;
}
! /* Like mrealloc but get error if no storage available. */
PTR
xrealloc (PTR ptr, size_t size)
{
! return (xmrealloc ((PTR) NULL, ptr, size));
}
! /* Free up space allocated by one of xmalloc(), xcalloc(), or
! xrealloc(). */
void
xfree (void *ptr)
{
! if (ptr != NULL)
! free (ptr); /* NOTE: GDB's only call to free() */
}
\f
--- 1074,1128 ----
}
return (val);
}
! void *
! xmcalloc (void *md, size_t number, size_t size)
{
void *mem;
if (number == 0 || size == 0)
mem = NULL;
else
{
! mem = mcalloc (md, number, size);
if (mem == NULL)
nomem (number * size);
}
return mem;
}
+
+ void
+ xmfree (void *md, void *ptr)
+ {
+ if (ptr != NULL)
+ mfree (md, ptr);
+ }
! /* Non- MMALLOC equivalents to xmalloc() et.al. */
! /* NOTE: These are declared using PTR to ensure consistency with
! "libiberty.h". */
PTR
+ xmalloc (size_t size)
+ {
+ return xmmalloc (NULL, size);
+ }
+
+ PTR
xrealloc (PTR ptr, size_t size)
{
! return xmrealloc (NULL, ptr, size);
}
! PTR
! xcalloc (size_t number, size_t size)
! {
! return xmcalloc (NULL, number, size);
! }
void
xfree (void *ptr)
{
! xmfree (NULL, ptr);
}
\f
From ac131313@cygnus.com Mon Mar 19 18:53:00 2001
From: Andrew Cagney <ac131313@cygnus.com>
To: GDB Patches <gdb-patches@sourceware.cygnus.com>
Subject: Re: [rfc] Delete macro MALLOC_INCOMPATIBLE
Date: Mon, 19 Mar 2001 18:53:00 -0000
Message-id: <3AB6C63D.8D8A4C7F@cygnus.com>
References: <3AB69BC0.E3617E2E@cygnus.com>
X-SW-Source: 2001-03/msg00371.html
Content-length: 3111
2001-03-19 Andrew Cagney <ac131313@redhat.com>
* defs.h: Remove #ifndef MALLOC_INCOMPATIBLE.
* config/sparc/xm-sun4os4.h (MALLOC_INCOMPATIBLE): Delete
macro. Do not include <malloc.h>.
* config/i386/xm-windows.h (MALLOC_INCOMPATIBLE): Ditto.
* config/pa/xm-hppah.h (MALLOC_INCOMPATIBLE): Delete macro.
(malloc, free, realloc): Delete declarations.
Index: doc/ChangeLog
2001-03-19 Andrew Cagney <ac131313@redhat.com>
* gdbint.texinfo (MALLOC_INCOMPATIBLE): Delete documentation.
Index: defs.h
===================================================================
RCS file: /cvs/src/src/gdb/defs.h,v
retrieving revision 1.43
diff -p -r1.43 defs.h
*** defs.h 2001/03/14 16:42:29 1.43
--- defs.h 2001/03/19 23:46:36
*************** extern int fclose (FILE *);
*** 945,952 ****
extern double atof (const char *); /* X3.159-1989 4.10.1.1 */
#endif
- #ifndef MALLOC_INCOMPATIBLE
-
#ifdef NEED_DECLARATION_MALLOC
extern PTR malloc ();
#endif
--- 945,950 ----
*************** extern PTR realloc ();
*** 958,965 ****
#ifdef NEED_DECLARATION_FREE
extern void free ();
#endif
-
- #endif /* MALLOC_INCOMPATIBLE */
/* Various possibilities for alloca. */
#ifndef alloca
--- 956,961 ----
Index: config/i386/xm-windows.h
===================================================================
RCS file: /cvs/src/src/gdb/config/i386/xm-windows.h,v
retrieving revision 1.1.1.2
diff -p -r1.1.1.2 xm-windows.h
*** xm-windows.h 1999/07/07 20:13:48 1.1.1.2
--- xm-windows.h 2001/03/19 23:46:37
***************
*** 28,36 ****
#undef HAVE_SBRK
#define CANT_FORK
- #define MALLOC_INCOMPATIBLE
-
- #include <malloc.h>
-
#define SIGQUIT 3
#define SIGTRAP 5
--- 28,32 ----
Index: config/sparc/xm-sun4os4.h
===================================================================
RCS file: /cvs/src/src/gdb/config/sparc/xm-sun4os4.h,v
retrieving revision 1.2
diff -p -r1.2 xm-sun4os4.h
*** xm-sun4os4.h 2001/03/06 08:21:37 1.2
--- xm-sun4os4.h 2001/03/19 23:46:37
***************
*** 23,35 ****
#define FPU
- /* /usr/include/malloc.h is included by vx-share/xdr_ld, and might
- declare these using char * not void *. The following should work with
- acc, gcc, or /bin/cc. */
-
- #define MALLOC_INCOMPATIBLE
- #include <malloc.h>
-
/* SunOS 4.x uses nonstandard "char *" as type of third argument to ptrace() */
#define PTRACE_ARG3_TYPE char*
--- 23,28 ----
Index: doc/gdbint.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdbint.texinfo,v
retrieving revision 1.20
diff -p -r1.20 gdbint.texinfo
*** gdbint.texinfo 2001/03/06 08:21:42 1.20
--- gdbint.texinfo 2001/03/19 23:46:53
*************** This macro is used as the argument to @c
*** 1151,1160 ****
@code{bfd_seek}). FIXME, should be replaced by SEEK_SET instead,
which is the POSIX equivalent.
- @item MALLOC_INCOMPATIBLE
- Define this if the system's prototype for @code{malloc} differs from the
- @sc{ansi} definition.
-
@item MMAP_BASE_ADDRESS
When using HAVE_MMAP, the first mapping should go at this address.
--- 1151,1156 ----
From kevinb@cygnus.com Mon Mar 19 19:04:00 2001
From: Kevin Buettner <kevinb@cygnus.com>
To: gdb-patches@sources.redhat.com
Subject: [PATCH] Conditionally include nlist.h in solib-legacy.c
Date: Mon, 19 Mar 2001 19:04:00 -0000
Message-id: <1010320030437.ZM8757@ocotillo.lan>
X-SW-Source: 2001-03/msg00372.html
Content-length: 1688
I've just committed the changes below. Hopefully, these will (finally)
fix J.T.'s NetBSD 1.3 solib-legacy.c build problems. I did test builds
on SunOS 4.1.4, NetBSD 1.4, NetBSD 1.5, FreeBSD 4.2, Red Hat Linux 7,
Solaris 8 (x86), and Unixware 7.
* configure.in (AC_CHECK_HEADERS): Check for existence of nlist.h.
* configure: Regenerate.
* config.in: Regenerate.
* solib-legacy.c (nlist.h): Include if HAVE_NLIST_H is defined.
Index: configure.in
===================================================================
RCS file: /cvs/src/src/gdb/configure.in,v
retrieving revision 1.59
diff -u -p -r1.59 configure.in
--- configure.in 2001/03/19 05:46:26 1.59
+++ configure.in 2001/03/20 02:51:41
@@ -118,7 +118,7 @@ case $host_os in solaris2.7 | solaris2.8
AC_DEFINE(_MSE_INT_H)
esac; esac
-AC_CHECK_HEADERS(ctype.h endian.h link.h thread_db.h proc_service.h \
+AC_CHECK_HEADERS(ctype.h endian.h nlist.h link.h thread_db.h proc_service.h \
memory.h objlist.h ptrace.h sgtty.h stddef.h stdlib.h \
string.h sys/procfs.h sys/ptrace.h sys/reg.h stdint.h \
term.h termio.h termios.h unistd.h wait.h sys/wait.h \
Index: solib-legacy.c
===================================================================
RCS file: /cvs/src/src/gdb/solib-legacy.c,v
retrieving revision 1.2
diff -u -p -r1.2 solib-legacy.c
--- solib-legacy.c 2001/03/19 05:46:27 1.2
+++ solib-legacy.c 2001/03/20 02:51:41
@@ -25,6 +25,12 @@
#include "solib-svr4.h"
#ifdef HAVE_LINK_H
+
+#ifdef HAVE_NLIST_H
+/* nlist.h needs to be included before link.h on some older *BSD systems. */
+#include <nlist.h>
+#endif
+
#include <link.h>
/* Fetch (and possibly build) an appropriate link_map_offsets structure
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [rfc/rfa?] Rationalize *malloc() calling *malloc()
2001-03-19 18:43 [rfc/rfa?] Rationalize *malloc() calling *malloc() Andrew Cagney
@ 2001-03-20 1:32 ` Eli Zaretskii
0 siblings, 0 replies; 2+ messages in thread
From: Eli Zaretskii @ 2001-03-20 1:32 UTC (permalink / raw)
To: Andrew Cagney; +Cc: GDB Patches
On Mon, 19 Mar 2001, Andrew Cagney wrote:
> --- 798,823 ----
>
> extern char *mstrsave (void *, const char *);
>
> ! #if !defined (USE_MMALLOC)
> ! /* NOTE: cagney/2000-03-04: The mmalloc functions need to use PTR
> ! rather than void* so that they are consistent with the delcaration
> ! in ../mmalloc/mmalloc.h. */
There's a typo in the above comment ("M-x ispell-comments-and-strings
RET" is your friend ;-).
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2001-03-20 1:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-19 18:43 [rfc/rfa?] Rationalize *malloc() calling *malloc() Andrew Cagney
2001-03-20 1:32 ` Eli Zaretskii
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox