clib_memory_h

/*
**	Copyright 2008-2017, Fermi Research Alliance, LLC.  All rights reserved.
*/
/******************************************************************************/
/* cbslib.h
**
**	function prototypes and defined constants for argument values for
**	CLIB dynamic memory routines
**
**	V0.0	B S Hendricks / Accelerator Controls		31-Jan-08
**	Added function prototype case insensitivity definitions
**
*/
/******************************************************************************/

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

/******************************************************************************/
/* 
**	defined constants for argument values
*/
/******************************************************************************/

/*
**  memory_manage...
*/
#define	MEMORY_MALLOC_TRACE_ON	2	/* check dynamic memory on every call to clib_malloc */
#define	MEMORY_FREE_TRACE_ON	4	/* check dynamic memory on every call to clib_free */
#define	MEMORY_MALLOC_FILL_ON	8	/* fill allocated memory with a specified value on every call to clib_malloc */
#define	MEMORY_FREE_FILL_ON	0x10	/* fill allocated memory with a specified value on every call to clib_free */


/******************************************************************************/
/* 
**	dynamic memory-related data structures
*/
/******************************************************************************/

typedef struct MEMORY_DIAG_INFO			/* dynamic memory analysis information */
    {
    unsigned int	num_alloc_blocks;
    unsigned int	dynamic_memory;
    unsigned int	free_errors;
    unsigned int	malloc_errors;
    unsigned int	realloc_errors;
    unsigned short	bad_alloc_list;
    unsigned short	bad_block_header;
    unsigned int	bad_header_block_num;
    unsigned short	bad_block_footer;
    unsigned int	bad_footer_block_num;
    } __attribute__((packed)) /* Added by the PACKINATOR(tm) */ MEMORY_DIAG_INFO;


/******************************************************************************/
/* 
**	dynamic memory-related macros
*/
/******************************************************************************/

#define	clib_xalloc(x,y) ((x) ? realloc((x), (y)) : malloc(y))	/* kluge to work around realloc bug */


/******************************************************************************/
/*
**	function prototypes for CLIB dynamic memory routines
*/
/******************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif

char *clib_calloc(unsigned int num_items, unsigned int item_size);
int clib_free(char **ptr);
char *clib_malloc(unsigned int num_bytes);
int clib_memory_block_length(char *address);
int clib_memory_info(MEMORY_DIAG_INFO *memory_info);
char *clib_realloc(char *ptr, unsigned int numbyt);
int memory_analyze(MEMORY_DIAG_INFO *memory_info, bool abort_on_fail = false);
unsigned int memory_diagnostic_control_c(unsigned int command);

#ifdef __cplusplus
}
#endif


/******************************************************************************/
/*
**	equivalent name definitions
*/
/******************************************************************************/

#define	CLIB_CALLOC			clib_calloc
#define	CLIB_FREE			clib_free
#define	CLIB_MALLOC			clib_malloc
#define	CLIB_MEMORY_BLOCK_LENGTH	clib_memory_block_length
#define	CLIB_MEMORY_INFO		clib_memory_info
#define	CLIB_REALLOC			clib_realloc
#define	MEMORY_ANALYZE			memory_analyze
#define	MEMORY_DIAGNOSTIC_CONTROL_C	memory_diagnostic_control_c

#endif