/**********************************************************************
 
	Copyright (C) 2003 Hirohisa MORI <joshua@nichibun.ac.jp>
 
	This program is free software; you can redistribute it 
	and/or modify it under the terms of the GLOBALBASE 
	Library General Public License (G-LGPL) as published by 

	http://www.globalbase.org/
 
	This program is distributed in the hope that it will be 
	useful, but WITHOUT ANY WARRANTY; without even the 
	implied warranty of MERCHANTABILITY or FITNESS FOR A 
	PARTICULAR PURPOSE.

**********************************************************************/



#ifndef ___MEMORY_DEBUG_H___
#define ___MEMORY_DEBUG_H___

#include	"machine/include.h"

#define MEMORY_DEBUG

typedef struct mem_free_info {
	struct mem_free_info *	next;
	struct mem_free_info *	time_next;
	unsigned int		ptr;
	char *			file;
	int			line;
} MEM_FREE_INFO;

typedef struct mem_list {
	struct mem_list *	next;
	char *			file;
	int 			line;
	int			size;
	struct mem_list *	max_next;
} MEM_LIST;

typedef struct mem_purge_tbl {
	int 			(*last_access)(int * access_time);
	void			(*purge_mem)();
	struct mem_purge_tbl *	next;
} MEM_PURGE_TBL;


void * xx_d_alloc(int size,char *,int);
void * xx_d_calloc(int size,int el,char *, int);
void xx_d_f_ree(void *,char *,int);
MEM_LIST * get_mem_debug_list(MEM_LIST * m);


void init_d_alloc();
#define d_alloc(s)	xx_d_alloc((s),__FILE__,__LINE__)
void *	d_re_alloc(void * ptr,int size);
#define d_calloc(s,el)	xx_d_calloc((s),(el),__FILE__,__LINE__)
#define d_f_ree(x)	xx_d_f_ree((x),__FILE__,__LINE__)

void _new_mem_hash();
MEM_LIST * _search_mem_list(char * file,int line);
MEM_LIST * _new_mem_list(char * file,int line);
void _delete_free_info(MEM_FREE_INFO * fi);
void _insert_free_info(void * ptr,char * file,int line);
int lm_cmp(MEM_LIST * m1,MEM_LIST * m2);
void check_mem_header(void * ptr);
INTEGER64 get_d_used_memory();
void set_mem_purge(MEM_PURGE_TBL * t);


#endif
