memory_macro_h

/*
**	Copyright 1995, Universities Research Association.  All rights reserved.
*/
/******************************************************************************/
/* memory_macro.h
**
**	Macros for handling dynamic memory
*/
/******************************************************************************/

#ifndef MEMORY_MACRO_DEFINED				/* do this only once */
#define	MEMORY_MACRO_DEFINED

/*
 * Includers used to get malloc, etc. warped to clib_malloc via #defines in this
 * file and presumably included cbslib.h for a prototype.  Now they get warped via
 * explicit definitions of weak symbols and will need a prototype for malloc, etc.
 *
 * We would like to just include stdlib.h for the prototypes of malloc, etc., but
 * that causes trouble.  The trouble is created by some things in these stdlib.h
 * either directly or indirectly. For example, stdlib.h creates a #define NULL
 * (void*)0, which conflicts with the clib #define of NULL to 0, an integer. 
 * Since some of the code base uses NULL as an integer, e.g., wnlib.c in ul_clib,
 * this is a problem.  As a result we go to some work to preserve the value of
 * NULL that existed prior to the include of this file.  However, since the
 * the preprocessor does not evaluate define's immediately the best we can do
 * is just assume that NULL was 0.
 */
#ifdef  NULL

#include <stdlib.h>
#undef  NULL
#define NULL 0

#else

#include <stdlib.h>

#endif

#include "cbslib.h"

#define malloc(siz)		((void *) clib_malloc((siz)))
#define calloc(num,siz)		((void *) clib_calloc((num),(siz)))
#define realloc(mem_addr,siz)	((void *) clib_realloc((char *) (mem_addr),(siz)))
/*
 * Trickery to deal with clib_free takeing a ptr to ptr while RTL free takes only
 * a ptr.   Would like to have all code subject to this macro, but that causes
 * trouble.  For example, in ul_clib's wnlib.cpp mem_addr is an expression for
 * some free calls.  This results in code that wants to take an address of an
 * expression, which is a syntax error.  Therefore, we continue to do this
 * warp here instead of in clib_wraps.h.
 */
#define free(mem_addr)		(clib_free(reinterpret_cast<char**>(&(mem_addr))))
#define cfree(mem_addr)		(clib_free(reinterpret_cast<char**>(&(mem_addr))))

#endif