/*
 * $Id: m_malloc.h,v 1.1 2003/06/06 00:15:12 tong Exp $
 *
 * ゼルダ用ヒープ関数群
 * ゲーム中にヒープを初期化しながら使用する
 * 
 * ・使用する前に必ず zelda_InitArena で初期化してください
 * ・zelda_malloc した領域 zelda_free で解放してください。
 * ・ヒープ領域を解放(再初期化)したときは,それ以前に zelda_malloc した領域を
 *   使用してはいけません。
 *
 *
 */

#ifndef __M_MALLOC_H_
#define __M_MALLOC_H_

#if defined(_LANGUAGE_C_PLUS_PLUS)
extern "C" {
#endif
#if 0
}
#endif

#include "m_types.h"





/*
 * size_t 型
 */
#if !defined(_SIZE_T) && !defined(_SIZE_T_)
#define _SIZE_T
#if (_MIPS_SZLONG == 32)
typedef unint 	size_t;
#endif
#if (_MIPS_SZLONG == 64)
typedef unlong 	size_t;
#endif
#endif

/*
 * ヒープ初期化関数
 * zelda_mallocなどの関数を使う前に必ず初期化を必要とします
 *
 * base ヒープ領域先頭ポインタ
 * len  ヒープ領域バイトサイズ
 * base, len ともに16の倍数にしてください
 */
extern void zelda_InitArena(void *base, size_t len);
extern void zelda_CleanupArena(void);

/*
 * zelda_MallocIsInitalized ヒープが初期化されているか？
 * zelda_GetFreeArena ヒープの空きサイズの調査
 * zelda_CheckArena   ヒープの整合性調査
 */
extern int  zelda_MallocIsInitalized(void);
extern void zelda_GetFreeArena(size_t *max_size, size_t *free_size, size_t *alloc_size);
extern int  zelda_CheckArena(void);

/*
 * zelda_malloc
 * zelda_malloc_r (逆malloc)
 * zelda_realloc
 * zelda_free
 * zelda_calloc
 *
 * それぞれ、標準関数の malloc, malloc_r, realloc, free, calloc に相当します。
 */
#if defined(M_MALLOC_C) || !DEBUG
extern void *zelda_malloc(size_t size);
extern void *zelda_malloc_r(size_t size);
extern void *zelda_realloc(void *ptr, size_t size);
extern void zelda_free(void *ptr);
extern void *zelda_calloc(size_t nelem, size_t elsize);
#endif
#if defined(M_MALLOC_C) || DEBUG
extern void *zelda_malloc_DEBUG(size_t size, const char *file, int line);
extern void *zelda_malloc_r_DEBUG(size_t size, const char *file, int line);
extern void *zelda_realloc_DEBUG(void *ptr, size_t size, const char *file, int line);
extern void zelda_free_DEBUG(void *ptr, const char *file, int line);
#endif

#if !defined(__M_MALLOC_C_)
#if !DEBUG
#define zelda_malloc_DEBUG(size, file, line) \
	zelda_malloc(size)
#define zelda_malloc_r_DEBUG(size, file, line) \
	zelda_malloc_r(size)
#define zelda_realloc_DEBUG(ptr, size, file, line) \
	zelda_realloc(ptr, size)
#define zelda_free_DEBUG(ptr, file, line) \
	zelda_free(ptr)
#else
#define zelda_malloc(size) \
	zelda_malloc_DEBUG(size, __FILE__, __LINE__)
#define zelda_malloc_r(size) \
	zelda_malloc_r_DEBUG(size, __FILE__, __LINE__)
#define zelda_realloc(ptr, size) \
	zelda_realloc_DEBUG(ptr, size, __FILE__, __LINE__)
#define zelda_free(ptr) \
	zelda_free_DEBUG(ptr, __FILE__, __LINE__)
#endif
#endif /* __M_MALLOC_C_ */

#if DEBUG
/*
 * zelda_DisplayArena ヒープ内容の表示
 */
extern void zelda_DisplayArena(void);


#endif /* DEBUG */
#if 0
extern void zelda_DisplayArena_OnFault(void);
#endif
extern int zelda_malloc_verbose;	/* 0:なし 1:エラー 2:警告 3:情報 */

#if 0
{
#endif
#if defined(_LANGUAGE_C_PLUS_PLUS)
} /* extern "C" */
#endif

#endif /* __M_MALLOC_H_ */
