/** * @file sc_memory.h * @bried メモリ管理モジュールヘッダファイル * @author Nomura Kei * @copyright 2003 - 2022 Nomura Kei */ #ifndef SC_MEMORY_H #define SC_MEMORY_H #include <stdlib.h> #include <stdbool.h> #ifdef __cplusplus extern "C" { #endif /** * 管理メモリ種別を表すための識別マーク。 */ #define SC_MEMORY_MARK_HEAD (0x55AA55A0) #define SC_MEMORY_MARK_MASK (0xFFFFFFFC) typedef enum { SC_MEMORY_DELETED = SC_MEMORY_MARK_HEAD|0x0, //< メモリが解放されている SC_MEMORY_ALLOCATED = SC_MEMORY_MARK_HEAD|0x1, //< メモリが確保されている SC_MEMORY_ALLOCATED_NEW = SC_MEMORY_MARK_HEAD|0x2, //< メモリが new により確保されている SC_MEMORY_ALLOCATED_NEW_ARRAY = SC_MEMORY_MARK_HEAD|0x3, //< メモリが new[] により確保されている } sc_memory_mark; /** * 指定されたメモリ管理用種別マークが正しいかを判定します。 * * @param mark 種別マーク * @return true/false (管理されているメモリ/管理されていないメモリ) */ #define sc_memory_is_valid(mark) ((mark & SC_MEMORY_MARK_MASK) == SC_MEMORY_MARK_HEAD) /** * メモリ管理情報 */ typedef struct sc_memory_ { const char* file; //< メモリを確保したファイル名 int line; //< メモリを確保した行番号 const char* func; //< メモリを確保した関数名 int size; //< サイズ sc_memory_mark _mark; // 確保したメモリの種別マーク struct sc_memory_* _prev; // 前の管理メモリへのポインタ struct sc_memory_* _next; // 後の管理メモリへのポインタ void* data; //< データ領域へのポインタ } sc_memory; // ハンドラ extern void (*sc_memory_ahandler)(sc_memory* meminfo, const char* msg); extern void (*sc_memory_fhandler)(sc_memory* meminfo, const char* msg); extern void (*sc_memory_ehandler)(sc_memory* meminfo, const char* msg); /// 本来の関数 void* sc_raw_malloc ( size_t size); void* sc_raw_calloc (size_t nmemb, size_t size); void* sc_raw_realloc(void* ptr, size_t size); void sc_raw_free (void* ptr); // メモリ管理用関数 void* sc_malloc ( size_t size, const char* file, int line, const char* func); void* sc_calloc (size_t nmemb, size_t size, const char* file, int line, const char* func); void* sc_realloc(void* ptr, size_t size, const char* file, int line, const char* func); void sc_free (void* ptr); bool sc_memory_entries(void (*callback)(sc_memory* entry)); bool sc_memory_freeif(bool (*callback)(sc_memory* entry)); void sc_memory_dump_entry(sc_memory* entry); void* sc_memory_allocate(void* ptr, size_t size, sc_memory_mark mark, const char* file, int line, const char* func); void sc_memory_free(void* ptr); const char* sc_memory_markstr(sc_memory_mark mark); #ifdef SC_MEMORY_MANAGE #define malloc( size) sc_malloc( size, __FILE__, __LINE__, __func__) #define calloc(nmemb, size) sc_calloc( nmemb, size, __FILE__, __LINE__, __func__) #define realloc(ptr, size) sc_realloc(ptr , size, __FILE__, __LINE__, __func__) #define free(ptr) sc_free(ptr) #endif // SC_MEMORY_MANAGE #ifdef __cplusplus } #endif #endif // SC_MEMORY_H