/* mem.h - Magic stuff for finding memory leaks.
 * Written by Ron S. Novy
 *
 *  This should be the last include in the source file you are testing and
 * should be included before any code.
 *
 */

#ifndef _UC_MEM_H_
#define _UC_MEM_H_

#include "ucode\dll.h"

#ifdef __cplusplus
extern "C" {
#endif


#ifdef DEBUG_LEAKS

DLL_EXPORT void * uc_mem_malloc(size_t bytes, const char *file, int line);
DLL_EXPORT void * uc_mem_zmalloc(size_t bytes, const char *file, int line);
DLL_EXPORT void   uc_mem_free(void *p);
DLL_EXPORT void * uc_mem_realloc(void *ptr, size_t size, const char *file, int line);
DLL_EXPORT void * uc_mem_calloc(size_t num, size_t size, const char *file, int line);
DLL_EXPORT char * uc_mem_strdup(const char *str, const char *file, int line);
DLL_EXPORT char * uc_mem_ustrdup(const char *str, const char *file, int line);

#include "ucode/cbuf.h"
DLL_EXPORT FIFO * uc_leak_fifo_open(const char *name, const char *mode, const char *file, int line) _WARN_UNUSED_RESULT_;
DLL_EXPORT FIFO * uc_leak_fifo_create(int block_size, const char *file, int line) _WARN_UNUSED_RESULT_;
DLL_EXPORT void   uc_leak_fifo_destroy(FIFO *f, const char *file, int line);
DLL_EXPORT void * uc_leak_fifo_close(FIFO *f, const char *file, int line) _WARN_UNUSED_RESULT_;


#include <stdio.h>
DLL_EXPORT void uc_leak_fclose(FILE *p);
DLL_EXPORT FILE *uc_leak_fopen(
    const char *file, int line, const char *filename, const char *mode);

DLL_EXPORT void debug_atexit(void);

#undef zmalloc
#undef malloc
#undef realloc
#undef calloc
#undef free
#undef strdup
#undef ustrdup

#undef fclose
#undef fopen

#define malloc(s)       uc_mem_malloc(s, __FILE__, __LINE__)
#define zmalloc(s)      uc_mem_zmalloc(s, __FILE__, __LINE__)
#define realloc(p, s)   uc_mem_realloc(p, s, __FILE__, __LINE__)
#define calloc(n, s)    uc_mem_calloc(n, s, __FILE__, __LINE__)
#define free(p)         uc_mem_free(p)
#define strdup(str)     uc_mem_strdup(str, __FILE__, __LINE__)
#define ustrdup(str)    uc_mem_ustrdup(str, __FILE__, __LINE__)

#define fifo_open(n, m) uc_leak_fifo_open(n, m, __FILE__, __LINE__)
#define fifo_create(b)  uc_leak_fifo_create(b, __FILE__, __LINE__)
#define fifo_destroy(f) uc_leak_fifo_destroy(f, __FILE__, __LINE__)
#define fifo_close(f)   uc_leak_fifo_close(f, __FILE__, __LINE__)

#define fclose(a)       uc_leak_fclose(a)
#define fopen(a, b)     uc_leak_fopen(__FILE__, __LINE__, a, b)

#else
#define debug_atexit()

#endif /* ifdef DEBUG_LEAKS */


#ifdef __cplusplus
}
#endif


#endif /* ifndef _UC_MEM_H_ */
