X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=include%2Fcmem.h;h=134baa81c66940ca6fc5941bf2bcdd3d0be73718;hp=50a29ec819503cb4651f0c4cf6ed5476bffda759;hb=1474b863964dc39064d70b7204a2c60ec9bffe50;hpb=bf31ea99380e3ee2198f61f6510ebe6e4a3961f5 diff --git a/include/cmem.h b/include/cmem.h index 50a29ec..134baa8 100644 --- a/include/cmem.h +++ b/include/cmem.h @@ -7,22 +7,72 @@ #include #include +/** + * @brief 配列のサイズを返すマクロ + */ #ifndef ARRAYSIZE #define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0])) #endif -/* mallocを実行し、0で初期化 */ -/* メモリを確保できない場合はエラーを出力して終了 */ -void *malloc_chk(size_t size, char *tag); +/** + * @brief メモリを解放するマクロ + */ +#ifndef FREE +#define FREE(ptr) {free(ptr); ptr = NULL;} +#endif + +/** + * @brief mallocを実行し、0で初期化する + * + * メモリを確保できない場合はエラーを出力して終了 + * + * @return なし + * + * @param size メモリーのサイズ + * @param tag エラーメッセージなどで表示されるタグ + */ +void *malloc_chk(size_t size, const char *tag); + +/** + * @brief 領域の数とサイズを指定してメモリーを確保するcallocを実行する + * + * メモリを確保できない場合はエラーを出力して終了 + * + * @return なし + * + * @param nmemb 領域の数 + * @param size 領域1個あたりのメモリーサイズ + * @param tag エラーメッセージなどで表示されるタグ + */ +void *calloc_chk(size_t nmemb, size_t size, const char *tag); -/* callocを実行 */ -/* メモリを確保できない場合はエラーを出力して終了 */ -void *calloc_chk(size_t nmemb, size_t size, char *tag); +/** + * @brief malloc_chkを実行してメモリを確保し、コピーした文字列を返す + * + * @return コピーした文字列 + * + * @param s 文字列 + * @param tag エラーメッセージなどで表示されるタグ + */ +char *strdup_chk(const char *s, const char *tag); -/* malloc_chkを実行してメモリを確保してから、 */ -/* コピーした文字列を返す */ -char *strdup_chk(const char *s, char *tag); +/** + * @brief malloc_chkを実行してメモリを確保し、コピーした文字列の指定した長さの部分を返す + * + * @return コピーした文字列 + * + * @param s 文字列 + * @param len 文字列の長さ + * @param tag エラーメッセージなどで表示されるタグ + */ +char *strndup_chk(const char *s, size_t len, const char *tag); -/* メモリがNULLの場合は解放 */ -void free_chk(void *ptr, char *tag); +/** + * @brief 文字列の末尾から、改行と空白とタブを削除する + * + * @return 末尾から改行と空白とタブを削除した文字列 + * + * @param s 文字列 + */ +char *strip_end(char *s); #endif