picox  0.1
xstdlib.h
[詳解]
1 
14 /*
15  * License: MIT license
16  * Copyright (c) <2015> <MaskedW [maskedw00@gmail.com]>
17  *
18  * Permission is hereby granted, free of charge, to any person
19  * obtaining a copy of this software and associated documentation
20  * files (the "Software"), to deal in the Software without
21  * restriction, including without limitation the rights to use, copy,
22  * modify, merge, publish, distribute, sublicense, and/or sell copies
23  * of the Software, and to permit persons to whom the Software is
24  * furnished to do so, subject to the following conditions:
25  *
26  * The above copyright notice and this permission notice shall be
27  * included in all copies or substantial portions of the Software.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
33  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
34  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
35  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36  * SOFTWARE.
37  */
38 
39 
40 #ifndef picox_core_detail_xstdlib_h_
41 #define picox_core_detail_xstdlib_h_
42 
43 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 
59 static inline void* x_malloc(size_t size)
60 {
61  if (size == 0)
62  return NULL;
63  void* const ptr = X_CONF_MALLOC(size);
65  X_UNUSED(ptr);
66 
67  return ptr;
68 }
69 
70 
73 static inline void x_free(void* ptr)
74 {
75  /* 規格上、freeにNULLを渡してもOKなはずなのだが、実装によっては微妙にグレー
76  * だったりするのでここでもNULLチェックしておく
77  */
78  if (ptr)
79  X_CONF_FREE(ptr);
80 }
81 
82 
85 static inline void* x_calloc(size_t nmemb, size_t size)
86 {
87  size = nmemb * size;
88  void* const ptr = x_malloc(size);
89  if (ptr)
90  memset(ptr, 0, size);
91  return ptr;
92 }
93 
94 
102 static inline void* x_calloc2(size_t size)
103 {
104  void* const ptr = x_malloc(size);
105  if (ptr)
106  memset(ptr, 0, size);
107  return ptr;
108 }
109 
110 
113 static inline void* x_realloc(void *old_mem, size_t size)
114 {
115  void* const new_mem = x_malloc(size);
116  if (!new_mem)
117  return NULL;
118 
119  if (!old_mem)
120  return new_mem;
121 
122  memcpy(new_mem, old_mem, size);
123  x_free(old_mem);
124 
125  return new_mem;
126 }
127 
128 
138 static inline void* x_realloc2(void *old_mem, size_t old_size, size_t new_size)
139 {
140  if (old_size == new_size)
141  return old_mem;
142 
143  void* const new_mem = x_malloc(new_size);
144  if (!new_mem)
145  return NULL;
146 
147  if (!old_mem)
148  return new_mem;
149 
150  /* 新しいサイズより旧いサイズの方が大きかったら新しいサイズ分コピーする。
151  * 古いサイズより新しいサイズの方が大きかったら古いサイズ分コピーする。
152  */
153  if (old_size > new_size)
154  memcpy(new_mem, old_mem, new_size);
155  else
156  memcpy(new_mem, old_mem, old_size);
157 
158  x_free(old_mem);
159 
160  return new_mem;
161 }
162 
163 
170 #define X_SAFE_FREE(ptr) (x_free((ptr)), (ptr) = NULL)
171 
172 
173 #ifdef __cplusplus
174 }
175 #endif
176 
177 
183 #endif // picox_core_detail_xstdlib_h_
static void * x_realloc2(void *old_mem, size_t old_size, size_t new_size)
old_memが指すold_sizeバイトのメモリブロックをnew_sizeバイトに再割当てして返します ...
Definition: xstdlib.h:138
static void * x_calloc(size_t nmemb, size_t size)
sizeバイトの要素nmemb個からなる配列にメモリを割り当て0初期化して返します
Definition: xstdlib.h:85
static void * x_realloc(void *old_mem, size_t size)
old_memが指すメモリブロックをsizeバイトに再割当てして返します
Definition: xstdlib.h:113
static void x_free(void *ptr)
ptrが指すメモリ空間を開放します
Definition: xstdlib.h:73
#define X_UNUSED(x)
コンパイラによる未使用変数の警告を抑制するマクロです。
Definition: xutils.h:161
static void * x_malloc(size_t size)
sizeバイトのメモリを割り当てて返します
Definition: xstdlib.h:59
#define X_CONF_MALLOC(size)
動的メモリ確保関数を設定します。未指定時はmalloc()が使用されます。
Definition: xconfig.h:143
#define X_CONF_FREE(ptr)
メモリ解放関数を設定します。未指定時はfree()が使用されます。
Definition: xconfig.h:151
static void * x_calloc2(size_t size)
sizeバイトのメモリを割り当て、0初期化して返します
Definition: xstdlib.h:102
#define X_ASSERT_MALLOC_NULL(expr)
動的メモリ確保のNULL検査用アサートです
Definition: xdebug.h:308