134baa81c66940ca6fc5941bf2bcdd3d0be73718
[YACASL2.git] / include / cmem.h
1 #ifndef YACASL2_CMEM_H_INCLUDED
2 #define YACASL2_CMEM_H_INCLUDED
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <assert.h>
8 #include <stdbool.h>
9
10 /**
11  * @brief 配列のサイズを返すマクロ
12  */
13 #ifndef ARRAYSIZE
14 #define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0]))
15 #endif
16
17 /**
18  * @brief メモリを解放するマクロ
19  */
20 #ifndef FREE
21 #define FREE(ptr) {free(ptr); ptr = NULL;}
22 #endif
23
24 /**
25  * @brief mallocを実行し、0で初期化する
26  *
27  * メモリを確保できない場合はエラーを出力して終了
28  *
29  * @return なし
30  *
31  * @param size メモリーのサイズ
32  * @param tag エラーメッセージなどで表示されるタグ
33  */
34 void *malloc_chk(size_t size, const char *tag);
35
36 /**
37  * @brief 領域の数とサイズを指定してメモリーを確保するcallocを実行する
38  *
39  * メモリを確保できない場合はエラーを出力して終了
40  *
41  * @return なし
42  *
43  * @param nmemb 領域の数
44  * @param size 領域1個あたりのメモリーサイズ
45  * @param tag エラーメッセージなどで表示されるタグ
46  */
47 void *calloc_chk(size_t nmemb, size_t size, const char *tag);
48
49 /**
50  * @brief malloc_chkを実行してメモリを確保し、コピーした文字列を返す
51  *
52  * @return コピーした文字列
53  *
54  * @param s 文字列
55  * @param tag エラーメッセージなどで表示されるタグ
56  */
57 char *strdup_chk(const char *s, const char *tag);
58
59 /**
60  * @brief malloc_chkを実行してメモリを確保し、コピーした文字列の指定した長さの部分を返す
61  *
62  * @return コピーした文字列
63  *
64  * @param s 文字列
65  * @param len 文字列の長さ
66  * @param tag エラーメッセージなどで表示されるタグ
67  */
68 char *strndup_chk(const char *s, size_t len, const char *tag);
69
70 /**
71  * @brief 文字列の末尾から、改行と空白とタブを削除する
72  *
73  * @return 末尾から改行と空白とタブを削除した文字列
74  *
75  * @param s 文字列
76  */
77 char *strip_end(char *s);
78 #endif