/*
To use dmalloc, define in your make file or build settings
	USE_DMALLOC
and include this header file:
	#include <dmalloc.h>

And add a call to 
	d_check()
before exiting your program.

You DO NOT need to use the d_*() functions in your source, you should use
malloc,free etc as normal it's all done with defines so you don't need to
modify code.

Call
	d_malloc_init(imsg); before calling any other dmalloc functions.



  	d_memory_limit(30000000);	Optional defaults to no limit
	
Currently you need to link in the treading and emsg sams, this is a fault
and will be fixed when I have a moment and figure out how.


*/
#ifndef DONE_DMALLOC
#define DONE_DMALLOC 1

#ifdef malloc
#undef malloc
#endif
#ifdef calloc
#undef calloc
#endif

#ifdef strdup
#undef strdup
#endif

#ifdef realloc
#undef realloc
#endif


#ifdef USE_DMALLOC


#	ifdef MEMORY_LIMIT

		void d_memory_limit(unsigned long size);

		unsigned long d_memory_peak(void);

#	endif

// a default value for dmid,
#ifndef DMID
#define DMID 9
#endif 


//#ifdef USE_DMALLOC_CHECKED
//#define malloc(x) d_malloc_checked(x,__FILE__,__LINE__)
//#else
//#define malloc(x) d_malloc(x,__FILE__,__LINE__)
		// get rid of warnings...

#define malloc(x) d_mallocid(DMID,x,__FILE__,__LINE__)
//#endif

#define calloc(x,y) d_callocid(DMID,x,y,__FILE__,__LINE__)
#define calloc_noleak(x,y) d_callocid(DMID,x,y,"ignore",0)
#define free(x) {d_free(x,__FILE__,__LINE__);x=NULL;}
#define strdup(x) d_strdupid(DMID,x,__FILE__,__LINE__)
#define realloc(x,sz) d_realloc(x,sz,__FILE__,__LINE__)
#define guard_test() d_guard_test(__FILE__,__LINE__)
#define strndup mystrndup


#else


#endif
int d_malloc_list_using(void);
void d_set_abort(void (*fn)(int x));
int d_check_pointer(char *point, char *src, int line);
int dump_show_free(int x);
int d_set_malloc_clean();
int d_total_mb(void);
void hits_report(char *fname);
int dmalloc_ncol(void);
char *mystrndup (const char *s, size_t n);
int simple_stop(void);
void d_mutex_release(void);
int d_mutex_wait(int line);
int d_malloc_set_pad(void);
int dmalloc_set_mutex(int (*cb)(int vm, int isget, int seconds, char *file, int line), int m);
int d_log_big(char *fname);
int cp_enable(void);
void gfree_check(void);
int set_freepc(int x );
int set_padpc(int x );
void track_init(void);

void pad_scan(void);
void d_dead_test(void);
void *d_callocid(int dmid, size_t num, size_t size , char *fname, int line);
void *d_mallocid(int dmid, int sz, char *file, int line);
void d_free(void *x, char *file, int line);
char *d_strdupid(int dmid,const char *s, char *file, int line);
char *real_strdup(const char *s);
int dmalloc_nwaiting(void);
int d_malloc_used(void);
int d_malloc_dodgy(void *p, int len, int showit);
void d_check(void);
int d_freq(void);
int simple_dump(char *fname);
int simple_track(int x);
void d_malloc_check(void);
void *d_realloc(void *x, int sz, char *file, int line);
int d_malloc_set_debug(int x );
void *real_calloc(int num, int size);
void d_free_test(void);
void real_free(void *x);
void *real_malloc(int size);
void *real_realloc(void *x, int sz);
void d_malloc_init(int (*vemsg)(const char * arg_list, ...));
void d_malloc_exit(void);
int d_guard_test(char *file, int line);
int d_free_idle(void);
int d_free_check(void);
unsigned long dmalloc_total(void);
void d_set_malloc_limit(int xmb);

void d_malloc_set_emsg(void (*vemsg)(const char * arg_list, ...));
int dmalloc_hits(void);
int dmalloc_hits_reset(void);

char * d_info(char *info);
void *d_malloc_checked(int sz, char* file, int line);
void d_set_nshow(int n);
void d_dump(void);
void d_malloc_checkone(void *x, char *reason);
int d_free_checklist(void);
void d_set_die(int (*my_die_fn)(char *reason)) ;
int d_free_set_big(int x);
void dmalloc_set_emsg(int (*vemsg)(const char * arg_list, ...));
void dmalloc_set_dump(int xdump_to_stdout, int xdump_to_emsg);
void dmalloc_ignore_leak_do(char *file, int line);
#define dmalloc_ignore_leak() dmalloc_ignore_leak_do(__FILE__,__LINE__)
int d_nused(void);
int nofrag_dump(char *fname);
int d_mutex_wait(int line);
int d_is_smalloc(void);

void simple_free(void *x, char *src, int line);
void *simple_malloc(int n, char *src, int line);
void *simple_realloc(void *x, int sz, char *src, int line);
void *simple_calloc(int num, int size, char *src, int line);
char *simple_strdup(const char *s, char *src, int line);
int nofrag_init(void);
int d_malloc_set_ned(int x);
int d_malloc_set_simple(int x);
void cp_free_later(void);
void cp_check_some(void);
int d_malloc_set_smalloc(int x);
void d_dump_crash(void);

#include "smalloc.h"

#ifndef USE_DMALLOC
#ifndef IS_DMALLOC_C
#define d_realloc(a,b,file,line) realloc(a,b)
#define d_free(a,b,c) free(a)
#define d_strdup(a,file,line) strdup(a)
#define d_malloc(a,file,line) malloc(a)
#define d_calloc(a,b,file,line) calloc(a,b)
#define d_realloc(a,b,file,line) realloc(a,b)

#define real_free(x) free(x)
#define real_malloc(x) malloc(x)
#define real_strdup(x) strdup(x)
#define real_calloc(a,b) calloc(a,b)


#endif 
#endif

#endif
