#ifndef __MEMH__
#define __MEMH__
typedef struct allocstruct
{
	char *buf;
	long size;
	long userid;  //either slot or object
	long block_status;
	long user_requested_size;
    int persistenttype;
	struct allocstruct *next;
	struct allocstruct *prev;
	struct allocstruct *next_adjacent_block;
	struct allocstruct *prev_adjacent_block;
    struct memoryheap *heap;
} ALLOC_STRUCT;
typedef struct memoryheap
{
	char *heap_start;
	long size;
	char *current_heap_ptr;
	long current_size;
	struct memoryheap *nextheap;
	struct memoryheap *prevheap;
    ALLOC_STRUCT *freed_pool;
    ALLOC_STRUCT *freed_pool_tail;
    ALLOC_STRUCT *allocated_pool;
    ALLOC_STRUCT *allocated_pool_tail;
    ALLOC_STRUCT *last_block_in_the_chain;
    ALLOC_STRUCT *block_chain;
    long alloc_count;
    long free_count;
    long adjacent_count;
    long mem_split_count;
    long left_merge_count;
    long right_merge_count;

	long recycle_count;
} MEMORY_HEAP;

#define PERSISTENT_LIFE_TIME              0x01
#define PERSISTENT_NONE                   0x00
#define PERSISTENT_UNTIL_NEXT_TRANSACTION 0x02

#ifdef __cplusplus
extern "C" 
{
#endif
void TuwaFreeMallocHeaps(void *heap_head);
MEMORY_HEAP *InitTuwaMemSys(long heapsize /* in megabytes */);
int ReturnFreedBlocksToHeap(MEMORY_HEAP *heaphead);
void  *TuwaFsMalloc(void *heap_ptr,long requested_size,long id, int persistent);
void  *TuwaFsCalloc(void *heap_ptr,long requested_size,long id, int persistent);
void  TuwaFsFree(void *heap_ptr,void *inptr);
void ClearAllHeaps(void *heap_head);
int GetChainSize(ALLOC_STRUCT *p);
int GetAdjacentChainSize(ALLOC_STRUCT *p);
int GetBlockCount(ALLOC_STRUCT *p);
int GetChainCount(ALLOC_STRUCT *p);
int ReturnedSomeFreedMem(MEMORY_HEAP *heap);
int CheckMemSanity(MEMORY_HEAP *heap);
#ifdef __cplusplus
}
#endif
#endif