/**
* @file memory.h
*
* Memory allocation wrappers.
*
* @author Geoff Davis <geoff@circlemudsquared.org>
* @ingroup common
* @license All rights reserved. See license.doc for complete information.
* @package cs
* @since v1.0
*
* Copyright (C) 1993, 94 by the Trustees of the Johns Hopkins University
* CircleMUD is based on DikuMUD, Copyright (C) 1990, 1991.
*/
#ifndef __MEMORY_H__
#define __MEMORY_H__
/**
* Allocates enough memory for an array of the specified type consisting of
* the specified number of elements.
* @param typeName the name of the type of each element
* @param elemCount the number of elements to be allocated
* @return a pointer to the allocated memory, or NULL
*/
#define CIRCLE_ALLOCN(typeName, elemCount) \
((sizeof(typeName) * (elemCount)) > 0 ? \
((typeName*) calloc((elemCount), sizeof(typeName))) : (typeName*) 0)
/**
* Copies array elements (instead of bytes) from one place to another.
* @param dest the location to which elements are to be written
* @param src the location from which elements are to be read
* @param typeName the name of the type of each element
* @param elemCount the number of elements to be copied
* @return none
*/
#define CIRCLE_COPYN(dest, src, typeName, elemCount) \
do { \
if ((dest) && (src) && (dest) != (src) && \
((elemCount) * sizeof(typeName)) > 0) { \
memmove((dest), (src), sizeof(typeName) * (elemCount)); \
} \
} while (0)
/**
* Frees the specified pointer.
* @param p the pointer to the heap-allocated block to be freed
* @return none
*/
#define CIRCLE_FREE(p) \
do { \
if ((p)) { \
free(p); \
(p) = NULL; \
} \
} while (0)
#endif /* __MEMORY_H__ */