picox  0.1
xstddef.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 self 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 self 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 #ifndef picox_core_detail_xstddef_h_
40 #define picox_core_detail_xstddef_h_
41 
42 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif /* __cplusplus */
54 
55 
58 #define X_COUNT_OF(a) (sizeof(a) / sizeof(*(a)))
59 
60 
63 #define X_COUNT_OF_ROW(x) (sizeof(x) / sizeof(x[0]))
64 
65 
68 #define X_COUNT_OF_COL(x) (sizeof(x[0]) / sizeof(x[0][0]))
69 
70 
73 #define X_COUNT_OF_2D(x) (X_COUNT_OF_ROW(x) * X_COUNT_OF_COL(x))
74 
75 
78 #define X_SIZEOF_MEM(s, m) (sizeof(((s*)0)->m))
79 
80 
83 #define X_OFFSET_OF(s, m) ((uintptr_t)&(((s *)0)->m))
84 
85 
88 #define X_ALIGN_OF(type) X_OFFSET_OF(struct { char c; type member; }, member)
89 
90 
115 #if (X_HAS_TYPEOF != 0) && (X_HAS_STATEMENT_EXPRESSIONS != 0)
116  #define X_CONTAINER_OF(ptr, type, member) \
117  ({ \
118  const X_TYPEOF(((type*)0)->member)* mptr = (ptr); \
119  (type*)((char*)mptr - X_OFFSET_OF(type,member) ); \
120  })
121 #else
122  #define X_CONTAINER_OF(ptr, type, member) \
123  ((type*) ((char*)(ptr) - X_OFFSET_OF(type, member)))
124 #endif
125 
126 
128 #define X_STATIC_ASSERT_CAT_(a, b) a ## b
129 
131 
134 #define X_STATIC_ASSERT(cond) \
135  enum { X_STATIC_ASSERT_CAT_(X_STATIC_ASSERTION_FAILED, __LINE__) = \
136  sizeof( struct { int assertion_failed[(cond) ? 1: -1];})}
137 
138 
152 #define X_SIZEOF_CHAR (1)
153 #define X_SIZEOF_SHORT (2)
154 
155 #if UINT_MAX == 0xFF
156  #define X_SIZEOF_INT (1)
157 #elif UINT_MAX == 0xFFFF
158  #define X_SIZEOF_INT (2)
159 #elif UINT_MAX == 0xFFFFFFFF
160  #define X_SIZEOF_INT (4)
161 #elif UINT_MAX == 0xFFFFFFFFFFFFFFFF
162  #define X_SIZEOF_INT (8)
163 #else
164  #error unspported platform
165 #endif
166 
167 #if ULONG_MAX == 0xFFFFFFFF
168  #define X_SIZEOF_LONG (4)
169 #elif ULONG_MAX == 0xFFFFFFFFFFFFFFFF
170  #define X_SIZEOF_LONG (8)
171 #else
172  #error unspported platform
173 #endif
174 
175 #if UINTPTR_MAX == 0xFFFF
176  #define X_SIZEOF_INTPTR (2)
177 #elif UINTPTR_MAX == 0xFFFFFFFF
178  #define X_SIZEOF_INTPTR (4)
179 #elif UINTPTR_MAX == 0xFFFFFFFFFFFFFFFF
180  #define X_SIZEOF_INTPTR (8)
181 #else
182  #error unspported platform
183 #endif
184 
185 #if SIZE_MAX == 0xFFFF
186  #define X_SIZEOF_SIZE (2)
187 #elif SIZE_MAX == 0xFFFFFFFF
188  #define X_SIZEOF_SIZE (4)
189 #elif SIZE_MAX == 0xFFFFFFFFFFFFFFFF
190  #define X_SIZEOF_SIZE (8)
191 #else
192  #error unspported platform
193 #endif
194 
195 
204 #define X_MSBOF_CHAR (X_BIT(CHAR_BIT))
205 #define X_MSBOF_SHORT (USHORT_MAX & (~(USHORT_MAX >> 1)))
206 #define X_MSBOF_INT (UINT_MAX & (~(UINT_MAX >> 1)))
207 #define X_MSBOF_LONG (ULONG_MAX & (~(ULONG_MAX >> 1)))
208 #define X_MSBOF_INTPTR (UINTPTR_MAX & (~(UINTPTR_MAX >> 1)))
209 #define X_MSBOF_SIZE (SIZE_MAX & (~(SIZE_MAX >> 1)))
210 #define X_MSBOF_INT8 (1U << 7)
211 #define X_MSBOF_INT16 (1U << 15)
212 #define X_MSBOF_INT32 (X_UINT32_C(1) << 31)
213 
214 
225 #if X_SIZEOF_INT < 4
226  #define X_INT32_C(c) c ## L
227  #define X_UINT32_C(c) c ## UL
228 #else
229  #define X_INT32_C(c) c
230  #define X_UINT32_C(c) c
231 #endif
232 
233 #if X_SIZEOF_LONG < 8
234  #define X_INT64_C(c) c ## LL
235  #define X_UINT64_C(c) c ## ULL
236 #else
237  #define X_INT64_C(c) c ## L
238  #define X_UINT64_C(c) c ## UL
239 #endif
240 
254 typedef uint32_t XSize;
255 
259 typedef int32_t XSSize;
260 
264 typedef int32_t XOffset;
265 
266 
272 typedef uint32_t XTag;
273 
274 
281 #define X_MAKE_TAG(a,b,c,d) \
282  ( ((uint32_t)(a) << 24) \
283  | ((uint32_t)(b) << 16) \
284  | ((uint32_t)(c) << 8) \
285  | ((uint32_t)(d) << 0) )
286 
290 typedef uint32_t XMode;
291 
295 typedef uint32_t XBits;
297 
299 typedef int (*XCompareFunc)(const void* a, const void* b);
301 
303 typedef void* (*XMallocFunc)(size_t size);
305 
307 typedef void (*XFreeFunc)(void* ptr);
308 
309 
316 typedef void (*XDeleter)(void* ptr);
318 
320 static inline void x_null_deleter(void* ptr)
321 {
322  (void)(ptr);
323 }
324 
328 typedef union XMaxAlign
329 {
330  char c; int i; long l; long long ll; void* p;
331  float f; double d; long double ld; void(*fp)(void);
332 } XMaxAlign;
333 
337 #define X_DEF_MAX_ALIGNED(name, size) \
338  XMaxAlign name[((size) + sizeof(XMaxAlign) - 1) / sizeof(XMaxAlign)]
339 
343 typedef enum
344 {
346  X_SEEK_SET = SEEK_SET,
348  X_SEEK_CUR = SEEK_CUR,
350  X_SEEK_END = SEEK_END
351 } XSeekMode;
352 
353 
370 typedef enum
371 {
372  X_ERR_NONE = 0,
373  X_ERR_IO = 1,
388  X_ERR_MANY = 16,
389  X_ERR_RANGE = 17,
397  X_ERR_INTERNAL = 25,
398  X_ERR_OTHER = 26,
399  X_ERR_UNKNOWN = 27,
400 } XError;
401 
402 
408 typedef enum
409 {
410  X_OPEN_FLAG_READ = (1 << 0),
411  X_OPEN_FLAG_WRITE = (1 << 1),
412  X_OPEN_FLAG_READ_WRITE = X_OPEN_FLAG_READ | X_OPEN_FLAG_WRITE,
413  X_OPEN_FLAG_APPEND = (1 << 2),
414  X_OPEN_FLAG_TRUNCATE = (1 << 3),
415 } XOpenFlag;
416 
417 
438 typedef enum
439 {
444  X_OPEN_MODE_READ = X_OPEN_FLAG_READ,
445 
452 
460 
466 
467 
474 
475 
483  X_OPEN_MODE_UNKNOWN = 0xFF,
484 } XOpenMode;
485 
489 #define X_PATH_MAX (X_CONF_FILE_PATH_MAX)
490 
494 #define X_NAME_MAX (X_CONF_FILE_NAME_MAX)
495 
499 #define X_LINE_MAX (X_CONF_LINE_MAX)
500 
504 #define X_XFS_TYPE (X_CONF_XFS_TYPE)
505 
506 
507 #ifdef __cplusplus
508 }
509 #endif /* __cplusplus */
510 
511 
517 #endif /* picox_core_detail_xstddef_h_ */
Definition: xstddef.h:378
Definition: xstddef.h:385
Definition: xstddef.h:382
Definition: xstddef.h:396
Definition: xstddef.h:376
Definition: xstddef.h:384
XOpenMode
ファイルオープン等のモードです
Definition: xstddef.h:435
Definition: xstddef.h:372
XError
errnoの代替として使用する共通のエラーコードを表す列挙型です
Definition: xstddef.h:367
Definition: xstddef.h:371
Definition: xstddef.h:383
Definition: xstddef.h:380
static void x_null_deleter(void *ptr)
何もしないデリータです
Definition: xstddef.h:317
int(* XCompareFunc)(const void *a, const void *b)
qsort()互換の比較関数ポインタ型です
Definition: xstddef.h:296
uint32_t XBits
XModeと同じくビットフラグを格納しますが、よりビットを意識した型です
Definition: xstddef.h:292
uint32_t XSize
何らかの大きさを表すのに十分なサイズを備えた符号なし整数型です
Definition: xstddef.h:251
Definition: xstddef.h:387
ストリームサイズの末尾
Definition: xstddef.h:347
Definition: xstddef.h:386
"r" 読み込み
Definition: xstddef.h:441
Definition: xstddef.h:390
Definition: xstddef.h:395
Definition: xstddef.h:392
Definition: xstddef.h:375
"r+" 読み書き
Definition: xstddef.h:462
ストリームの先頭
Definition: xstddef.h:343
組込み型の最大アライメント型です。
Definition: xstddef.h:325
uint32_t XTag
何らかのIDを格納するための符号なし整数が型です
Definition: xstddef.h:269
Definition: xstddef.h:393
"w" 上書き書き込み
Definition: xstddef.h:448
Definition: xstddef.h:410
int32_t XSSize
XSizeの符号あり版です。size_tに対するssize_tと同じ役割です
Definition: xstddef.h:256
XOpenFlag
ファイルオープン等に使用するフラグです
Definition: xstddef.h:405
Definition: xstddef.h:388
void(* XFreeFunc)(void *ptr)
free()互換のメモリ開放関数ポインタ型です
Definition: xstddef.h:304
Definition: xstddef.h:374
"w+" 上書き読み書き
Definition: xstddef.h:470
ストリームポインタの現在地
Definition: xstddef.h:345
"a+" 追記読み書き
Definition: xstddef.h:479
Definition: xstddef.h:389
Definition: xstddef.h:381
int32_t XOffset
何らかのオフセットを表すのに十分な大きさをもった符号あり整数型です
Definition: xstddef.h:261
Definition: xstddef.h:411
Definition: xstddef.h:370
Definition: xstddef.h:409
Definition: xstddef.h:377
uint32_t XMode
何らかのビットフラグを格納することを意図した型です
Definition: xstddef.h:287
XSeekMode
シークの起点を指定する列挙型です
Definition: xstddef.h:340
Definition: xstddef.h:391
Definition: xstddef.h:379
void(* XDeleter)(void *ptr)
オブジェクトを削除する関数ポインタ型です
Definition: xstddef.h:313
Definition: xstddef.h:408
Definition: xstddef.h:394
"a" 追記書き込み
Definition: xstddef.h:456
Definition: xstddef.h:373