/*
* IRC - Internet Relay Chat, include/ircd_alloc.h
* Copyright (C) 1999 Thomas Helvey <tomh@inxpress.net>
- *
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Commentary by Bleep (Thomas Helvey)
- *
- * $Id$
+ */
+/** @file
+ * @brief IRC daemon memory allocation functions.
+ * @version $Id$
*/
#ifndef INCLUDED_ircd_alloc_h
#define INCLUDED_ircd_alloc_h
/*
* memory resource allocation and test functions
*/
+/** Type of handler for out-of-memory conditions. */
typedef void (*OutOfMemoryHandler)(void);
extern void set_nomem_handler(OutOfMemoryHandler handler);
/* The mappings for the My* functions... */
+/** Helper macro for standard allocations. */
#define MyMalloc(size) \
DoMalloc(size, "malloc", __FILE__, __LINE__)
+/** Helper macro for zero-initialized allocations. */
#define MyCalloc(nelem, size) \
- DoMallocZero(size * nelem, "calloc", __FILE__, __LINE__)
+ DoMallocZero((size) * (nelem), "calloc", __FILE__, __LINE__)
+/** Helper macro for freeing memory. */
#define MyFree(p) \
- DoFree(p, __FILE__, __LINE__)
-
-/* No realloc because it is not currently used, and it is not really the
- * nicest function to be using anyway(i.e. its evil if you want it
- * go ahead and write it).
- */
+ if (p) \
+ DoFree(p, __FILE__, __LINE__)
-extern void *malloc_tmp;
+/** Helper macro for reallocating memory. */
+#define MyRealloc(p, size) \
+ DoRealloc(p, size, __FILE__, __LINE__)
/* First version: fast non-debugging macros... */
#ifndef MDEBUG
#define INCLUDED_stdlib_h
#endif
-extern OutOfMemoryHandler noMemHandler;
-
+/** Implementation macro for freeing memory. */
#define DoFree(x, file, line) do { free((x)); (x) = 0; } while(0)
-#define DoMalloc(size, type, file, line) \
- (\
- (malloc_tmp = malloc(size), \
- (malloc_tmp == NULL) ? noMemHandler() : 0), \
- malloc_tmp)
-
-#define DoMallocZero(size, type, file, line) \
- (\
- (DoMalloc(size, type, file, line), \
- memset(malloc_tmp, 0, size)), \
- malloc_tmp)
+extern void* DoMalloc(size_t len, const char*, const char*, int);
+extern void* DoMallocZero(size_t len, const char*, const char*, int);
+extern void *DoRealloc(void *, size_t, const char*, int);
/* Second version: slower debugging versions... */
#else /* defined(MDEBUG) */
#define DoMallocZero(size, type, file, line) \
dbg_malloc_zero(size, type, file, line)
#define DoFree(p, file, line) \
- dbg_free(p, file, line)
+ do { dbg_free(p, file, line); (p) = 0; } while (0)
+#define DoRealloc(p, size, file, line) \
+ dbg_realloc(p, size, file, line)
#endif /* defined(MDEBUG) */
#endif /* INCLUDED_ircd_alloc_h */