[rfc/rfa?] Rationalize *malloc() calling *malloc()
Andrew Cagney
ac131313@cygnus.com
Fri Mar 23 16:12:00 GMT 2001
http://sources.redhat.com/ml/gdb-patches/2001-03/msg00370.html
I committed the attatched. I reworked the comments explaing each of the
malloc() layers a little.
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 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.47
diff -p -r1.47 defs.h
*** defs.h 2001/03/21 18:31:47 1.47
--- defs.h 2001/03/23 23:37:47
*************** extern void free_current_contents (void
*** 510,531 ****
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);
--- 510,519 ----
*************** extern char *msavestring (void *, const
*** 953,967 ****
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. */
--- 941,966 ----
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/23 23:37:52
*************** request_quit (int signo)
*** 917,957 ****
/* 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 ----
/* 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,1060 ----
}
}
! /* The xmmalloc() family of memory management routines.
! These are are like the mmalloc() family except that they implement
! consistent semantics and guard against typical memory management
! problems: if a malloc fails, an internal error is thrown; if
! free(NULL) is called, it is ignored; if *alloc(0) is called, NULL
! is returned.
!
! All these routines are implemented using the mmalloc() family. */
!
! 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() */
}
--- 1079,1140 ----
}
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);
+ }
+
+ /* The xmalloc() (libiberty.h) family of memory management routines.
! These are like the ISO-C malloc() family except that they implement
! consistent semantics and guard against typical memory management
! problems. See xmmalloc() above for further information.
+ All these routines are wrappers to the xmmalloc() family. */
+
+ /* NOTE: These are declared using PTR to ensure consistency with
+ "libiberty.h". xfree() is GDB local. */
+
+ 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);
}
More information about the Gdb-patches
mailing list