nlib
Platform.h
[詳解]
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_PLATFORM_H_
4 #define INCLUDE_NN_NLIB_PLATFORM_H_
5 
6 #ifdef __ILP64__
7 # error Sorry, ILP64 not supported
8 #endif
9 
10 // Note that the C preprocessor # operator must be able to use.
11 #define NLIB_VERSION_YEAR 2015
12 #define NLIB_VERSION_YEAR_SHORT 15
13 #define NLIB_VERSION_DATE 0908
14 #define NLIB_VERSION 20150908
15 
16 #ifdef __cplusplus
17 # ifndef __STDC_LIMIT_MACROS
18 # ifdef _MSC_VER
19 # pragma message(__FILE__ ": __STDC_LIMIT_MACROS not defined, compile may fail")
20 # elif !defined(NN_PLATFORM_CTR) && !defined(CAFE)
21 # warning __STDC_LIMIT_MACROS not defined, compile may fail
22 # endif
23 # define __STDC_LIMIT_MACROS
24 # endif
25 # ifndef __STDC_CONSTANT_MACROS
26 # ifdef _MSC_VER
27 # pragma message(__FILE__ ": __STDC_CONSTANT_MACROS not defined, compile may fail")
28 # elif !defined(NN_PLATFORM_CTR) && !defined(CAFE)
29 # warning __STDC_CONSTANT_MACROS not defined, compile may fail
30 # endif
31 # define __STDC_CONSTANT_MACROS
32 # endif
33 # ifndef __STDC_FORMAT_MACROS
34 # ifdef _MSC_VER
35 # pragma message(__FILE__ ": __STDC_FORMAT_MACROS not defined, compile may fail")
36 # elif !defined(NN_PLATFORM_CTR) && !defined(CAFE)
37 # warning __STDC_FORMAT_MACROS not defined, compile may fail
38 # endif
39 # define __STDC_FORMAT_MACROS
40 # endif
41 #endif
42 
43 #include <stddef.h> // for size_t
44 #include <stdio.h> // for SEEK_CUR, SEEK_END, SEEK_SET
45 #include <stdarg.h> // for va_list
46 #include <string.h> // for memcpy, memmove, memset
47 
48 #if defined(NLIB_RENAME_CAPI) && !defined(_MSC_VER)
49 #ifndef NLIB_CAPI
50 # define NLIB_CAPI(x) mypfx##x
51 #endif
52 
53 #define nlib_error_string NLIB_CAPI(nlib_error_string)
54 #define nlib_get_native_last_error NLIB_CAPI(nlib_get_native_last_error)
55 #define nlib_getversion NLIB_CAPI(nlib_getversion)
56 #define nlib_epochtime NLIB_CAPI(nlib_epochtime)
57 #define nlib_ticktime NLIB_CAPI(nlib_ticktime)
58 #define nlib_sleep NLIB_CAPI(nlib_sleep)
59 #define nlib_gen_random NLIB_CAPI(nlib_gen_random)
60 #define nlib_mempagesize NLIB_CAPI(nlib_mempagesize)
61 #define nlib_virtual_alloc NLIB_CAPI(nlib_virtual_alloc)
62 #define nlib_virtual_free NLIB_CAPI(nlib_virtual_free)
63 #define nlib_physical_alloc NLIB_CAPI(nlib_physical_alloc)
64 #define nlib_physical_free NLIB_CAPI(nlib_physical_free)
65 #define nlib_mlock NLIB_CAPI(nlib_mlock)
66 #define nlib_munlock NLIB_CAPI(nlib_munlock)
67 #define nlib_tls_alloc NLIB_CAPI(nlib_tls_alloc)
68 #define nlib_tls_free NLIB_CAPI(nlib_tls_free)
69 #define nlib_tls_setvalue NLIB_CAPI(nlib_tls_setvalue)
70 #define nlib_tls_getvalue NLIB_CAPI(nlib_tls_getvalue)
71 #define nlib_mutex_init NLIB_CAPI(nlib_mutex_init)
72 #define nlib_mutex_recursive_init NLIB_CAPI(nlib_mutex_recursive_init)
73 #define nlib_mutex_recursive_timed_init NLIB_CAPI(nlib_mutex_recursive_timed_init)
74 #define nlib_mutex_lock NLIB_CAPI(nlib_mutex_lock)
75 #define nlib_mutex_trylock NLIB_CAPI(nlib_mutex_trylock)
76 #define nlib_mutex_trylock_for NLIB_CAPI(nlib_mutex_trylock_for)
77 #define nlib_mutex_unlock NLIB_CAPI(nlib_mutex_unlock)
78 #define nlib_mutex_destroy NLIB_CAPI(nlib_mutex_destroy)
79 #define nlib_semaphore_init NLIB_CAPI(nlib_semaphore_init)
80 #define nlib_semaphore_wait NLIB_CAPI(nlib_semaphore_wait)
81 #define nlib_semaphore_trywait NLIB_CAPI(nlib_semaphore_trywait)
82 #define nlib_semaphore_trywait_for NLIB_CAPI(nlib_semaphore_trywait_for)
83 #define nlib_semaphore_post NLIB_CAPI(nlib_semaphore_post)
84 #define nlib_semaphore_post_ex NLIB_CAPI(nlib_semaphore_post_ex)
85 #define nlib_semaphore_destroy NLIB_CAPI(nlib_semaphore_destroy)
86 #define nlib_cond_init NLIB_CAPI(nlib_cond_init)
87 #define nlib_cond_signal NLIB_CAPI(nlib_cond_signal)
88 #define nlib_cond_broadcast NLIB_CAPI(nlib_cond_broadcast)
89 #define nlib_cond_wait NLIB_CAPI(nlib_cond_wait)
90 #define nlib_cond_wait_for NLIB_CAPI(nlib_cond_wait_for)
91 #define nlib_cond_wait_until NLIB_CAPI(nlib_cond_wait_until)
92 #define nlib_cond_destroy NLIB_CAPI(nlib_cond_destroy)
93 #define nlib_rwlock_init NLIB_CAPI(nlib_rwlock_init)
94 #define nlib_rwlock_destroy NLIB_CAPI(nlib_rwlock_destroy)
95 #define nlib_rwlock_rdlock NLIB_CAPI(nlib_rwlock_rdlock)
96 #define nlib_rwlock_tryrdlock NLIB_CAPI(nlib_rwlock_tryrdlock)
97 #define nlib_rwlock_tryrdlock_for NLIB_CAPI(nlib_rwlock_tryrdlock_for)
98 #define nlib_rwlock_tryrdlock_until NLIB_CAPI(nlib_rwlock_tryrdlock_until)
99 #define nlib_rwlock_rdunlock NLIB_CAPI(nlib_rwlock_rdunlock)
100 #define nlib_rwlock_wrlock NLIB_CAPI(nlib_rwlock_wrlock)
101 #define nlib_rwlock_trywrlock NLIB_CAPI(nlib_rwlock_trywrlock)
102 #define nlib_rwlock_trywrlock_for NLIB_CAPI(nlib_rwlock_trywrlock_for)
103 #define nlib_rwlock_trywrlock_until NLIB_CAPI(nlib_rwlock_trywrlock_until)
104 #define nlib_rwlock_wrunlock NLIB_CAPI(nlib_rwlock_wrunlock)
105 #define nlib_condrwlock_init NLIB_CAPI(nlib_condrwlock_init)
106 #define nlib_condrwlock_destroy NLIB_CAPI(nlib_condrwlock_destroy)
107 #define nlib_condrwlock_signal NLIB_CAPI(nlib_condrwlock_signal)
108 #define nlib_condrwlock_broadcast NLIB_CAPI(nlib_condrwlock_broadcast)
109 #define nlib_condrwlock_wait NLIB_CAPI(nlib_condrwlock_wait)
110 #define nlib_condrwlock_wait_for NLIB_CAPI(nlib_condrwlock_wait_for)
111 #define nlib_condrwlock_wait_until NLIB_CAPI(nlib_condrwlock_wait_until)
112 #define nlib_barrier_init NLIB_CAPI(nlib_barrier_init)
113 #define nlib_barrier_destroy NLIB_CAPI(nlib_barrier_destroy)
114 #define nlib_barrier_wait NLIB_CAPI(nlib_barrier_wait)
115 #define nlib_once NLIB_CAPI(nlib_once)
116 #define nlib_tryonce NLIB_CAPI(nlib_tryonce)
117 #define nlib_mq_open NLIB_CAPI(nlib_mq_open)
118 #define nlib_mq_getattr NLIB_CAPI(nlib_mq_getattr)
119 #define nlib_mq_close NLIB_CAPI(nlib_mq_close)
120 #define nlib_mq_readonly NLIB_CAPI(nlib_mq_readonly)
121 #define nlib_mq_send NLIB_CAPI(nlib_mq_send)
122 #define nlib_mq_send_until NLIB_CAPI(nlib_mq_send_until)
123 #define nlib_mq_receive NLIB_CAPI(nlib_mq_receive)
124 #define nlib_mq_receive_until NLIB_CAPI(nlib_mq_receive_until)
125 #define nlib_mq_drop NLIB_CAPI(nlib_mq_drop)
126 #define nlib_yield NLIB_CAPI(nlib_yield)
127 #define nlib_thread_create NLIB_CAPI(nlib_thread_create)
128 #define nlib_thread_join NLIB_CAPI(nlib_thread_join)
129 #define nlib_thread_detach NLIB_CAPI(nlib_thread_detach)
130 #define nlib_thread_self NLIB_CAPI(nlib_thread_self)
131 #define nlib_thread_getconcurrency NLIB_CAPI(nlib_thread_getconcurrency)
132 #define nlib_thread_getid NLIB_CAPI(nlib_thread_getid)
133 #define nlib_thread_equal NLIB_CAPI(nlib_thread_equal)
134 #define nlib_thread_getcpu NLIB_CAPI(nlib_thread_getcpu)
135 #define nlib_thread_setaffinity NLIB_CAPI(nlib_thread_setaffinity)
136 #define nlib_thread_setname NLIB_CAPI(nlib_thread_setname)
137 #define nlib_thread_attr_init NLIB_CAPI(nlib_thread_attr_init)
138 #define nlib_thread_attr_setint NLIB_CAPI(nlib_thread_attr_setint)
139 #define nlib_thread_attr_getint NLIB_CAPI(nlib_thread_attr_getint)
140 #define nlib_thread_attr_setptr NLIB_CAPI(nlib_thread_attr_setptr)
141 #define nlib_thread_attr_getptr NLIB_CAPI(nlib_thread_attr_getptr)
142 #define nlib_thread_attr_setstack NLIB_CAPI(nlib_thread_attr_setstack)
143 #define nlib_thread_attr_getstack NLIB_CAPI(nlib_thread_attr_getstack)
144 #define nlib_thread_attr_destroy NLIB_CAPI(nlib_thread_attr_destroy)
145 #define nlib_thread_getpriority NLIB_CAPI(nlib_thread_getpriority)
146 #define nlib_thread_setpriority NLIB_CAPI(nlib_thread_setpriority)
147 #define nlib_thread_priority_min NLIB_CAPI(nlib_thread_priority_min)
148 #define nlib_thread_priority_max NLIB_CAPI(nlib_thread_priority_max)
149 #define nlib_thread_priority_default NLIB_CAPI(nlib_thread_priority_default)
150 #define nlib_thread_exit NLIB_CAPI(nlib_thread_exit)
151 #define nlib_thread_cleanup_push_ NLIB_CAPI(nlib_thread_cleanup_push_)
152 #define nlib_thread_cleanup_pop_ NLIB_CAPI(nlib_thread_cleanup_pop_)
153 #define nlib_write_stdout NLIB_CAPI(nlib_write_stdout)
154 #define nlib_write_stderr NLIB_CAPI(nlib_write_stderr)
155 #define nlib_debug_break NLIB_CAPI(nlib_debug_break)
156 #define nlib_debug_backtrace NLIB_CAPI(nlib_debug_backtrace)
157 #define nlib_debug_backtrace_gettext NLIB_CAPI(nlib_debug_backtrace_gettext)
158 #define nlib_getenv NLIB_CAPI(nlib_getenv)
159 #define nlib_log_print NLIB_CAPI(nlib_log_print)
160 #define nlib_log_vprint NLIB_CAPI(nlib_log_vprint)
161 #define nlib_log_attr_setint NLIB_CAPI(nlib_log_attr_setint)
162 #define nlib_fd_open NLIB_CAPI(nlib_fd_open)
163 #define nlib_fd_close NLIB_CAPI(nlib_fd_close)
164 #define nlib_fd_read NLIB_CAPI(nlib_fd_read)
165 #define nlib_fd_write NLIB_CAPI(nlib_fd_write)
166 #define nlib_fd_seek NLIB_CAPI(nlib_fd_seek)
167 #define nlib_fd_pread NLIB_CAPI(nlib_fd_pread)
168 #define nlib_fd_pwrite NLIB_CAPI(nlib_fd_pwrite)
169 #define nlib_fd_truncate NLIB_CAPI(nlib_fd_truncate)
170 #define nlib_fd_getsize NLIB_CAPI(nlib_fd_getsize)
171 #define nlib_fd_flush NLIB_CAPI(nlib_fd_flush)
172 #define nlib_fd_sync NLIB_CAPI(nlib_fd_sync)
173 #define nlib_fd_native_handle NLIB_CAPI(nlib_fd_native_handle)
174 #define nlib_fd_readv NLIB_CAPI(nlib_fd_readv)
175 #define nlib_fd_writev NLIB_CAPI(nlib_fd_writev)
176 #define nlib_fd_preadv NLIB_CAPI(nlib_fd_preadv)
177 #define nlib_fd_pwritev NLIB_CAPI(nlib_fd_pwritev)
178 #define nlib_unlink NLIB_CAPI(nlib_unlink)
179 #define nlib_mkdir NLIB_CAPI(nlib_mkdir)
180 #define nlib_rmdir NLIB_CAPI(nlib_rmdir)
181 #define nlib_rename NLIB_CAPI(nlib_rename)
182 #define nlib_dir_open NLIB_CAPI(nlib_dir_open)
183 #define nlib_dir_close NLIB_CAPI(nlib_dir_close)
184 #define nlib_dir_read NLIB_CAPI(nlib_dir_read)
185 #define nlib_is_dir NLIB_CAPI(nlib_is_dir)
186 #define nlib_exist_path NLIB_CAPI(nlib_exist_path)
187 #define nlib_disk_freespace NLIB_CAPI(nlib_disk_freespace)
188 #define nlib_spinlock_lock_ NLIB_CAPI(nlib_spinlock_lock_)
189 #define nlib_spinlock_unlock_ NLIB_CAPI(nlib_spinlock_unlock_)
190 #define nlib_vsnprintf NLIB_CAPI(nlib_vsnprintf)
191 #define nlib_snprintf NLIB_CAPI(nlib_snprintf)
192 #define nlib_vdprintf NLIB_CAPI(nlib_vdprintf)
193 #define nlib_dprintf NLIB_CAPI(nlib_dprintf)
194 #define nlib_printf NLIB_CAPI(nlib_printf)
195 #define nlib_vsnwprintf NLIB_CAPI(nlib_vsnwprintf)
196 #define nlib_snwprintf NLIB_CAPI(nlib_snwprintf)
197 #define nlib_vdwprintf NLIB_CAPI(nlib_vdwprintf)
198 #define nlib_dwprintf NLIB_CAPI(nlib_dwprintf)
199 #define nlib_wprintf NLIB_CAPI(nlib_wprintf)
200 #define nlib_vsnprintf_fallback NLIB_CAPI(nlib_vsnprintf_fallback)
201 #define nlib_snprintf_fallback NLIB_CAPI(nlib_snprintf_fallback)
202 #define nlib_vsnwprintf_fallback NLIB_CAPI(nlib_vsnwprintf_fallback)
203 #define nlib_snwprintf_fallback NLIB_CAPI(nlib_snwprintf_fallback)
204 #define nlib_memcmp NLIB_CAPI(nlib_memcmp)
205 #define nlib_memchr NLIB_CAPI(nlib_memchr)
206 #define nlib_memrchr NLIB_CAPI(nlib_memrchr)
207 #define nlib_memchr_not NLIB_CAPI(nlib_memchr_not)
208 #define nlib_memchr_range_not NLIB_CAPI(nlib_memchr_range_not)
209 #define nlib_memchr_lt NLIB_CAPI(nlib_memchr_lt)
210 #define nlib_memchr_gt NLIB_CAPI(nlib_memchr_gt)
211 #define nlib_memchr_mb NLIB_CAPI(nlib_memchr_mb)
212 #define nlib_memchr_set NLIB_CAPI(nlib_memchr_set)
213 #define nlib_memchr_set_not NLIB_CAPI(nlib_memchr_set_not)
214 #define nlib_skipws NLIB_CAPI(nlib_skipws)
215 #define nlib_strlen NLIB_CAPI(nlib_strlen)
216 #define nlib_strnlen NLIB_CAPI(nlib_strnlen)
217 #define nlib_strcpy NLIB_CAPI(nlib_strcpy)
218 #define nlib_strncpy NLIB_CAPI(nlib_strncpy)
219 #define nlib_strchr NLIB_CAPI(nlib_strchr)
220 #define nlib_strrchr NLIB_CAPI(nlib_strrchr)
221 #define nlib_strchr_mb NLIB_CAPI(nlib_strchr_mb)
222 #define nlib_wcslen NLIB_CAPI(nlib_wcslen)
223 #define nlib_wcsnlen NLIB_CAPI(nlib_wcsnlen)
224 #define nlib_wcscpy NLIB_CAPI(nlib_wcscpy)
225 #define nlib_wcsncpy NLIB_CAPI(nlib_wcsncpy)
226 #define nlib_strcat NLIB_CAPI(nlib_strcat)
227 #define nlib_strncat NLIB_CAPI(nlib_strncat)
228 #define nlib_wcscat NLIB_CAPI(nlib_wcscat)
229 #define nlib_wcsncat NLIB_CAPI(nlib_wcsncat)
230 #define nlib_wide_to_utf8 NLIB_CAPI(nlib_wide_to_utf8)
231 #define nlib_utf8_to_wide NLIB_CAPI(nlib_utf8_to_wide)
232 #define nlib_strcplen NLIB_CAPI(nlib_strcplen)
233 #define nlib_strcplen_ex NLIB_CAPI(nlib_strcplen_ex)
234 #define nlib_wcscplen NLIB_CAPI(nlib_wcscplen)
235 #define nlib_swapendian_16 NLIB_CAPI(nlib_swapendian_16)
236 #define nlib_swapendian_32 NLIB_CAPI(nlib_swapendian_32)
237 #define nlib_swapendian_64 NLIB_CAPI(nlib_swapendian_64)
238 #define nlib_malloc NLIB_CAPI(nlib_malloc)
239 #define nlib_free NLIB_CAPI(nlib_free)
240 #define nlib_calloc NLIB_CAPI(nlib_calloc)
241 #define nlib_realloc NLIB_CAPI(nlib_realloc)
242 #define nlib_free_size NLIB_CAPI(nlib_free_size)
243 #define nlib_memalign NLIB_CAPI(nlib_memalign)
244 #define nlib_popcnt NLIB_CAPI(nlib_popcnt)
245 #define nlib_popcnt64 NLIB_CAPI(nlib_popcnt64)
246 #define nlib_popcnt16 NLIB_CAPI(nlib_popcnt16)
247 #endif
248 
249 #if defined(_MSC_VER)
250 # include "nn/nlib/Platform_win32.h"
251 #elif defined(__linux__) || \
252  defined(__FreeBSD__) || \
253  defined(__CYGWIN__) || \
254  (defined(__APPLE__) && defined(__MACH__))
255 # ifndef NLIB_UNIX
256 # define NLIB_UNIX
257 # endif
258 # include "nn/nlib/Platform_unix.h"
259 #elif defined(NN_PLATFORM_CTR)
260 # include "nn/nlib/Platform_ctr.h"
261 #elif defined(CAFE)
262 # ifndef NLIB_CAFE_PPC
263 # define NLIB_CAFE_PPC
264 # endif
265 # include "nn/nlib/Platform_cafe.h"
266 #endif
267 
268 #if defined(__ARM_NEON__) || defined(__aarch64__)
269 # ifndef NLIB_NEON
270 # define NLIB_NEON
271 # endif
272 #endif
273 
274 #ifdef __SSE4_1__
275 # ifndef NLIB_SSE41
276 # define NLIB_SSE41
277 # endif
278 #endif
279 
280 #if defined(NLIB_SSE41) || defined(NLIB_NEON)
281 # define NLIB_SIMD
282 # ifdef NLIB_NEON
283 # include <arm_neon.h> // NOLINT
284 # endif
285 # ifdef NLIB_SSE41
286 // Use _mm_popcnt_u32() only if the executing processor supports SSE4.2
287 # include <nmmintrin.h> // NOLINT
288 # endif
289 #endif
290 
291 // https://www.jpcert.or.jp/sc-rules/c-int01-c.html
292 // https://www.securecoding.cert.org/confluence/display/seccode/INT01-C.+Use+rsize_t+or+size_t+for+all+integer+values+representing+the+size+of+an+object
293 #ifndef RSIZE_MAX
294 # ifndef NLIB_64BIT
295 # define RSIZE_MAX 0x7FFFFFFFL
296 # else
297 # define RSIZE_MAX 0x7FFFFFFFFFFFFFFFLL
298 # endif
299 #endif
300 
301 #ifndef NLIB_WARN
302 # define NLIB_WARN(exp) ("WARNING: " exp)
303 // #pragma message NLIB_WARN("your message")
304 #endif
305 
306 // NOTE:
307 // You can use those macros on the environment which printf does not support '%z'
308 // for size_t.
309 // "%"PRIuS, sizet_val
310 // See: https://google-styleguide.googlecode.com/svn/trunk/cppguide.html#64-bit_Portability
311 // In Japanese: http://www.textdrop.net/google-styleguide-ja/cppguide.xml
312 #ifndef __PRIS_PREFIX
313 # define __PRIS_PREFIX "z"
314 #endif
315 
316 #ifndef PRIdS
317 # define PRIdS __PRIS_PREFIX "d"
318 #endif
319 
320 #ifndef PRIxS
321 # define PRIxS __PRIS_PREFIX "x"
322 #endif
323 
324 #ifndef PRIuS
325 # define PRIuS __PRIS_PREFIX "u"
326 #endif
327 
328 #ifndef PRIXS
329 # define PRIXS __PRIS_PREFIX "X"
330 #endif
331 
332 #ifndef PRIoS
333 # define PRIoS __PRIS_PREFIX "o"
334 #endif
335 
336 #ifndef NLIB_LOCKABLE
337 # define NLIB_LOCKABLE
338 #endif
339 
340 #ifndef NLIB_LOCK_FUNC
341 # define NLIB_LOCK_FUNC(...)
342 #endif
343 
344 #ifndef NLIB_SHARED_LOCK_FUNC
345 # define NLIB_SHARED_LOCK_FUNC(...)
346 #endif
347 
348 #ifndef NLIB_SHARED_TRYLOCK_FUNC
349 # define NLIB_SHARED_TRYLOCK_FUNC(...)
350 #endif
351 
352 #ifndef NLIB_UNLOCK_FUNC
353 # define NLIB_UNLOCK_FUNC(...)
354 #endif
355 
356 #ifndef NLIB_SHARED_UNLOCK_FUNC
357 # define NLIB_SHARED_UNLOCK_FUNC(...)
358 #endif
359 
360 #ifndef NLIB_TRYLOCK_FUNC
361 # define NLIB_TRYLOCK_FUNC(...)
362 #endif
363 
364 #ifndef NLIB_GUARDED_BY
365 # define NLIB_GUARDED_BY(x)
366 #endif
367 
368 #ifndef NLIB_PT_GUARDED_BY
369 # define NLIB_PT_GUARDED_BY(x)
370 #endif
371 
372 #ifndef NLIB_LOCK_REQUIRED
373 # define NLIB_LOCK_REQUIRED(...)
374 #endif
375 
376 #ifndef NLIB_LOCK_EXCLUDED
377 # define NLIB_LOCK_EXCLUDED(...)
378 #endif
379 
380 #ifndef NLIB_SHARED_LOCK_REQUIRED
381 # define NLIB_SHARED_LOCK_REQUIRED(...)
382 #endif
383 
384 #ifndef NLIB_SCOPED_LOCKABLE
385 # define NLIB_SCOPED_LOCKABLE
386 #endif
387 
388 #ifndef NLIB_NO_THREAD_SAFETY_ANALYSIS
389 # define NLIB_NO_THREAD_SAFETY_ANALYSIS
390 #endif
391 
392 #ifndef NLIB_ASSUME
393 # define NLIB_ASSUME(cond) switch (0) case 0: default: if (cond) ; else __builtin_unreachable() /* NOLINT */
394 #endif
395 
396 #ifdef NLIB_NONNULL_ENABLED
397 # define NLIB_EINVAL_IFNULL(p)
398 #else
399 # define NLIB_EINVAL_IFNULL(p) if (!p) return EINVAL
400 #endif
401 
402 //
403 // Error Type
404 //
405 typedef int errno_t; // TR 24731-1
406 
407 //
408 // stdint
409 //
410 #ifdef NLIB_HAS_STDHEADER_STDINT
411 # include <stdint.h> // NOLINT
412 #endif
413 
414 #ifdef NLIB_HAS_STDHEADER_INTTYPES
415 # include <inttypes.h> // NOLINT
416 #endif
417 
418 #if defined(__LP64__) && __LP64__ == 1
419 // if long, unsinged long are 64 bits long
420 #define NLIB_LP64
421 typedef int64_t nlib_long_compatible_t;
422 typedef uint64_t nlib_ulong_compatible_t;
423 #else
424 typedef int32_t nlib_long_compatible_t;
425 typedef uint32_t nlib_ulong_compatible_t;
426 #endif
427 
428 #ifndef NLIB_VIS_PUBLIC_ALT
429 #define NLIB_VIS_PUBLIC_ALT NLIB_VIS_PUBLIC
430 #endif
431 
432 #ifdef __cplusplus
433 extern "C" {
434 #endif
435 
437 
438 //
439 // Native error
440 //
442 
443 //
444 // Version
445 //
446 
447 // returns NLIB_VERSION
449 
450 //
451 // Atomic
452 //
453 #if defined(__INTELLISENSE__)
454 #define NLIB_ATOMIC_RELAXED (0)
455 #define NLIB_ATOMIC_ACQUIRE (1)
456 #define NLIB_ATOMIC_RELEASE (2)
457 #define NLIB_ATOMIC_ACQ_REL (3)
458 #define NLIB_ATOMIC_SEQ_CST (7)
459 
460 int32_t nlib_atomic_load32(const int32_t* ptr, int memorder);
461 void nlib_atomic_store32(int32_t* ptr, int32_t val, int memorder);
462 // *target = value, and returns the old value of *target
463 int32_t nlib_atomic_exchange32(int32_t* ptr, int32_t val, int memorder);
464 // *ptr = desired and return non-zero if successful
465 int nlib_atomic_compare_exchange32(int32_t* ptr, int32_t* expected,
466  int32_t desired, int weak,
467  int success_memorder, int failure_memorder);
468 // *ptr += val; return *ptr;
469 int32_t nlib_atomic_add_fetch32(int32_t* ptr, int32_t val, int memorder);
470 // *ptr -= val; return *ptr;
471 int32_t nlib_atomic_sub_fetch32(int32_t* ptr, int32_t val, int memorder);
472 // *ptr &= val; return *ptr;
473 int32_t nlib_atomic_and_fetch32(int32_t* ptr, int32_t val, int memorder);
474 // *ptr ^= val; return *ptr;
475 int32_t nlib_atomic_xor_fetch32(int32_t* ptr, int32_t val, int memorder);
476 // *ptr |= val; return *ptr;
477 int32_t nlib_atomic_or_fetch32(int32_t* ptr, int32_t val, int memorder);
478 // tmp = *ptr; *ptr += val; return tmp;
479 int32_t nlib_atomic_fetch_add32(int32_t* ptr, int32_t val, int memorder);
480 // tmp = *ptr; *ptr -= val; return tmp;
481 int32_t nlib_atomic_fetch_sub32(int32_t* ptr, int32_t val, int memorder);
482 // tmp = *ptr; *ptr &= val; return tmp;
483 int32_t nlib_atomic_fetch_and32(int32_t* ptr, int32_t val, int memorder);
484 // tmp = *ptr; *ptr ^= val; return tmp;
485 int32_t nlib_atomic_fetch_xor32(int32_t* ptr, int32_t val, int memorder);
486 // tmp = *ptr; *ptr |= val; return tmp;
487 int32_t nlib_atomic_fetch_or32(int32_t* ptr, int32_t val, int memorder);
488 
489 int64_t nlib_atomic_load64(const int64_t* ptr, int memorder);
490 void nlib_atomic_store64(int64_t* ptr, int64_t val, int memorder);
491 // *target = value, and returns the old value of *target
492 int64_t nlib_atomic_exchange64(int64_t* ptr, int64_t val, int memorder);
493 // *ptr = desired and return non-zero if successful
494 int nlib_atomic_compare_exchange64(int64_t* ptr, int64_t* expected,
495  int64_t desired, int weak,
496  int success_memorder, int failure_memorder);
497 // *ptr += val; return *ptr;
498 int64_t nlib_atomic_add_fetch64(int64_t* ptr, int64_t val, int memorder);
499 // *ptr -= val; return *ptr;
500 int64_t nlib_atomic_sub_fetch64(int64_t* ptr, int64_t val, int memorder);
501 // *ptr &= val; return *ptr;
502 int64_t nlib_atomic_and_fetch64(int64_t* ptr, int64_t val, int memorder);
503 // *ptr ^= val; return *ptr;
504 int64_t nlib_atomic_xor_fetch64(int64_t* ptr, int64_t val, int memorder);
505 // *ptr |= val; return *ptr;
506 int64_t nlib_atomic_or_fetch64(int64_t* ptr, int64_t val, int memorder);
507 // tmp = *ptr; *ptr += val; return tmp;
508 int64_t nlib_atomic_fetch_add64(int64_t* ptr, int64_t val, int memorder);
509 // tmp = *ptr; *ptr -= val; return tmp;
510 int64_t nlib_atomic_fetch_sub64(int64_t* ptr, int64_t val, int memorder);
511 // tmp = *ptr; *ptr &= val; return tmp;
512 int64_t nlib_atomic_fetch_and64(int64_t* ptr, int64_t val, int memorder);
513 // tmp = *ptr; *ptr ^= val; return tmp;
514 int64_t nlib_atomic_fetch_xor64(int64_t* ptr, int64_t val, int memorder);
515 // tmp = *ptr; *ptr |= val; return tmp;
516 int64_t nlib_atomic_fetch_or64(int64_t* ptr, int64_t val, int memorder);
517 
518 void* nlib_atomic_loadptr(void* const* ptr, int memorder);
519 void nlib_atomic_storeptr(void** ptr, void* val, int memorder);
520 // *ptr = desired and return non-zero if successful
521 int nlib_atomic_compare_exchangeptr(void** ptr, void** expected,
522  void* desired, int weak,
523  int success_memorder, int failure_memorder);
524 
525 void nlib_atomic_thread_fence(int memorder);
526 #endif
527 
528 //
529 // Time, Duration
530 //
531 
532 // 100ns => 1, 1970/01/01 == 0
533 typedef int64_t nlib_time;
534 // 100ns => 1, 1ms => 10000
535 typedef int64_t nlib_duration;
536 
537 // 100ns => 1, 1970/01/01 == 0
539 // 100ns => 1, boot time == 0, msec = *t / 10000
541 // 100ns => 1, sleep 1 msec = nlib_sleep(10000)
542 NLIB_VIS_PUBLIC errno_t nlib_sleep(nlib_duration t);
543 
544 //
545 // Random
546 //
547 
548 // Store 'size' bytes of random values on 'buf'
550 
551 //
552 // Virtual Memory, Physical Memory
553 //
555 NLIB_VIS_PUBLIC errno_t nlib_virtual_alloc(void** ptr, size_t size) NLIB_NONNULL;
556 NLIB_VIS_PUBLIC errno_t nlib_virtual_free(void* ptr, size_t size) NLIB_NONNULL;
557 NLIB_VIS_PUBLIC errno_t nlib_physical_alloc(void* ptr, size_t size, int prot) NLIB_NONNULL;
559 NLIB_VIS_PUBLIC errno_t nlib_mlock(void* addr, size_t len) NLIB_NONNULL;
560 NLIB_VIS_PUBLIC errno_t nlib_munlock(void* addr, size_t len) NLIB_NONNULL;
561 
562 #define NLIB_PHYSICAL_ALLOC_PROT_NONE 0
563 #define NLIB_PHYSICAL_ALLOC_PROT_READ 1
564 #define NLIB_PHYSICAL_ALLOC_PROT_WRITE 2
565 #define NLIB_PHYSICAL_ALLOC_PROT_EXEC 4
566 
567 //
568 // TLS
569 //
570 #ifdef NLIB_UNIX
571 typedef pthread_key_t nlib_tls;
572 #else
573 typedef unsigned int nlib_tls;
574 #endif
575 typedef void (*nlib_tls_destructor)(void* tls_value);
576 // code snippets:
577 // # map tls_key on thread local storage
578 // nlib_tls tls_key;
579 // e = nlib_tls_alloc(&tls_key, NULL); # no dtor invoked if destr is NULL
580 // if (e != 0) { error ... }
581 // # unmap tls_key
582 // nlib_tls_free(tls_key);
583 // # access from a thread
584 // void* thread_local_value;
585 // nlib_tls_getvalue(tls_key, &thread_local_value);
586 // # use and update thread_local_value
587 // nlib_tls_setvalue(tls_key, thread_local_value);
591 NLIB_VIS_PUBLIC errno_t nlib_tls_setvalue(nlib_tls tls, const void* value);
592 NLIB_VIS_PUBLIC errno_t nlib_tls_getvalue(nlib_tls tls, void** value) NLIB_NONNULL;
593 
594 //
595 // Mutex
596 //
597 
598 // you can use NLIB_MUTEX_INITIALIZER static initializer
600 // you can use NLIB_RECURSIVE_MUTEX_INITIALIZER static initializer
602 // you can use NLIB_RECURSIVE_TIMED_MUTEX_INITIALIZER static initializer
604 
605 // code snippets:
606 // nlib_mutex m;
607 // if (nlib_mutex_init(&m) != 0) { ... } # always returns 0 on almost all platforms?
608 // nlib_mutex_lock(&m);
609 // ....
610 // nlib_mutex_unlock(&m);
611 // nlib_mutex_destroy(&m);
613 // returns EBUSY if a lock cannot be acquired
615 // returns ETIMEDOUT if timeout
617  nlib_duration delta) NLIB_NONNULL;
619 // don't forget to write this, some platforms require this called.
621 
622 //
623 // Semaphore
624 //
625 
628 // returns EAGAIN if semaphore cannot be acquired
630 // returns ETIMEDOUT if timeout
632  nlib_semaphore* sem, nlib_duration duration) NLIB_NONNULL;
634  int* __restrict previous_count) NLIB_NONNULL_1;
635 NLIB_VIS_PUBLIC errno_t nlib_semaphore_post_ex(nlib_semaphore* __restrict sem, int release_count,
636  int* __restrict previous_count) NLIB_NONNULL_1;
638 
639 //
640 // Condition Variable
641 //
642 
643 // you can use NLIB_COND_INITIALIZER for static initializer
647 // code snippets:
648 // Initialization:
649 // bool flag = false;
650 // nlib_mutex m;
651 // nlib_cond cond;
652 // nlib_mutex_init(&m);
653 // nlib_cond_init(&cond);
654 // Thread1:
655 // nlib_mutex_lock(&m);
656 // while (!flag)
657 // e = nlib_cond_wait(&cond, &m); # m to be unlocked in nlib_cond_wait
658 // # note that nlib_cond_wait may return without signal notified
659 // if (e != 0) { error .... }
660 // # do job and reset flag
661 // flag = false;
662 // nlib_mutex_unlock(&m);
663 // Thread2:
664 // nlib_mutex_lock(&m);
665 // flag = true;
666 // nlib_cond_broadcast(&cond);
667 // nlib_mutex_unlock(&m);
669  nlib_mutex* __restrict mutex) NLIB_NONNULL;
670 // returns ETIMEDOUT if timeout, and see baloon for nlib_cond_wait()
672  nlib_mutex* __restrict mutex,
673  nlib_duration duration) NLIB_NONNULL;
674 // returns ETIMEDOUT if timeout, and see baloon for nlib_cond_wait()
676  nlib_mutex* __restrict mutex,
677  nlib_time abstime) NLIB_NONNULL;
679 
680 //
681 // Read/Write lock
682 //
683 #ifndef NLIB_RWLOCK_HAS_NATIVE
684 struct nlib_rwlock_ {
685  int32_t _0[3];
686  nlib_mutex _1[2];
687  nlib_cond _2;
688 };
689 typedef struct nlib_rwlock_ nlib_rwlock;
690 
691 #define NLIB_RWLOCK_INITIALIZER { \
692  { 0, 0, 0 }, \
693  { NLIB_RECURSIVE_TIMED_MUTEX_INITIALIZER, NLIB_RECURSIVE_TIMED_MUTEX_INITIALIZER }, \
694  NLIB_COND_INITIALIZER }
695 #endif
696 
699 
703  nlib_duration duration) NLIB_NONNULL;
705  nlib_time abstime) NLIB_NONNULL;
707 
711  nlib_duration duration) NLIB_NONNULL;
713  nlib_time abstime) NLIB_NONNULL;
715 
716 #if defined(_MSC_VER) && defined(NLIB_RWLOCK_HAS_NATIVE)
717 typedef struct nlib_condrwlock_ {
718  CONDITION_VARIABLE cond;
720 #define NLIB_CONDRWLOCK_INITIALIZER { CONDITION_VARIABLE_INIT }
721 #else
722 typedef struct nlib_condrwlock_ {
723  nlib_cond cond;
724  nlib_mutex mutex;
726 #define NLIB_CONDRWLOCK_INITIALIZER { NLIB_COND_INITIALIZER, NLIB_MUTEX_INITIALIZER }
727 #endif
728 
734  nlib_rwlock* __restrict rwlock,
735  int rdlock) NLIB_NONNULL;
737  nlib_rwlock* __restrict rwlock,
738  nlib_duration duration,
739  int rdlock) NLIB_NONNULL;
741  nlib_rwlock* __restrict rwlock,
742  nlib_time abstime,
743  int rdlock) NLIB_NONNULL;
744 
745 //
746 // Barrier
747 //
748 #ifndef NLIB_BARRIER_HAS_NATIVE
749 struct nlib_barrier_ {
750  nlib_mutex _0;
751  nlib_cond _1;
752  unsigned int _2[3];
753 };
754 typedef struct nlib_barrier_ nlib_barrier;
755 #endif
756 
760 
761 //
762 // Once
763 //
764 struct nlib_onceflag_ {
765  int status;
766 };
767 typedef struct nlib_onceflag_ nlib_onceflag;
768 #define NLIB_ONCE_INIT { 0 }
769 typedef void (*nlib_oncefunc)(void);
770 
771 // code snippets
772 // void OnceFunc() { .... }
773 // nlib_onceflag flag = NLIB_ONCE_INIT; // should be static initialized
774 // nlib_once(&flag, OnceFunc); // OnceFunc executes only once
776 // returns EBUSY immedidately if an other thread is executing 'func'
778 
779 //
780 // Message Queue
781 //
782 #ifdef NLIB_DOXYGEN
783 typedef int32_t nlib_mq;
784 #else
785 typedef struct nlib_mq_ {
786  int32_t raw_handle; // 0 for invalid handle
787 } nlib_mq;
788 #endif
789 typedef void* nlib_mq_msg;
790 
791 #define NLIB_MQ_BLOCK 0
792 #define NLIB_MQ_NONBLOCK 1
793 #define NLIB_MQ_LOCKFREE 2
794 
796 #ifdef NLIB_DOXYGEN
797 struct nlib_mq_attr {
798  int32_t flag;
799  int32_t max_msg;
800  int32_t cur_msg;
802 };
803 #else
804 typedef struct nlib_mq_attr_ {
805  int32_t flag; // NLIB_MQ_BLOCK / NLIB_MQ_NONBLOCK
806  int32_t max_msg; // 0 for 128
807  int32_t cur_msg; // blocking mode only
808  nlib_mq_msg_destructor destructor;
809 } nlib_mq_attr;
810 #endif
811 
813  const nlib_mq_attr* attr) NLIB_NONNULL;
815  nlib_mq_attr* attr) NLIB_NONNULL;
818 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT errno_t nlib_mq_send(nlib_mq mq, nlib_mq_msg msg, int prio);
819 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT errno_t nlib_mq_send_until(nlib_mq mq, nlib_mq_msg msg,
820  int prio, nlib_time abstime);
821 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT errno_t nlib_mq_receive(nlib_mq mq, nlib_mq_msg* msg,
822  int* prio) NLIB_NONNULL_2;
824  nlib_mq_msg* msg,
825  int* prio,
826  nlib_time abstime) NLIB_NONNULL_2;
827 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT errno_t nlib_mq_drop(nlib_mq mq, nlib_mq_msg* msg,
828  int* prio) NLIB_NONNULL_2;
829 
830 //
831 // Thread
832 //
834 #define NLIB_THREAD_INVALID (nlib_thread)(0) // NOLINT
835 
836 #ifndef NLIB_SPINLOCK_HAS_NATIVE
837 typedef int32_t nlib_spinlock;
838 #endif
839 
840 #ifndef NLIB_THREAD_ATTR_HAS_NATIVE
841 struct nlib_thread_attr_ {
842  nlib_spinlock spin;
843  int detach_state;
844  int priority;
845  uint32_t affinity;
846  void* stack_addr;
847  size_t stack_size;
848 };
849 typedef struct nlib_thread_attr_ nlib_thread_attr;
850 #else
851 struct nlib_thread_attr_ {
852  pthread_attr_t attr;
853  uint32_t affinity;
854 };
855 typedef struct nlib_thread_attr_ nlib_thread_attr;
856 #endif
857 typedef void (*nlib_thread_func)(void* arg);
858 // -1 for invalid
859 typedef int nlib_thread_id;
860 
861 // code snippets:
862 // nlib_thread th;
863 // if ((e = nlib_thread_create(&th, NULL, myfunc, myarg)) != 0) { error ... }
864 // nlib_thread_join(th); # or nlib_thread_detach(th);
866  nlib_thread* __restrict thread,
867  const nlib_thread_attr* __restrict attr,
868  nlib_thread_func func,
869  void* __restrict arg) NLIB_NONNULL_1 NLIB_NONNULL_3;
878 // the argument must be string literal
879 // nlib_thread_setname("MY THREAD");
880 NLIB_VIS_PUBLIC errno_t nlib_thread_setname(const char* string_literal) NLIB_NONNULL;
881 
882 // NOTE:
883 // win32 does not have GetThreadAffinityMask()
884 // errno_t nlib_thread_get_affinify(nlib_thread thread, uint32_t* affinity);
885 
888  int value) NLIB_NONNULL;
889 NLIB_VIS_PUBLIC errno_t nlib_thread_attr_getint(const nlib_thread_attr* __restrict attr, int key,
890  int* __restrict value) NLIB_NONNULL;
892  void* __restrict value) NLIB_NONNULL_1;
893 NLIB_VIS_PUBLIC errno_t nlib_thread_attr_getptr(const nlib_thread_attr* __restrict attr, int key,
894  void** __restrict value) NLIB_NONNULL;
896  void* __restrict stack_addr,
897  size_t stack_size) NLIB_NONNULL;
899  void** __restrict stack_addr,
900  size_t* __restrict stack_size) NLIB_NONNULL;
902 
903 #define NLIB_THREAD_ATTR_KEY_DETACHSTATE (1)
904 #define NLIB_THREAD_ATTR_KEY_STACKSIZE (2)
905 #define NLIB_THREAD_ATTR_KEY_PRIORITY (4)
906 #define NLIB_THREAD_ATTR_KEY_AFFINITY (5)
907 
913 
914 #ifndef NN_PLATFORM_CTR
915 // See also nlib_thread_exit_cpp();
917 #endif
918 
919 #ifdef NLIB_DOXYGEN
920 void nlib_thread_cleanup_push(void (*fn)(void*), void* arg);
921 void nlib_thread_cleanup_pop(int exec);
922 #elif defined(pthread_cleanup_push)
923 # define nlib_thread_cleanup_push(fn, arg) pthread_cleanup_push(fn, arg)
924 # define nlib_thread_cleanup_pop(exec) pthread_cleanup_pop(exec)
925 #elif !defined(NN_PLATFORM_CTR)
926 struct nlib_thread_cleanup_handler_ {
927  void (*func)(void*);
928  void* arg;
929  struct nlib_thread_cleanup_handler_* next;
930 };
931 #define nlib_thread_cleanup_push(fn, arg) switch (0) case 0: default: { \
932  struct nlib_thread_cleanup_handler_ _thread_cleanup_handler = { fn, arg, NULL }; \
933  nlib_thread_cleanup_push_(&_thread_cleanup_handler)
934 #define nlib_thread_cleanup_pop(exec) nlib_thread_cleanup_pop_(exec); }
935 
936 NLIB_VIS_PUBLIC void nlib_thread_cleanup_push_(struct nlib_thread_cleanup_handler_* handler);
937 NLIB_VIS_PUBLIC void nlib_thread_cleanup_pop_(int exec);
938 #endif
939 
940 //
941 // Console/Debug
942 //
943 
944 // note that buf is not null terminated
945 NLIB_VIS_PUBLIC errno_t nlib_write_stdout(size_t* __restrict result, const void* __restrict buf,
946  size_t count) NLIB_NONNULL;
947 // note that buf is not null terminated
948 NLIB_VIS_PUBLIC errno_t nlib_write_stderr(size_t* __restrict result, const void* __restrict buf,
949  size_t count) NLIB_NONNULL;
951 NLIB_VIS_PUBLIC errno_t nlib_debug_backtrace(size_t* __restrict result, void** __restrict buffer,
952  size_t count) NLIB_NONNULL;
953 NLIB_VIS_PUBLIC errno_t nlib_debug_backtrace_gettext(char* __restrict str, size_t strbufsize,
954  void* const* __restrict buf,
955  size_t count) NLIB_NONNULL;
956 NLIB_VIS_PUBLIC errno_t nlib_getenv(size_t* __restrict result, char* __restrict buf, size_t bufsize,
957  const char* __restrict varname) NLIB_NONNULL_1 NLIB_NONNULL_4;
958 
959 typedef enum nlib_log_priority {
960  NLIB_LOG_UNKNOWN = 0,
961  NLIB_LOG_DEAFULT,
968  NLIB_LOG_SILENT,
973 
974 typedef enum nlib_log_key {
975  NLIB_LOG_ATTR_UNKNOWN = 0,
976  NLIB_LOG_ATTR_STDOUT,
977  NLIB_LOG_ATTR_STDERR,
978  NLIB_LOG_ATTR_MSVC_TRACE,
979  NLIB_LOG_ATTR_SYSLOG,
980  NLIB_LOG_ATTR_NLIB_FD,
981  NLIB_LOG_ATTR_MAX
982 } nlib_log_key;
983 
984 #ifndef NLIB_ATTRIBUTE_PRINTF
985 # define NLIB_ATTRIBUTE_PRINTF(x, y) __attribute__((format(printf, x, y)))
986 #endif
987 
988 NLIB_VIS_PUBLIC int nlib_log_print(int prio, _Printf_format_string_ const char* __restrict tag,
989  const char* __restrict fmt, ...)
990  NLIB_ATTRIBUTE_PRINTF(3, 4) NLIB_NONNULL;
991 NLIB_VIS_PUBLIC int nlib_log_vprint(int prio, _Printf_format_string_ const char* __restrict tag,
992  const char* __restrict fmt, va_list ap) NLIB_NONNULL;
993 NLIB_VIS_PUBLIC errno_t nlib_log_attr_setint(int prio, int key, int value);
994 
995 //
996 // File Access
997 //
998 
999 #ifdef O_RDONLY
1000 # define NLIB_FD_O_RDONLY O_RDONLY
1001 #else
1002 # define NLIB_FD_O_RDONLY (0x0000)
1003 #endif
1004 
1005 #ifdef O_WRONLY
1006 # define NLIB_FD_O_WRONLY O_WRONLY
1007 #else
1008 # define NLIB_FD_O_WRONLY (0x0001)
1009 #endif
1010 
1011 #ifdef O_RDWR
1012 # define NLIB_FD_O_RDWR O_RDWR
1013 #else
1014 # define NLIB_FD_O_RDWR (0x0002)
1015 #endif
1016 
1017 #ifdef O_CREAT
1018 # define NLIB_FD_O_CREAT O_CREAT
1019 #else
1020 # define NLIB_FD_O_CREAT (0x0100)
1021 #endif
1022 
1023 #ifdef O_TRUNC
1024 # define NLIB_FD_O_TRUNC O_TRUNC
1025 #else
1026 # define NLIB_FD_O_TRUNC (0x0800)
1027 #endif
1028 
1029 #ifdef O_APPEND
1030 # define NLIB_FD_O_APPEND O_APPEND
1031 #else
1032 # define NLIB_FD_O_APPEND (0x0008)
1033 #endif
1034 
1035 #ifdef SEEK_SET
1036 # define NLIB_SEEK_SET SEEK_SET
1037 #else
1038 # define NLIB_SEEK_SET 0
1039 #endif
1040 
1041 #ifdef SEEK_CUR
1042 # define NLIB_SEEK_CUR SEEK_CUR
1043 #else
1044 # define NLIB_SEEK_CUR 1
1045 #endif
1046 
1047 // NOTE:
1048 // SEEK_END not supported(because of FIO19-C)
1049 
1050 typedef int64_t nlib_offset;
1051 typedef int nlib_fd;
1052 #define NLIB_FD_INVALID (-1)
1053 
1055  nlib_fd* fd, const char* native_path, unsigned int flags, const void* extra)
1056  NLIB_NONNULL_1 NLIB_NONNULL_2;
1059  size_t* __restrict result, nlib_fd fd, void* __restrict buf, size_t count) NLIB_NONNULL;
1061  size_t* __restrict result, nlib_fd fd, const void* __restrict buf, size_t count) NLIB_NONNULL;
1063  nlib_offset* result, nlib_fd fd, nlib_offset offset, int whence) NLIB_NONNULL;
1064 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT errno_t nlib_fd_pread(size_t* __restrict result, nlib_fd fd,
1065  void* __restrict buf, size_t count,
1066  nlib_offset offset) NLIB_NONNULL;
1067 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT errno_t nlib_fd_pwrite(size_t* __restrict result, nlib_fd fd,
1068  const void* __restrict buf,
1069  size_t count, nlib_offset offset)
1070  NLIB_NONNULL;
1071 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT errno_t nlib_fd_truncate(nlib_fd fd, nlib_offset length);
1072 
1074  nlib_fd fd) NLIB_NONNULL;
1077 NLIB_VIS_PUBLIC errno_t nlib_fd_native_handle(void** native_handle, nlib_fd fd) NLIB_NONNULL;
1078 // errno_t nlib_fd_fcntl_getflag(unsigned int* flags, nlib_fd fd);
1079 // errno_t nlib_fd_fcntl_setflag(nlib_fd fd, unsigned int flags);
1080 
1081 // Scatter/Gather buffer
1082 struct nlib_fd_iovec_ {
1083  void* iov_base;
1084  size_t iov_len;
1085 };
1086 typedef struct nlib_fd_iovec_ nlib_fd_iovec;
1087 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT errno_t nlib_fd_readv(size_t* __restrict result, nlib_fd fd,
1088  const nlib_fd_iovec* __restrict iov,
1089  int iovcnt) NLIB_NONNULL;
1090 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT errno_t nlib_fd_writev(size_t* __restrict result, nlib_fd fd,
1091  const nlib_fd_iovec* __restrict iov,
1092  int iovcnt) NLIB_NONNULL;
1093 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT errno_t nlib_fd_preadv(size_t* __restrict result, nlib_fd fd,
1094  const nlib_fd_iovec* __restrict iov,
1095  int iovcnt,
1096  nlib_offset offset) NLIB_NONNULL;
1097 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT errno_t nlib_fd_pwritev(size_t* __restrict result, nlib_fd fd,
1098  const nlib_fd_iovec* __restrict iov,
1099  int iovcnt,
1100  nlib_offset offset) NLIB_NONNULL;
1101 // errno_t nlib_fd_stat(stat* stat, nlib_fd fd);
1102 
1104 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT errno_t nlib_mkdir(const char* native_path,
1105  unsigned int flags) NLIB_NONNULL;
1107 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT errno_t nlib_rename(const char* __restrict old_path,
1108  const char* __restrict new_path)
1109  NLIB_NONNULL;
1110 
1111 struct nlib_dir_ {
1112  void* ptr;
1113 };
1114 typedef struct nlib_dir_ nlib_dir;
1115 typedef struct nlib_dirent_ {
1116  uint32_t flags; // 0: file, 1: directory
1117  char name[768];
1118 } nlib_dirent;
1119 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT errno_t nlib_dir_open(nlib_dir* __restrict dir,
1120  const char* __restrict native_path)
1121  NLIB_NONNULL;
1122 NLIB_VIS_PUBLIC errno_t nlib_dir_close(nlib_dir dir);
1124  nlib_dir dir) NLIB_NONNULL;
1125 
1126 // CTR does not have getcwd.....
1127 // errno_t nlib_getcwd(char* buf, size_t bufsize);
1128 // errno_t nlib_chdir(const char* path);
1129 
1130 NLIB_VIS_PUBLIC errno_t nlib_is_dir(int* __restrict result,
1131  const char* __restrict native_path) NLIB_NONNULL;
1132 // *result != 0 if exists
1133 NLIB_VIS_PUBLIC errno_t nlib_exist_path(int* __restrict result,
1134  const char* __restrict native_path) NLIB_NONNULL;
1135 NLIB_VIS_PUBLIC errno_t nlib_disk_freespace(const char* __restrict native_path,
1136  uint64_t* __restrict free_bytes_available,
1137  uint64_t* __restrict total_bytes,
1138  uint64_t* __restrict total_free_bytes) NLIB_NONNULL_1;
1139 
1140 //
1141 // Socket(Win32/Linux/Cygwin only)
1142 //
1143 #if defined(_MSC_VER) || defined(NLIB_UNIX)
1144 #define NLIB_SOCKET_ENABLED
1145 
1146 #ifdef AF_INET
1147 # define NLIB_AF_INET AF_INET
1148 #endif
1149 
1150 #ifdef AF_INET6
1151 # define NLIB_AF_INET6 AF_INET6
1152 #endif
1153 
1154 #ifdef SOCK_STREAM
1155 # define NLIB_SOCK_STREAM SOCK_STREAM
1156 #endif
1157 
1158 #ifdef SOCK_DGRAM
1159 # define NLIB_SOCK_DGRAM SOCK_DGRAM
1160 #endif
1161 
1162 #ifdef SOCK_RAW
1163 # define NLIB_SOCK_RAW SOCK_RAW
1164 #endif
1165 
1166 #ifdef SOCK_RDM
1167 # define NLIB_SOCK_RDM SOCK_RDM
1168 #endif
1169 
1170 // sizeof(SOCKET) is 8 on Win64, but it's safe because the upper 32bit of SOCKET is always 0.
1171 // https://stackoverflow.com/questions/1953639/is-it-safe-to-cast-socket-to-int-under-win64
1172 typedef int nlib_sock;
1173 #define NLIB_SOCKET_INVALID (nlib_sock)(-1) // NOLINT
1174 
1175 NLIB_VIS_PUBLIC errno_t nlib_socket(nlib_sock* sockfd, int af, int type, int protocol);
1176 
1177 typedef struct sockaddr nlib_sockaddr;
1178 typedef struct sockaddr_in nlib_sockaddr_in;
1179 typedef struct sockaddr_in6 nlib_sockaddr_in6;
1180 NLIB_VIS_PUBLIC errno_t nlib_bind(nlib_sock sockfd, const nlib_sockaddr* addr, int namelen);
1181 NLIB_VIS_PUBLIC errno_t nlib_listen(nlib_sock sockfd, int backlog);
1182 NLIB_VIS_PUBLIC errno_t nlib_accept(nlib_sock* __restrict s, nlib_sock sockfd,
1183  nlib_sockaddr* __restrict addr, int* __restrict addrlen);
1184 NLIB_VIS_PUBLIC errno_t nlib_connect(nlib_sock sockfd, const nlib_sockaddr* addr, int addrlen);
1185 
1186 #ifdef MSG_DONTROUTE
1187 # define NLIB_MSG_DONTROUTE MSG_DONTROUTE
1188 #endif
1189 
1190 #ifdef MSG_OOB
1191 # define NLIB_MSG_OOB MSG_OOB
1192 #endif
1193 
1194 NLIB_VIS_PUBLIC errno_t nlib_send(size_t* __restrict size, nlib_sock sockfd,
1195  const void* __restrict buf,
1196  size_t len, int flags);
1197 NLIB_VIS_PUBLIC errno_t nlib_sendto(size_t* __restrict size, nlib_sock sockfd,
1198  const void* __restrict buf, size_t len, int flags,
1199  const nlib_sockaddr* __restrict dest_addr, int addrlen);
1200 
1201 #ifdef MSG_PEEK
1202 # define NLIB_MSG_PEEK MSG_PEEK
1203 #endif
1204 
1205 #ifdef MSG_WAITALL
1206 # define NLIB_MSG_WAITALL MSG_WAITALL
1207 #endif
1208 
1209 NLIB_VIS_PUBLIC errno_t nlib_recv(size_t* __restrict size, nlib_sock sockfd, void* __restrict buf,
1210  size_t len, int flags);
1211 NLIB_VIS_PUBLIC errno_t nlib_recvfrom(size_t* __restrict size, nlib_sock sockfd,
1212  void* __restrict buf, size_t len, int flags,
1213  nlib_sockaddr* __restrict dest_addr, int* __restrict addrlen);
1214 NLIB_VIS_PUBLIC errno_t nlib_closesocket(nlib_sock sockfd);
1215 
1216 #ifdef _MSC_VER
1217 # define SHUT_RD SD_RECEIVE
1218 # define SHUT_WR SD_SEND
1219 # define SHUT_RDWR SD_BOTH
1220 #endif
1221 
1222 #ifdef SHUT_RD
1223 # define NLIB_SHUT_RD SHUT_RD
1224 #endif
1225 
1226 #ifdef SHUT_WR
1227 # define NLIB_SHUT_WR SHUT_WR
1228 #endif
1229 
1230 #ifdef SHUT_RDWR
1231 # define NLIB_SHUT_RDWR SHUT_RDWR
1232 #endif
1233 NLIB_VIS_PUBLIC errno_t nlib_shutdownsocket(nlib_sock sockfd, int how);
1234 
1235 NLIB_VIS_PUBLIC uint32_t nlib_htonl(uint32_t hostlong);
1236 NLIB_VIS_PUBLIC uint16_t nlib_htons(uint16_t hostshort);
1237 NLIB_VIS_PUBLIC uint32_t nlib_ntohl(uint32_t netlong);
1238 NLIB_VIS_PUBLIC uint16_t nlib_ntohs(uint16_t netshort);
1239 
1240 #ifdef INADDR_ANY
1241 # define NLIB_INADDR_ANY INADDR_ANY
1242 #endif
1243 
1244 #ifdef IN6ADDR_ANY_INIT
1245 # define NLIB_IN6ADDR_ANY_INIT IN6ADDR_ANY_INIT
1246 #endif
1247 
1248 #ifdef INADDR_LOOPBACK
1249 # define NLIB_INADDR_LOOPBACK INADDR_LOOPBACK
1250 #endif
1251 
1252 #ifdef IN6ADDR_LOOPBACK_INIT
1253 # define NLIB_IN6ADDR_LOOPBACK_INIT IN6ADDR_LOOPBACK_INIT
1254 #endif
1255 
1256 typedef struct in_addr nlib_in_addr;
1257 typedef struct in6_addr nlib_in6_addr;
1258 NLIB_VIS_PUBLIC errno_t nlib_inet_pton(int af, const char* __restrict src, void* __restrict dst);
1259 NLIB_VIS_PUBLIC errno_t nlib_inet_ntop(int af, const void* __restrict src, char* __restrict dst,
1260  size_t dst_size);
1261 
1262 /*
1263 # define EAI_ADDRFAMILY 1
1264 # define EAI_AGAIN 2
1265 # define EAI_BADFLAGS 3
1266 # define EAI_FAIL 4
1267 # define EAI_FAMILY 5
1268 # define EAI_MEMORY 6
1269 # define EAI_NODATA 7
1270 # define EAI_NONAME 8
1271 # define EAI_SERVICE 9
1272 # define EAI_SOCKTYPE 10
1273 # define EAI_SYSTEM 11
1274 # define EAI_BADHINTS 12
1275 # define EAI_PROTOCOL 13
1276 # define EAI_OVERFLOW 14
1277 */
1278 
1279 // for EAI_ADDRFAMILY, EAI_AGAIN, ... etc.
1280 typedef int eai_error_t;
1281 typedef struct addrinfo nlib_addrinfo;
1282 NLIB_VIS_PUBLIC eai_error_t nlib_getaddrinfo(const char* __restrict node,
1283  const char* __restrict service,
1284  const nlib_addrinfo* __restrict hints,
1285  nlib_addrinfo** __restrict res);
1286 NLIB_VIS_PUBLIC void nlib_freeaddrinfo(nlib_addrinfo* res);
1287 
1288 #ifdef SOL_SOCKET
1289 # define NLIB_SOL_SOCKET SOL_SOCKET
1290 #endif
1291 
1292 #ifdef IPPROTO_TCP
1293 # define NLIB_IPPROTO_TCP IPPROTO_TCP
1294 #endif
1295 
1296 #ifdef SO_KEEPALIVE
1297 # define NLIB_SO_KEEPALIVE SO_KEEPALIVE
1298 #endif
1299 
1300 #ifdef SO_LINGER
1301 # define NLIB_SO_LINGER SO_LINGER
1302 #endif
1303 
1304 #ifdef SO_OOBINLINE
1305 # define NLIB_SO_OOBINLINE SO_OOBINLINE
1306 #endif
1307 
1308 #ifdef SO_RCVBUF
1309 # define NLIB_SO_RCVBUF SO_RCVBUF
1310 #endif
1311 
1312 #ifdef SO_REUSEADDR
1313 # define NLIB_SO_REUSEADDR SO_REUSEADDR
1314 #endif
1315 
1316 #ifdef SO_RCVTIMEO
1317 # define NLIB_SO_RCVTIMEO SO_RCVTIMEO
1318 #endif
1319 
1320 #ifdef SO_SNDBUF
1321 # define NLIB_SO_SNDBUF SO_SNDBUF
1322 #endif
1323 
1324 #ifdef SO_SNDTIMEO
1325 # define NLIB_SO_SNDTIMEO SO_SNDTIMEO
1326 #endif
1327 
1328 #ifdef TCP_NODELAY
1329 # define NLIB_TCP_NODELAY TCP_NODELAY
1330 #endif
1331 
1332 NLIB_VIS_PUBLIC errno_t nlib_getsockopt(nlib_sock sockfd, int level, int optname,
1333  void* __restrict optval, int* __restrict optlen);
1334 NLIB_VIS_PUBLIC errno_t nlib_setsockopt(nlib_sock sockfd, int level, int optname,
1335  const void* optval, int optlen);
1336 
1337 typedef fd_set nlib_fd_set;
1338 NLIB_VIS_PUBLIC errno_t nlib_select(int nfds, nlib_fd_set* __restrict readfds,
1339  nlib_fd_set* __restrict writefds,
1340  nlib_fd_set* __restrict exceptfds, nlib_duration duration);
1341 
1342 #define NLIB_FD_CLR FD_CLR
1343 #define NLIB_FD_ISSET FD_ISSET
1344 #define NLIB_FD_SET FD_SET
1345 #define NLIB_FD_ZERO FD_ZERO
1346 
1347 #endif
1348 
1349 //
1350 // errno workaround
1351 //
1352 
1353 // PLEASE DO NOT DEFINE POSIX.1-2008 errno macros in your code
1354 // http://pubs.opengroup.org/onlinepubs/9699919799/
1355 #ifdef _MSC_VER
1356 # ifdef EDQUOT
1357 # warning Do not define EDQUOT. nlib may not work correctly.
1358 # endif
1359 #endif
1360 
1361 /*
1362 #ifdef NN_PLATFORM_CTR
1363 // socket_User.autogen.h defines POSIX.1-2008 errno macros as enum values.
1364 # ifdef NN_SOCKET_SOCKET_USER_AUTOGEN_H_
1365 # warning DO NOT INCLUDE socket_User.autogen.h in CTR socket library, Compile may fail.
1366 # define NLIB_SKIP_ERRNO_DEFINE
1367 # ifdef __errno_h
1368 # warning DO NOT INCLUDE errno.h when you include socket_User.autogen.h, you may not be able to handle errors of CTR socket library correctly. // NOLINT
1369 # endif
1370 # endif
1371 #endif
1372 */
1373 
1374 #ifndef NLIB_SKIP_ERRNO_DEFINE
1375 #include <errno.h> // NOLINT, for POSIX error values
1376 #define NLIB_E_BASE 2000
1377 
1378 // if boost/cerrno.hpp has definitions on errno values,
1379 // nlib defines the same values which boost/cerrno.hpp defines.
1380 
1381 // [E2BIG]
1382 // Argument list too long.
1383 #ifndef E2BIG
1384 # define E2BIG 9946
1385 #endif
1386 
1387 // [EACCES]
1388 // Permission denied.
1389 #ifndef EACCES
1390 # define EACCES 9973
1391 #endif
1392 
1393 // [EADDRINUSE]
1394 // Address in use.
1395 #ifndef EADDRINUSE
1396 # define EADDRINUSE 9902
1397 #endif
1398 
1399 // [EADDRNOTAVAIL]
1400 // Address not available.
1401 #ifndef EADDRNOTAVAIL
1402 # define EADDRNOTAVAIL 9903
1403 #endif
1404 
1405 // [EAFNOSUPPORT]
1406 // Address family not supported.
1407 #ifndef EAFNOSUPPORT
1408 # define EAFNOSUPPORT 9901
1409 #endif
1410 
1411 // [EAGAIN]
1412 // Resource unavailable, try again (may be the same value as [EWOULDBLOCK]).
1413 #ifndef EAGAIN
1414 # define EAGAIN 9976
1415 #endif
1416 
1417 // [EALREADY]
1418 // Connection already in progress.
1419 #ifndef EALREADY
1420 # define EALREADY 9907
1421 #endif
1422 
1423 // [EBADF]
1424 // Bad file descriptor.
1425 #ifndef EBADF
1426 # define EBADF 9949
1427 #endif
1428 
1429 // [EBADMSG]
1430 // Bad message.
1431 #ifndef EBADMSG
1432 # define EBADMSG 9905
1433 #endif
1434 
1435 // [EBUSY]
1436 // Device or resource busy.
1437 #ifndef EBUSY
1438 # define EBUSY 9952
1439 #endif
1440 
1441 // [ECANCELED]
1442 // Operation canceled.
1443 #ifndef ECANCELED
1444 # define ECANCELED 9927
1445 #endif
1446 
1447 // [ECHILD]
1448 // No child processes.
1449 #ifndef ECHILD
1450 # define ECHILD 9963
1451 #endif
1452 
1453 // [ECONNABORTED]
1454 // Connection aborted.
1455 #ifndef ECONNABORTED
1456 # define ECONNABORTED 9906
1457 #endif
1458 
1459 // [ECONNREFUSED]
1460 // Connection refused.
1461 #ifndef ECONNREFUSED
1462 # define ECONNREFUSED 9908
1463 #endif
1464 
1465 // [ECONNRESET]
1466 // Connection reset.
1467 #ifndef ECONNRESET
1468 # define ECONNRESET 9909
1469 #endif
1470 
1471 // [EDEADLK]
1472 // Resource deadlock would occur.
1473 #ifndef EDEADLK
1474 # define EDEADLK 9975
1475 #endif
1476 
1477 // [EDESTADDRREQ]
1478 // Destination address required.
1479 #ifndef EDESTADDRREQ
1480 # define EDESTADDRREQ 9910
1481 #endif
1482 
1483 // [EDOM]
1484 // Mathematics argument out of domain of function.
1485 #ifndef EDOM
1486 # define EDOM 9947
1487 #endif
1488 
1489 // [EDQUOT]
1490 // Reserved.
1491 #ifndef EDQUOT
1492 # define EDQUOT (NLIB_E_BASE + 19)
1493 #endif
1494 
1495 // [EEXIST]
1496 // File exists.
1497 #ifndef EEXIST
1498 # define EEXIST 9955
1499 #endif
1500 
1501 // [EFAULT]
1502 // Bad address.
1503 #ifndef EFAULT
1504 # define EFAULT 9948
1505 #endif
1506 
1507 // [EFBIG]
1508 // File too large.
1509 #ifndef EFBIG
1510 # define EFBIG 9956
1511 #endif
1512 
1513 // [EHOSTUNREACH]
1514 // Host is unreachable.
1515 #ifndef EHOSTUNREACH
1516 # define EHOSTUNREACH 9911
1517 #endif
1518 
1519 // [EIDRM]
1520 // Identifier removed.
1521 #ifndef EIDRM
1522 # define EIDRM 9912
1523 #endif
1524 
1525 // [EILSEQ]
1526 // Illegal byte sequence.
1527 #ifndef EILSEQ
1528 # define EILSEQ 9945
1529 #endif
1530 
1531 // [EINPROGRESS]
1532 // Operation in progress.
1533 #ifndef EINPROGRESS
1534 # define EINPROGRESS 9928
1535 #endif
1536 
1537 // [EINTR]
1538 // Interrupted function.
1539 #ifndef EINTR
1540 # define EINTR 9959
1541 #endif
1542 
1543 // [EINVAL]
1544 // Invalid argument.
1545 #ifndef EINVAL
1546 # define EINVAL 9943
1547 #endif
1548 
1549 // [EIO]
1550 // I/O error.
1551 #ifndef EIO
1552 # define EIO 9961
1553 #endif
1554 
1555 // [EISCONN]
1556 // Socket is connected.
1557 #ifndef EISCONN
1558 # define EISCONN 9904
1559 #endif
1560 
1561 // [EISDIR]
1562 // Is a directory.
1563 #ifndef EISDIR
1564 # define EISDIR 9962
1565 #endif
1566 
1567 // [ELOOP]
1568 // Too many levels of symbolic links.
1569 #ifndef ELOOP
1570 # define ELOOP 9939
1571 #endif
1572 
1573 // [EMFILE]
1574 // File descriptor value too large.
1575 #ifndef EMFILE
1576 # define EMFILE 9978
1577 #endif
1578 
1579 // [EMLINK]
1580 // Too many links.
1581 #ifndef EMLINK
1582 # define EMLINK 9979
1583 #endif
1584 
1585 // [EMSGSIZE]
1586 // Message too large.
1587 #ifndef EMSGSIZE
1588 # define EMSGSIZE 9913
1589 #endif
1590 
1591 // [EMULTIHOP]
1592 // Reserved.
1593 #ifndef EMULTIHOP
1594 # define EMULTIHOP (NLIB_E_BASE + 36)
1595 #endif
1596 
1597 // [ENAMETOOLONG]
1598 // Filename too long.
1599 #ifndef ENAMETOOLONG
1600 # define ENAMETOOLONG 9957
1601 #endif
1602 
1603 // [ENETDOWN]
1604 // Network is down.
1605 #ifndef ENETDOWN
1606 # define ENETDOWN 9914
1607 #endif
1608 
1609 // [ENETRESET]
1610 // Connection aborted by network.
1611 #ifndef ENETRESET
1612 # define ENETRESET 9915
1613 #endif
1614 
1615 // [ENETUNREACH]
1616 // Network unreachable.
1617 #ifndef ENETUNREACH
1618 # define ENETUNREACH 9916
1619 #endif
1620 
1621 // [ENFILE]
1622 // Too many files open in system.
1623 #ifndef ENFILE
1624 # define ENFILE 9977
1625 #endif
1626 
1627 // [ENOBUFS]
1628 // No buffer space available.
1629 #ifndef ENOBUFS
1630 # define ENOBUFS 9917
1631 #endif
1632 
1633 // [ENODATA]
1634 // [OB XSR] [Option Start] No message is available on the STREAM head read queue. [Option End]
1635 #ifndef ENODATA
1636 # define ENODATA 9919
1637 #endif
1638 
1639 // [ENODEV]
1640 // No such device.
1641 #ifndef ENODEV
1642 # define ENODEV 9967
1643 #endif
1644 
1645 // [ENOENT]
1646 // No such file or directory.
1647 #ifndef ENOENT
1648 # define ENOENT 9968
1649 #endif
1650 
1651 // [ENOEXEC]
1652 // Executable file format error.
1653 #ifndef ENOEXEC
1654 # define ENOEXEC 9954
1655 #endif
1656 
1657 // [ENOLCK]
1658 // No locks available.
1659 #ifndef ENOLCK
1660 # define ENOLCK 9964
1661 #endif
1662 
1663 // [ENOLINK]
1664 // Reserved.
1665 #ifndef ENOLINK
1666 # define ENOLINK 9918
1667 #endif
1668 
1669 // [ENOMEM]
1670 // Not enough space.
1671 #ifndef ENOMEM
1672 # define ENOMEM 9971
1673 #endif
1674 
1675 // [ENOMSG]
1676 // No message of the desired type.
1677 #ifndef ENOMSG
1678 # define ENOMSG 9920
1679 #endif
1680 
1681 // [ENOPROTOOPT]
1682 // Protocol not available.
1683 #ifndef ENOPROTOOPT
1684 # define ENOPROTOOPT 9921
1685 #endif
1686 
1687 // [ENOSPC]
1688 // No space left on device.
1689 #ifndef ENOSPC
1690 # define ENOSPC 9965
1691 #endif
1692 
1693 // [ENOSR]
1694 // [OB XSR] [Option Start] No STREAM resources. [Option End]
1695 #ifndef ENOSR
1696 # define ENOSR 9922
1697 #endif
1698 
1699 // [ENOSTR]
1700 // [OB XSR] [Option Start] Not a STREAM. [Option End]
1701 #ifndef ENOSTR
1702 # define ENOSTR 9924
1703 #endif
1704 
1705 // [ENOSYS]
1706 // Function not supported.
1707 #ifndef ENOSYS
1708 # define ENOSYS 9942
1709 #endif
1710 
1711 // [ENOTCONN]
1712 // The socket is not connected.
1713 #ifndef ENOTCONN
1714 # define ENOTCONN 9925
1715 #endif
1716 
1717 // [ENOTDIR]
1718 // Not a directory or a symbolic link to a directory.
1719 #ifndef ENOTDIR
1720 # define ENOTDIR 9970
1721 #endif
1722 
1723 // [ENOTEMPTY]
1724 // Directory not empty.
1725 #ifndef ENOTEMPTY
1726 # define ENOTEMPTY 9953
1727 #endif
1728 
1729 // [ENOTRECOVERABLE]
1730 // State not recoverable.
1731 #ifndef ENOTRECOVERABLE
1732 # define ENOTRECOVERABLE 9934
1733 #endif
1734 
1735 // [ENOTSOCK]
1736 // Not a socket.
1737 #ifndef ENOTSOCK
1738 # define ENOTSOCK 9923
1739 #endif
1740 
1741 // [ENOTSUP]
1742 // Not supported (may be the same value as [EOPNOTSUPP]).
1743 #ifndef ENOTSUP
1744 # define ENOTSUP 9926
1745 #endif
1746 
1747 // [ENOTTY]
1748 // Inappropriate I/O control operation.
1749 #ifndef ENOTTY
1750 # define ENOTTY 9958
1751 #endif
1752 
1753 // [ENXIO]
1754 // No such device or address.
1755 #ifndef ENXIO
1756 # define ENXIO 9966
1757 #endif
1758 
1759 // [EOPNOTSUPP]
1760 // Operation not supported on socket (may be the same value as [ENOTSUP]).
1761 #ifndef EOPNOTSUPP
1762 # define EOPNOTSUPP 9929
1763 #endif
1764 
1765 // [EOVERFLOW]
1766 // Value too large to be stored in data type.
1767 #ifndef EOVERFLOW
1768 # define EOVERFLOW 9940
1769 #endif
1770 
1771 // [EOWNERDEAD]
1772 // Previous owner died.
1773 #ifndef EOWNERDEAD
1774 # define EOWNERDEAD 9931
1775 #endif
1776 
1777 // [EPERM]
1778 // Operation not permitted.
1779 #ifndef EPERM
1780 # define EPERM 9972
1781 #endif
1782 
1783 // [EPIPE]
1784 // Broken pipe.
1785 #ifndef EPIPE
1786 # define EPIPE 9950
1787 #endif
1788 
1789 // [EPROTO]
1790 // Protocol error.
1791 #ifndef EPROTO
1792 # define EPROTO 9932
1793 #endif
1794 
1795 // [EPROTONOSUPPORT]
1796 // Protocol not supported.
1797 #ifndef EPROTONOSUPPORT
1798 # define EPROTONOSUPPORT 9933
1799 #endif
1800 
1801 // [EPROTOTYPE]
1802 // Protocol wrong type for socket.
1803 #ifndef EPROTOTYPE
1804 # define EPROTOTYPE 9941
1805 #endif
1806 
1807 // [ERANGE]
1808 // Result too large.
1809 #ifndef ERANGE
1810 # define ERANGE 9944
1811 #endif
1812 
1813 // [EROFS]
1814 // Read-only file system.
1815 #ifndef EROFS
1816 # define EROFS 9974
1817 #endif
1818 
1819 // [ESPIPE]
1820 // Invalid seek.
1821 #ifndef ESPIPE
1822 # define ESPIPE 9960
1823 #endif
1824 
1825 // [ESRCH]
1826 // No such process.
1827 #ifndef ESRCH
1828 # define ESRCH 9969
1829 #endif
1830 
1831 // [ESTALE]
1832 // Reserved.
1833 #ifndef ESTALE
1834 # define ESTALE (NLIB_E_BASE + 75)
1835 #endif
1836 
1837 // [ETIME]
1838 // [OB XSR] [Option Start] Stream ioctl() timeout. [Option End]
1839 #ifndef ETIME
1840 # define ETIME 9935
1841 #endif
1842 
1843 // [ETIMEDOUT]
1844 // Connection timed out.
1845 #ifndef ETIMEDOUT
1846 # define ETIMEDOUT 9938
1847 #endif
1848 
1849 // [ETXTBSY]
1850 // Text file busy.
1851 #ifndef ETXTBSY
1852 # define ETXTBSY 9936
1853 #endif
1854 
1855 // [EWOULDBLOCK]
1856 // Operation would block (may be the same value as [EAGAIN]).
1857 #ifndef EWOULDBLOCK
1858 # define EWOULDBLOCK 9930
1859 #endif
1860 
1861 // [EXDEV]
1862 // Cross-device link.
1863 #ifndef EXDEV
1864 # define EXDEV 9951
1865 #endif
1866 #endif // NLIB_SKIP_ERRNO_DEFINE
1867 
1868 //
1869 // nlib_spinlock
1870 //
1871 #ifndef NLIB_SPINLOCK_HAS_NATIVE
1872 #define NLIB_SPINLOCK_INITIALIZER (0)
1873 NLIB_VIS_PUBLIC void nlib_spinlock_lock_(nlib_spinlock* lock) NLIB_NONNULL;
1874 #ifndef NN_PLATFORM_CTR
1875 NLIB_VIS_PUBLIC void nlib_spinlock_unlock_(nlib_spinlock* lock) NLIB_NONNULL;
1876 #endif
1877 
1878 static NLIB_ALWAYS_INLINE void nlib_spinlock_init(nlib_spinlock* lock) {
1879  *lock = 0;
1880 }
1881 static NLIB_ALWAYS_INLINE void nlib_spinlock_lock(nlib_spinlock* lock) {
1882  int32_t expected = 0;
1883  if (!nlib_atomic_compare_exchange32(lock, &expected, 1, 1,
1885  nlib_spinlock_lock_(lock);
1886  }
1887 }
1888 static NLIB_ALWAYS_INLINE errno_t nlib_spinlock_trylock(nlib_spinlock* lock) {
1889  int32_t expected = 0;
1890  if (nlib_atomic_compare_exchange32(lock, &expected, 1, 0,
1892  return 0;
1893  else
1894  return EBUSY;
1895 }
1896 static NLIB_ALWAYS_INLINE void nlib_spinlock_unlock(nlib_spinlock* lock) {
1897 #ifdef NN_PLATFORM_CTR
1899 #else
1900  int32_t expected = 1;
1901  if (!nlib_atomic_compare_exchange32(lock, &expected, 0, 0,
1903  NLIB_ASSUME(expected == 2);
1904  nlib_spinlock_unlock_(lock);
1905  }
1906 #endif
1907 }
1908 #endif
1909 
1910 //
1911 // Utilities(Safer style functions)
1912 //
1913 NLIB_VIS_PUBLIC errno_t nlib_vsnprintf(size_t* __restrict count, char* __restrict buf, size_t size,
1914  _Printf_format_string_ const char* __restrict fmt, va_list args) NLIB_NONNULL_4;
1915 NLIB_VIS_PUBLIC errno_t nlib_snprintf(size_t* __restrict count, char* __restrict buf, size_t size,
1916  _Printf_format_string_ const char* __restrict fmt,
1917  ...) NLIB_ATTRIBUTE_PRINTF(4, 5) NLIB_NONNULL_4;
1918 NLIB_VIS_PUBLIC errno_t nlib_vdprintf(nlib_fd fd, size_t* __restrict count,
1919  _Printf_format_string_ const char* __restrict fmt, va_list args) NLIB_NONNULL_3;
1920 NLIB_VIS_PUBLIC errno_t nlib_dprintf(nlib_fd fd, size_t* __restrict count,
1921  _Printf_format_string_ const char* __restrict fmt,
1922  ...) NLIB_ATTRIBUTE_PRINTF(3, 4) NLIB_NONNULL_3;
1923 // fmt and string must be in UTF-8 even if you use Visual Studio
1925  _Printf_format_string_ const char* fmt, ...) NLIB_ATTRIBUTE_PRINTF(1, 2) NLIB_NONNULL_1;
1926 
1927 NLIB_VIS_PUBLIC errno_t nlib_vsnwprintf(size_t* __restrict count, wchar_t* __restrict buf,
1928  size_t size,
1929  _Printf_format_string_ const wchar_t* __restrict fmt,
1930  va_list args) NLIB_NONNULL_4;
1932  nlib_snwprintf(size_t* __restrict count, wchar_t* __restrict buf, size_t size,
1933  _Printf_format_string_ const wchar_t* __restrict fmt, ...) NLIB_NONNULL_4;
1934 NLIB_VIS_PUBLIC errno_t nlib_vdwprintf(nlib_fd fd, size_t* __restrict count,
1935  _Printf_format_string_ const wchar_t* __restrict fmt,
1936  va_list args) NLIB_NONNULL_3;
1937 NLIB_VIS_PUBLIC errno_t nlib_dwprintf(nlib_fd fd, size_t* __restrict count,
1938  _Printf_format_string_ const wchar_t* __restrict fmt,
1939  ...) NLIB_NONNULL_3;
1940 NLIB_VIS_PUBLIC int nlib_wprintf(_Printf_format_string_ const wchar_t* fmt, ...) NLIB_NONNULL_1;
1941 
1942 NLIB_VIS_PUBLIC errno_t nlib_vsnprintf_fallback(size_t* __restrict count, char* __restrict buf,
1943  size_t size,
1944  _Printf_format_string_ const char* __restrict fmt,
1945  va_list args) NLIB_NONNULL_4;
1947  nlib_snprintf_fallback(size_t* __restrict count, char* __restrict buf, size_t size,
1948  _Printf_format_string_ const char* __restrict fmt, ...)
1949  NLIB_ATTRIBUTE_PRINTF(4, 5) NLIB_NONNULL_4;
1951  nlib_vsnwprintf_fallback(size_t* __restrict count, wchar_t* __restrict buf, size_t size,
1952  _Printf_format_string_ const wchar_t* __restrict fmt,
1953  va_list args) NLIB_NONNULL_4;
1955  nlib_snwprintf_fallback(size_t* __restrict count, wchar_t* __restrict buf, size_t size,
1956  _Printf_format_string_ const wchar_t* __restrict fmt,
1957  ...) NLIB_NONNULL_4;
1958 
1959 // http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1078.pdf
1960 // http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1079.htm
1961 // a bit different from Microsoft's strcpy_s or etc.
1962 // returns ERANGE instead of EINVAL
1963 
1964 #ifdef NLIB_DOXYGEN
1965 errno_t nlib_memcpy(void* s1, size_t s1max, const void* s2, size_t n);
1966 errno_t nlib_memmove(void *s1, size_t s1max, const void *s2, size_t n);
1967 errno_t nlib_memset(void* buf, int ch, size_t n);
1968 #endif
1969 
1970 static errno_t nlib_memcpy(void* __restrict s1, size_t s1max, const void* __restrict s2,
1971  size_t n) NLIB_NONNULL;
1972 static errno_t nlib_memmove(void* s1, size_t s1max, const void* s2, size_t n) NLIB_NONNULL;
1973 static errno_t nlib_memset(void* buf, int ch, size_t n) NLIB_NONNULL;
1974 NLIB_VIS_PUBLIC_ALT int nlib_memcmp(const void* buf1, const void* buf2, size_t n) NLIB_NONNULL;
1975 NLIB_VIS_PUBLIC_ALT const void* nlib_memchr(const void* s, int c, size_t n) NLIB_NONNULL;
1976 NLIB_VIS_PUBLIC_ALT const void* nlib_memrchr(const void* s, int c, size_t n) NLIB_NONNULL;
1977 NLIB_VIS_PUBLIC_ALT const void* nlib_memchr_not(const void* s, int c, size_t n) NLIB_NONNULL;
1978 NLIB_VIS_PUBLIC_ALT const void* nlib_memchr_range_not(const void* __restrict s,
1979  const char* __restrict range,
1980  size_t n) NLIB_NONNULL;
1981 NLIB_VIS_PUBLIC_ALT const void* nlib_memchr_lt(const void* s, int c, size_t n) NLIB_NONNULL;
1982 NLIB_VIS_PUBLIC_ALT const void* nlib_memchr_gt(const void* s, int c, size_t n) NLIB_NONNULL;
1983 // find (c & 0x80) != 0
1984 NLIB_VIS_PUBLIC_ALT const void* nlib_memchr_mb(const void* s, size_t n) NLIB_NONNULL;
1985 NLIB_VIS_PUBLIC const void* nlib_memchr_set(const void* __restrict s, const char* __restrict set,
1986  size_t n) NLIB_NONNULL;
1987 NLIB_VIS_PUBLIC const void* nlib_memchr_set_not(const void* __restrict s,
1988  const char* __restrict set, size_t n) NLIB_NONNULL;
1989 // ' ', CR, LF, HT are skipped
1990 NLIB_VIS_PUBLIC_ALT const char* nlib_skipws(size_t* __restrict cnt_lf,
1991  const char** __restrict last_lf,
1992  const char* __restrict s, size_t n) NLIB_NONNULL_2;
1993 
1994 NLIB_VIS_PUBLIC_ALT size_t nlib_strlen(const char* s) NLIB_NONNULL;
1995 // see strnlen_s
1996 NLIB_VIS_PUBLIC_ALT size_t nlib_strnlen(const char* s, size_t maxsize) NLIB_NONNULL;
1997 // see strcpy_s
1999  nlib_strcpy(char* __restrict s1, size_t s1max, const char* __restrict s2) NLIB_NONNULL;
2000 // see strncpy_s
2001 NLIB_VIS_PUBLIC errno_t nlib_strncpy(char* __restrict s1, size_t s1max, const char* __restrict s2,
2002  size_t n) NLIB_NONNULL;
2003 NLIB_VIS_PUBLIC_ALT const char* nlib_strchr(const char* s, int c) NLIB_NONNULL;
2004 NLIB_VIS_PUBLIC_ALT const char* nlib_strrchr(const char* s, int c) NLIB_NONNULL;
2005 // find (c & 0x80) != 0, used for skipping ASCII chars
2006 NLIB_VIS_PUBLIC_ALT const char* nlib_strchr_mb(const char* s) NLIB_NONNULL;
2007 
2008 NLIB_VIS_PUBLIC size_t nlib_wcslen(const wchar_t* s) NLIB_NONNULL;
2009 NLIB_VIS_PUBLIC size_t nlib_wcsnlen(const wchar_t* s, size_t maxsize) NLIB_NONNULL; // wcsnlen_s
2010 NLIB_VIS_PUBLIC errno_t nlib_wcscpy(wchar_t* __restrict s1, size_t s1max,
2011  const wchar_t* __restrict s2) NLIB_NONNULL; // wcscpy_s
2012 NLIB_VIS_PUBLIC errno_t nlib_wcsncpy(wchar_t* __restrict s1, size_t s1max,
2013  const wchar_t* __restrict s2,
2014  size_t n) NLIB_NONNULL; // wcsncpy_s
2015 
2016 NLIB_VIS_PUBLIC errno_t nlib_strcat(char* __restrict s1, size_t s1max,
2017  const char* __restrict s2) NLIB_NONNULL; // strcat_s
2018 NLIB_VIS_PUBLIC errno_t nlib_strncat(char* __restrict s1, size_t s1max, const char* __restrict s2,
2019  size_t n) NLIB_NONNULL; // strncat_s
2020 
2021 NLIB_VIS_PUBLIC errno_t nlib_wcscat(wchar_t* __restrict s1, size_t s1max,
2022  const wchar_t* __restrict s2) NLIB_NONNULL; // wcscat_s
2023 NLIB_VIS_PUBLIC errno_t nlib_wcsncat(wchar_t* __restrict s1, size_t s1max,
2024  const wchar_t* __restrict s2,
2025  size_t n) NLIB_NONNULL; // wcsncat_s
2026 
2027 NLIB_VIS_PUBLIC errno_t nlib_wide_to_utf8(size_t* __restrict utf8count, char* __restrict utf8,
2028  size_t buflen,
2029  const wchar_t* __restrict wcstr) NLIB_NONNULL_4;
2030 NLIB_VIS_PUBLIC errno_t nlib_utf8_to_wide(size_t* __restrict wccount, wchar_t* __restrict wcstr,
2031  size_t buflen,
2032  const char* __restrict utf8) NLIB_NONNULL_4;
2033 
2035  nlib_strcplen(size_t* __restrict count, const char* __restrict str) NLIB_NONNULL_2;
2036 // *count = # of codepoints, *len = strlen(str)
2037 NLIB_VIS_PUBLIC_ALT NLIB_CHECK_RESULT errno_t
2038  nlib_strcplen_ex(size_t* __restrict count, size_t* __restrict len,
2039  const char* __restrict str) NLIB_NONNULL_3;
2041  nlib_wcscplen(size_t* __restrict count, const wchar_t* __restrict str) NLIB_NONNULL_2;
2042 
2043 // for (0..count) { swapendian(p[count]); }
2044 NLIB_VIS_PUBLIC_ALT errno_t nlib_swapendian_16(uint16_t* p, size_t count) NLIB_NONNULL;
2045 // for (0..count) { swapendian(p[count]); }
2046 NLIB_VIS_PUBLIC_ALT errno_t nlib_swapendian_32(uint32_t* p, size_t count) NLIB_NONNULL;
2047 // for (0..count) { swapendian(p[count]); }
2048 NLIB_VIS_PUBLIC_ALT errno_t nlib_swapendian_64(uint64_t* p, size_t count) NLIB_NONNULL;
2049 
2050 //
2051 // malloc functions which nlib uses
2052 // You can redefine them.
2053 // See sample replace_malloc.cpp of nlibnx_heap.a library.
2054 //
2055 
2056 // weak function
2057 NLIB_VIS_PUBLIC_ALT NLIB_CHECK_RESULT void* nlib_malloc(size_t size)
2058 NLIB_ATTRIBUTE_MALLOC NLIB_ATTRIBUTE_ALLOC_SIZE1(1);
2059 
2060 // weak function
2061 NLIB_VIS_PUBLIC_ALT void nlib_free(void* ptr);
2062 
2063 // weak function
2064 NLIB_VIS_PUBLIC_ALT NLIB_CHECK_RESULT void* nlib_calloc(size_t nmemb, size_t size)
2065 NLIB_ATTRIBUTE_MALLOC NLIB_ATTRIBUTE_ALLOC_SIZE2(1, 2);
2066 
2067 // weak function
2068 NLIB_VIS_PUBLIC_ALT NLIB_CHECK_RESULT void* nlib_realloc(void* ptr, size_t size)
2069 NLIB_ATTRIBUTE_MALLOC NLIB_ATTRIBUTE_ALLOC_SIZE1(2);
2070 
2071 // weak function(calls nlib_free(ptr) by default)
2072 NLIB_VIS_PUBLIC_ALT void nlib_free_size(void* ptr, size_t size);
2073 
2074 // weak function, not defined if WIN32 or CTR
2075 NLIB_VIS_PUBLIC_ALT NLIB_CHECK_RESULT void* nlib_memalign(size_t alignment, size_t size)
2076 NLIB_ATTRIBUTE_MALLOC NLIB_ATTRIBUTE_ALLOC_SIZE1(2) NLIB_ATTRIBUTE_ALLOC_ALIGN(1);
2077 
2078 #ifndef NLIB_MEMCPY
2079 # define NLIB_MEMCPY(a, b, c) memcpy((a), (b), (c))
2080 #endif
2081 
2082 #ifndef NLIB_MEMMOVE
2083 # define NLIB_MEMMOVE(a, b, c) memmove((a), (b), (c))
2084 #endif
2085 
2086 #ifndef NLIB_MEMSET
2087 # define NLIB_MEMSET(a, b, c) memset((a), (b), (c))
2088 #endif
2089 
2090 #ifndef NLIB_C_INLINE
2091 # define NLIB_C_INLINE __inline
2092 #endif
2093 
2094 // memcpy_s
2095 static NLIB_C_INLINE errno_t nlib_memcpy(void* s1, size_t s1max, const void* s2, size_t n) {
2096 #ifndef NLIB_NONNULL_ENABLED
2097  if (!s1 || !s2) return ERANGE;
2098 #endif
2099  if (s1max < n) {
2100  NLIB_MEMSET(s1, 0, s1max);
2101  return ERANGE;
2102  }
2103  NLIB_MEMCPY(s1, s2, n);
2104  return 0;
2105 }
2106 
2107 // memmove_s
2108 static NLIB_C_INLINE errno_t nlib_memmove(void *s1, size_t s1max, const void *s2, size_t n) {
2109 #ifndef NLIB_NONNULL_ENABLED
2110  if (!s1 || !s2) return ERANGE;
2111 #endif
2112  if (s1max < n) return ERANGE;
2113  NLIB_MEMMOVE(s1, s2, n);
2114  return 0;
2115 }
2116 
2117 static NLIB_C_INLINE errno_t nlib_memset(void* buf, int ch, size_t n) {
2118  NLIB_EINVAL_IFNULL(buf);
2119  NLIB_MEMSET(buf, ch, n);
2120  return 0;
2121 }
2122 
2123 #undef NLIB_MEMCPY
2124 #undef NLIB_MEMMOVE
2125 #undef NLIB_MEMSET
2126 
2127 #ifdef NLIB_DOXYGEN
2128 int nlib_clz64(uint64_t x);
2129 int nlib_ctz64(uint64_t x);
2130 int nlib_clz(uint32_t x);
2131 int nlib_ctz(uint32_t x);
2132 #endif
2133 
2134 NLIB_VIS_PUBLIC_ALT int nlib_popcnt(uint32_t x);
2135 NLIB_VIS_PUBLIC_ALT int nlib_popcnt64(uint64_t x);
2136 NLIB_VIS_PUBLIC int nlib_popcnt16(uint16_t x);
2137 
2138 #ifdef __cplusplus
2139 }
2140 #endif
2141 
2142 //
2143 // C++ functions in global namespace
2144 //
2145 #ifdef __cplusplus
2146 
2147 // code is only for C++ because uchar.h is rarely supported
2148 #if defined(__INTELLISENSE__)
2149 # define NLIB_NOEXCEPT
2150 #elif defined(NLIB_CXX11_NOEXCEPT)
2151 # define NLIB_NOEXCEPT noexcept
2152 #elif !defined(NLIB_NOEXCEPT)
2153 # define NLIB_NOEXCEPT throw()
2154 #endif
2155 
2156 #ifdef NLIB_CXX11_NEW_CHARACTER_TYPES
2157 typedef char16_t nlib_utf16_t;
2158 typedef char32_t nlib_utf32_t;
2159 #else
2160 typedef uint16_t nlib_utf16_t;
2161 typedef uint32_t nlib_utf32_t;
2162 #endif
2163 
2164 // 0 if error
2166  nlib_utf32_t* utf32, nlib_utf16_t upper, nlib_utf16_t lower) NLIB_NOEXCEPT NLIB_NONNULL;
2167 // 0 if error
2169  nlib_utf16_t* lower,
2170  nlib_utf32_t utf32) NLIB_NOEXCEPT;
2171 // 0 if error
2172 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT int nlib_utf8_to_utf32char(nlib_utf32_t* utf32, const char* utf8)
2173  NLIB_NOEXCEPT NLIB_NONNULL;
2174 // 0 if error
2176  nlib_utf32_t utf32) NLIB_NOEXCEPT;
2177 
2178 NLIB_VIS_PUBLIC errno_t nlib_utf16_to_utf8(size_t* utf8count, char* utf8, size_t buflen,
2179  const nlib_utf16_t* utf16) NLIB_NOEXCEPT NLIB_NONNULL_4;
2180 NLIB_VIS_PUBLIC errno_t nlib_utf8_to_utf16(size_t* utf16count, nlib_utf16_t* utf16, size_t buflen,
2181  const char* utf8) NLIB_NOEXCEPT NLIB_NONNULL_4;
2182 NLIB_VIS_PUBLIC errno_t nlib_utf32_to_utf8(size_t* utf8count, char* utf8, size_t buflen,
2183  const nlib_utf32_t* utf32) NLIB_NOEXCEPT NLIB_NONNULL_4;
2184 NLIB_VIS_PUBLIC errno_t nlib_utf8_to_utf32(size_t* utf32count, nlib_utf32_t* utf32, size_t buflen,
2185  const char* utf8) NLIB_NOEXCEPT NLIB_NONNULL_4;
2186 
2187 extern "C" NLIB_VIS_PUBLIC_ALT size_t nlib_utf16len_(const uint16_t* str) NLIB_NONNULL;
2188 extern "C" NLIB_VIS_PUBLIC_ALT size_t
2189  nlib_utf16nlen_(const uint16_t* str, size_t maxsize) NLIB_NONNULL;
2190 extern "C" NLIB_VIS_PUBLIC errno_t
2191  nlib_utf16cpy_(uint16_t* s1, size_t s1max, const uint16_t* s2) NLIB_NONNULL;
2192 extern "C" NLIB_VIS_PUBLIC errno_t
2193  nlib_utf16ncpy_(uint16_t* s1, size_t s1max, const uint16_t* s2, size_t n) NLIB_NONNULL;
2194 NLIB_CHECK_RESULT inline size_t nlib_utf16len(const nlib_utf16_t* str) NLIB_NOEXCEPT {
2195  return nlib_utf16len_(reinterpret_cast<const uint16_t*>(str));
2196 }
2197 NLIB_CHECK_RESULT inline size_t nlib_utf16nlen(const nlib_utf16_t* str, size_t maxsize)
2198  NLIB_NOEXCEPT {
2199  return nlib_utf16nlen_(reinterpret_cast<const uint16_t*>(str), maxsize);
2200 }
2201 inline errno_t nlib_utf16cpy(nlib_utf16_t* s1, size_t s1max,
2202  const nlib_utf16_t* s2) NLIB_NOEXCEPT {
2203  return nlib_utf16cpy_(reinterpret_cast<uint16_t*>(s1), s1max,
2204  reinterpret_cast<const uint16_t*>(s2));
2205 }
2206 inline errno_t nlib_utf16ncpy(nlib_utf16_t* s1, size_t s1max,
2207  const nlib_utf16_t* s2, size_t n) NLIB_NOEXCEPT {
2208  return nlib_utf16ncpy_(reinterpret_cast<uint16_t*>(s1), s1max,
2209  reinterpret_cast<const uint16_t*>(s2), n);
2210 }
2211 
2212 extern "C" NLIB_VIS_PUBLIC_ALT size_t nlib_utf32len_(const uint32_t* str) NLIB_NONNULL;
2213 extern "C" NLIB_VIS_PUBLIC_ALT size_t nlib_utf32nlen_(const uint32_t* str,
2214  size_t maxsize) NLIB_NONNULL;
2215 extern "C" NLIB_VIS_PUBLIC errno_t
2216  nlib_utf32cpy_(uint32_t* s1, size_t s1max, const uint32_t* s2) NLIB_NONNULL;
2217 extern "C" NLIB_VIS_PUBLIC errno_t
2218  nlib_utf32ncpy_(uint32_t* s1, size_t s1max, const uint32_t* s2, size_t n) NLIB_NONNULL;
2219 NLIB_CHECK_RESULT inline size_t nlib_utf32len(const nlib_utf32_t* str) NLIB_NOEXCEPT {
2220  return nlib_utf32len_(reinterpret_cast<const uint32_t*>(str));
2221 }
2222 NLIB_CHECK_RESULT inline size_t nlib_utf32nlen(const nlib_utf32_t* str, size_t maxsize)
2223  NLIB_NOEXCEPT {
2224  return nlib_utf32nlen_(reinterpret_cast<const uint32_t*>(str), maxsize);
2225 }
2226 inline errno_t nlib_utf32cpy(nlib_utf32_t* s1, size_t s1max,
2227  const nlib_utf32_t* s2) NLIB_NOEXCEPT {
2228  return nlib_utf32cpy_(reinterpret_cast<uint32_t*>(s1), s1max,
2229  reinterpret_cast<const uint32_t*>(s2));
2230 }
2231 inline errno_t nlib_utf32ncpy(nlib_utf32_t* s1, size_t s1max,
2232  const nlib_utf32_t* s2, size_t n) NLIB_NOEXCEPT {
2233  return nlib_utf32ncpy_(reinterpret_cast<uint32_t*>(s1), s1max,
2234  reinterpret_cast<const uint32_t*>(s2), n);
2235 }
2236 
2237 extern "C" NLIB_VIS_PUBLIC_ALT NLIB_CHECK_RESULT errno_t
2238  nlib_utf16cplen_ex_(size_t* count, size_t* len, const uint16_t* str) NLIB_NONNULL_3;
2239 
2241  size_t* count, const nlib_utf16_t* str) NLIB_NOEXCEPT {
2242  return nlib_utf16cplen_ex_(count, NULL, reinterpret_cast<const uint16_t*>(str));
2243 }
2245  size_t* count, size_t* len, const nlib_utf16_t* str) NLIB_NOEXCEPT {
2246  return nlib_utf16cplen_ex_(count, len, reinterpret_cast<const uint16_t*>(str));
2247 }
2249  nlib_utf32cplen(size_t* count, const nlib_utf32_t* str) NLIB_NOEXCEPT NLIB_NONNULL_2;
2250 
2251 template <size_t N>
2253  nlib_vsnprintf(size_t* count, char (&buf)[N], _Printf_format_string_ const char* fmt,
2254  va_list args) NLIB_NOEXCEPT {
2255  return nlib_vsnprintf(count, buf, N, fmt, args);
2256 }
2257 
2258 template<size_t N>
2259 inline NLIB_VIS_HIDDEN errno_t nlib_snprintf(size_t* count, char (&buf)[N],
2260  _Printf_format_string_ const char* fmt, ...) NLIB_NOEXCEPT {
2261  va_list args;
2262  va_start(args, fmt);
2263  errno_t e = nlib_vsnprintf(count, buf, N, fmt, args);
2264  va_end(args);
2265  return e;
2266 }
2267 
2268 template <size_t N>
2270  nlib_vsnwprintf(size_t* count, wchar_t (&buf)[N], _Printf_format_string_ const wchar_t* fmt,
2271  va_list args) NLIB_NOEXCEPT {
2272  return nlib_vsnwprintf(count, buf, N, fmt, args);
2273 }
2274 
2275 template<size_t N>
2276 inline NLIB_VIS_HIDDEN errno_t nlib_snwprintf(size_t* count, wchar_t(&buf)[N],
2277  _Printf_format_string_ const wchar_t* fmt, ...) NLIB_NOEXCEPT {
2278  va_list args;
2279  va_start(args, fmt);
2280  errno_t e = nlib_vsnwprintf(count, buf, N, fmt, args);
2281  va_end(args);
2282  return e;
2283 }
2284 
2285 template <size_t N>
2287  nlib_vsnprintf_fallback(size_t* count, char (&buf)[N], _Printf_format_string_ const char* fmt,
2288  va_list args) NLIB_NOEXCEPT {
2289  return nlib_vsnprintf_fallback(count, buf, N, fmt, args);
2290 }
2291 
2292 template<size_t N>
2293 inline NLIB_VIS_HIDDEN errno_t nlib_snprintf_fallback(size_t* count, char (&buf)[N],
2294  _Printf_format_string_ const char* fmt, ...) NLIB_NOEXCEPT {
2295  va_list args;
2296  va_start(args, fmt);
2297  errno_t e = nlib_vsnprintf_fallback(count, buf, N, fmt, args);
2298  va_end(args);
2299  return e;
2300 }
2301 
2302 template <size_t N>
2303 NLIB_ALWAYS_INLINE errno_t nlib_vsnwprintf_fallback(size_t* count, wchar_t (&buf)[N],
2304  _Printf_format_string_ const wchar_t* fmt,
2305  va_list args) NLIB_NOEXCEPT {
2306  return nlib_vsnwprintf_fallback(count, buf, N, fmt, args);
2307 }
2308 
2309 template<size_t N>
2310 inline NLIB_VIS_HIDDEN errno_t nlib_snwprintf_fallback(size_t* count, wchar_t(&buf)[N],
2311  _Printf_format_string_ const wchar_t* fmt, ...) NLIB_NOEXCEPT {
2312  va_list args;
2313  va_start(args, fmt);
2314  errno_t e = nlib_vsnwprintf_fallback(count, buf, N, fmt, args);
2315  va_end(args);
2316  return e;
2317 }
2318 
2319 template <size_t N>
2320 NLIB_ALWAYS_INLINE errno_t nlib_strcpy(char (&s1)[N], const char* s2) NLIB_NOEXCEPT {
2321  return nlib_strcpy(&s1[0], N, s2);
2322 }
2323 
2324 template <size_t N>
2325 NLIB_ALWAYS_INLINE errno_t nlib_strncpy(char (&s1)[N], const char* s2, size_t n) NLIB_NOEXCEPT {
2326  return nlib_strncpy(&s1[0], N, s2, n);
2327 }
2328 
2329 template <size_t N>
2330 NLIB_ALWAYS_INLINE errno_t nlib_wcscpy(wchar_t (&s1)[N], const wchar_t* s2) NLIB_NOEXCEPT {
2331  return nlib_wcscpy(&s1[0], N, s2);
2332 }
2333 
2334 template <size_t N>
2336  nlib_wcsncpy(wchar_t (&s1)[N], const wchar_t* s2, size_t n) NLIB_NOEXCEPT {
2337  return nlib_wcsncpy(&s1[0], N, s2, n);
2338 }
2339 
2340 template <size_t N>
2341 NLIB_ALWAYS_INLINE errno_t nlib_strcat(char (&s1)[N], const char* s2) NLIB_NOEXCEPT {
2342  return nlib_strcat(&s1[0], N, s2);
2343 }
2344 
2345 template <size_t N>
2346 NLIB_ALWAYS_INLINE errno_t nlib_strncat(char (&s1)[N], const char* s2, size_t n) NLIB_NOEXCEPT {
2347  return nlib_strncat(&s1[0], N, s2, n);
2348 }
2349 
2350 template <size_t N>
2351 NLIB_ALWAYS_INLINE errno_t nlib_wcscat(wchar_t (&s1)[N], const wchar_t* s2) NLIB_NOEXCEPT {
2352  return nlib_wcscat(&s1[0], N, s2);
2353 }
2354 
2355 template <size_t N>
2357  nlib_wcsncat(wchar_t (&s1)[N], const wchar_t* s2, size_t n) NLIB_NOEXCEPT {
2358  return nlib_wcsncat(&s1[0], N, s2, n);
2359 }
2360 
2361 template<size_t N>
2363  size_t* result, char (&utf8)[N], const wchar_t* wcstr) NLIB_NOEXCEPT {
2364  return nlib_wide_to_utf8(result, &utf8[0], N, wcstr);
2365 }
2366 
2367 template<size_t N>
2369  size_t* result, wchar_t (&wcstr)[N], const char* utf8) NLIB_NOEXCEPT {
2370  return nlib_utf8_to_wide(result, &wcstr[0], N, utf8);
2371 }
2372 
2373 template<size_t N>
2375  size_t* utf8count, char (&utf8)[N], const nlib_utf16_t* utf16) NLIB_NOEXCEPT {
2376  return nlib_utf16_to_utf8(utf8count, &utf8[0], N, utf16);
2377 }
2378 
2379 template<size_t N>
2381  size_t* utf16count, nlib_utf16_t (&utf16)[N], const char* utf8) NLIB_NOEXCEPT {
2382  return nlib_utf8_to_utf16(utf16count, &utf16[0], N, utf8);
2383 }
2384 
2385 template<size_t N>
2387  size_t* utf8count, char (&utf8)[N], const nlib_utf32_t* utf32) NLIB_NOEXCEPT {
2388  return nlib_utf32_to_utf8(utf8count, &utf8[0], N, utf32);
2389 }
2390 
2391 template<size_t N>
2393  size_t* utf32count, nlib_utf32_t(&utf32)[N], const char* utf8) NLIB_NOEXCEPT {
2394  return nlib_utf8_to_utf32(utf32count, &utf32[0], N, utf8);
2395 }
2396 
2397 #if !defined(NN_PLATFORM_CTR) && !defined(CAFE)
2398 // This may throw C++ exception defined by nlib, See also nlib_thread_exit()
2399 NLIB_VIS_PUBLIC void nlib_thread_exit_cpp() NLIB_NORETURN;
2400 #endif
2401 
2402 #endif
2403 
2404 #endif // INCLUDE_NN_NLIB_PLATFORM_H_
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Platform.h:2151
int32_t nlib_atomic_xor_fetch32(int32_t *ptr, int32_t val, int memorder)
アトミックな値の排他的論理和の計算を行います。動作はgccの__atomic_xor_fetch()に準じます。 ...
errno_t nlib_debug_backtrace_gettext(char *str, size_t strbufsize, void *const *buf, size_t count)
nlib_debug_backtrace()で得られた情報から文字列情報を作成します。
errno_t nlib_thread_priority_max(int *priority)
実行優先度に指定できる数値の最大値を取得します。
errno_t nlib_log_attr_setint(int prio, int key, int value)
優先度ごとの出力先の指定を行います。
int64_t nlib_atomic_fetch_and64(int64_t *ptr, int64_t val, int memorder)
アトミックな値の論理積の計算を行います。動作はgccの__atomic_fetch_and()に準じます。 ...
const char * nlib_strrchr(const char *s, int c)
文字列の末尾から文字を検索します。
const char * nlib_strchr_mb(const char *s)
文字列の先頭から文字を検索して、ヌル文字又は0x80-0xFFのバイトへのポインタを返します。 ...
int nlib_thread_equal(nlib_thread th1, nlib_thread th2)
2つのスレッドが同一スレッドを指すかどうかチェックします。
errno_t nlib_utf8_to_utf16(size_t *utf16count, nlib_utf16_t *utf16, size_t buflen, const char *utf8) noexcept
UTF-8文字列からUTF-16文字列に変換します。UTF-16文字列はヌル終端されます。
errno_t nlib_semaphore_post_ex(nlib_semaphore *sem, int release_count, int *previous_count)
セマフォカウントをreleaseCount 増加させる。
errno_t nlib_condrwlock_wait(nlib_condrwlock *cond, nlib_rwlock *rwlock, int rdlock)
rwlockをアンロックし、条件変数を待機します。実行が再開したらrwlockを再ロックします。 ...
NLIB_CHECK_RESULT errno_t nlib_wcscplen(size_t *count, const wchar_t *str)
文字列中のコードポイントの数を取得します。
指定した優先度との論理和をとると、指定した優先度かそれ以上の優先度を指定したことになります。 nlib_log...
Definition: Platform.h:969
void nlib_thread_exit_cpp() NLIB_NORETURN
呼び出しスレッドを終了します。
NLIB_CHECK_RESULT errno_t nlib_fd_write(size_t *result, nlib_fd fd, const void *buf, size_t count)
ファイルディスクリプタへ、(最大)count バイトをbuf から書きこみます。
int nlib_atomic_compare_exchangeptr(void **ptr, void **expected, void *desired, int weak, int success_memorder, int failure_memorder)
アトミックな値の比較と入れ替えを行います。動作はgccの__atomic_compare_exchange_n()に準じます。 ...
errno_t nlib_rwlock_destroy(nlib_rwlock *rwlock)
リードライトロックオブジェクトを破壊します。
int32_t nlib_atomic_load32(const int32_t *ptr, int memorder)
アトミックに値をロードします。動作はgccの__atomic_load_n()に準じます。
errno_t nlib_virtual_alloc(void **ptr, size_t size)
仮想メモリアドレス空間を割り当てます。
errno_t nlib_dir_close(nlib_dir dir)
ディレクトリをクローズする
errno_t nlib_vdwprintf(nlib_fd fd, size_t *count, const wchar_t *fmt, va_list args)
ファイルディスクリプタに出力するvsnwprintfです。
NLIB_CHECK_RESULT errno_t nlib_fd_preadv(size_t *result, nlib_fd fd, const nlib_fd_iovec *iov, int iovcnt, nlib_offset offset)
内部でpread()又はnlib_fd_pread()を使うこと以外は、nlib_fd_readv()と同様です。
NLIB_CHECK_RESULT void * nlib_calloc(size_t nmemb, size_t size)
C標準関数のcalloc()を呼び出すweak関数です。nlibはこの関数を経由してcalloc()を呼び出します。 ...
int nlib_wprintf(const wchar_t *fmt,...)
wprintf()の代替です。
errno_t nlib_condrwlock_wait_until(nlib_condrwlock *cond, nlib_rwlock *rwlock, nlib_time abstime, int rdlock)
rwlockをアンロックし、条件変数を待機します。実行が再開したらrwlockを再ロックします。 ...
errno_t nlib_wcsncat(wchar_t *s1, size_t s1max, const wchar_t *s2, size_t n)
N1078のwcsncat_sに相当する実装です。
int64_t nlib_atomic_fetch_add64(int64_t *ptr, int64_t val, int memorder)
アトミックな値の加算を行います。動作はgccの__atomic_fetch_add()に準じます。
errno_t nlib_strncat(char *s1, size_t s1max, const char *s2, size_t n)
N1078のstrncat_sに相当する実装です。
errno_t nlib_utf16cplen_ex(size_t *count, size_t *len, const nlib_utf16_t *str) noexcept
文字列中のコードポイントの数を取得します。
Definition: Platform.h:2244
const void * nlib_memchr_lt(const void *s, int c, size_t n)
メモリ領域[s, s + n)の先頭からn バイトを検索して、バイトc 未満の文字があるデータへのポインタを返しま...
struct nlib_rwlock_ nlib_rwlock
リードライトロックオブジェクトの型です。
Definition: Platform.h:689
errno_t nlib_vdprintf(nlib_fd fd, size_t *count, const char *fmt, va_list args)
ファイルディスクリプタに出力するvsnprintf()です。
errno_t nlib_cond_signal(nlib_cond *cond)
条件変数cond を待っているスレッドの1つの実行を再開させます。
errno_t nlib_thread_priority_default(int *priority)
実行優先度に指定できる数値のデフォルト値を取得します。
nlib_mq_msg_destructor destructor
メッセージキューから取り出したメッセージのデストラクタ関数を設定、取得できます。
Definition: Platform.h:801
errno_t nlib_vsnprintf(size_t *count, char *buf, size_t size, const char *fmt, va_list args)
より安全な形式のvsnprintfで、標準のvsnprintfの動作の違いも吸収します。
errno_t nlib_mq_close(nlib_mq mq)
ハンドルで示されるメッセージキューをクローズします。
errno_t nlib_wcscpy(wchar_t *s1, size_t s1max, const wchar_t *s2)
N1078のwcscpy_sに相当する実装です。
errno_t nlib_ticktime(nlib_duration *t)
ブートからの経過時間を取得します。
NLIB_CHECK_RESULT void * nlib_memalign(size_t alignment, size_t size)
memalign()を呼び出すweak関数です。nlibはこの関数を経由してmemalign()を呼び出します。 ...
#define NLIB_NONNULL
全ての引数にNULLを指定することができないことを示します。
Definition: Platform_unix.h:66
NLIB_CHECK_RESULT int nlib_utf32char_to_utf8(char(&utf8)[4], nlib_utf32_t utf32) noexcept
1文字のUTF-32をUTF-8に変換します。
NLIB_CHECK_RESULT errno_t nlib_mq_receive(nlib_mq mq, nlib_mq_msg *msg, int *prio)
メッセージをキューから受信します。受信したメッセージはユーザーがデストラクタ関数で削除する必要があり...
指定した優先度との論理和をとると、指定した優先度かそれ以下の優先度を指定したことになります。 nlib_log...
Definition: Platform.h:970
sem_t nlib_semaphore
セマフォオブジェクトの型です。
NLIB_CHECK_RESULT errno_t nlib_fd_readv(size_t *result, nlib_fd fd, const nlib_fd_iovec *iov, int iovcnt)
fdに関連付けられたファイルから複数の非連続なバッファへの読み込みを行います。
errno_t nlib_rwlock_rdlock(nlib_rwlock *rwlock)
読み込みロックを取得しクリティカルセクションに入ります。取得できるまでブロックします。 ...
errno_t nlib_mlock(void *addr, size_t len)
指定したメモリ領域がスワップアウトされないようにします。
NLIB_CHECK_RESULT errno_t nlib_mutex_trylock_for(nlib_mutex *mutex, nlib_duration delta)
与えられたmutexをロックします。タイムアウトします。
NLIB_CHECK_RESULT errno_t nlib_semaphore_trywait_for(nlib_semaphore *sem, nlib_duration duration)
セマフォカウントが0でなければ、セマフォカウントを1減少させる。0の場合はduration の期間だけ待つ。 ...
size_t nlib_strnlen(const char *s, size_t maxsize)
N1078のstrnlen_sに相当する実装です。
#define NLIB_ATTRIBUTE_MALLOC
利用可能であれば__attribute__((malloc))が定義されます。
Definition: Platform_unix.h:73
errno_t nlib_barrier_destroy(nlib_barrier *barrier)
バリアオブジェクトを破壊します。
int nlib_memcmp(const void *buf1, const void *buf2, size_t n)
buf1 とbuf2 を先頭からn バイト分unsigned charとして比較します。
nlib_log_priority
出力の優先度(種類)を定義しています。
Definition: Platform.h:959
errno_t nlib_exist_path(int *result, const char *native_path)
パスが存在するかどうかを検査します。
errno_t nlib_cond_broadcast(nlib_cond *cond)
条件変数cond を待っているスレッド全ての実行を再開させます。
errno_t nlib_virtual_free(void *ptr, size_t size)
仮想メモリアドレス空間の割り当てを解除します
const void * nlib_memchr_not(const void *s, int c, size_t n)
メモリ領域[s, s + n)の先頭からn バイトを検索して、バイトc でないポインタを返します。 ...
errno_t nlib_thread_setaffinity(nlib_thread thread, uint32_t affinity)
指定されたスレッドのプロセッサアフィニティマスクを設定します。
int32_t nlib_atomic_or_fetch32(int32_t *ptr, int32_t val, int memorder)
アトミックな値の論理和の計算を行います。動作はgccの__atomic_or_fetch()に準じます。 ...
NLIB_CHECK_RESULT errno_t nlib_dir_open(nlib_dir *dir, const char *native_path)
ディレクトリをオープンする
#define NLIB_ATOMIC_RELEASE
gccの__ATOMIC_RELEASEやC++11のstd::memory_order_releaseに準じます。
#define NLIB_VIS_HIDDEN
関数やクラス等のシンボルをライブラリの外部に公開しません。
Definition: Platform_unix.h:50
const char * nlib_skipws(size_t *cnt_lf, const char **last_lf, const char *s, size_t n)
n 個の文字から成る文字列を探索して最初の空白でない文字へのポインタを返します。
NLIB_CHECK_RESULT errno_t nlib_strcplen(size_t *count, const char *str)
文字列中のコードポイントの数を取得します。
errno_t nlib_mutex_lock(nlib_mutex *mutex)
与えられたmutexをロックします。
errno_t nlib_cond_init(nlib_cond *cond)
条件変数を初期化します。
errno_t nlib_rwlock_trywrlock_until(nlib_rwlock *rwlock, nlib_time abstime)
書き込みロックを取得しクリティカルセクションに入ることを試みます。タイムアウトします。 ...
int nlib_popcnt16(uint16_t x)
1となっているビットの数を返します。
struct nlib_thread_attr_ nlib_thread_attr
新しく作られるスレッドに適用されるスレッド属性
Definition: Platform.h:849
int64_t nlib_atomic_fetch_sub64(int64_t *ptr, int64_t val, int memorder)
アトミックな値の減算を行います。動作はgccの__atomic_fetch_sub()に準じます。
errno_t nlib_thread_self(nlib_thread *thread)
実行中のスレッドに対応するnlib_threadの値を格納する。
void nlib_thread_cleanup_push(void(*fn)(void *), void *arg)
fnを専用のスタックにプッシュします。
errno_t nlib_mutex_unlock(nlib_mutex *mutex)
与えられたmutex をアンロックします。
エラーレベルのメッセージを出力するときに指定します。
Definition: Platform.h:966
void nlib_free_size(void *ptr, size_t size)
サイズを指定してメモリを解放します。デフォルトではnlib_free()を呼び出します。
NLIB_CHECK_RESULT errno_t nlib_mq_receive_until(nlib_mq mq, nlib_mq_msg *msg, int *prio, nlib_time abstime)
メッセージをキューからタイムアウトつきで受信します。受信したメッセージはユーザーがデストラクタ関数で...
NLIB_CHECK_RESULT errno_t nlib_rename(const char *old_path, const char *new_path)
ファイル名の変更する
NLIB_CHECK_RESULT errno_t nlib_fd_getsize(nlib_offset *size, nlib_fd fd)
ファイルサイズを取得します。
errno_t nlib_thread_priority_min(int *priority)
実行優先度に指定できる数値の最小値を取得します。
NLIB_CHECK_RESULT errno_t nlib_dir_read(nlib_dirent *ent, nlib_dir dir)
ディレクトリエントリがあればそれをを1つ読み込む。
uint32_t nlib_ulong_compatible_t
unsigned longと互換性のある整数型がtypedefされています。
Definition: Platform.h:425
errno_t nlib_thread_getid(nlib_thread_id *id)
実行中のスレッドに対応する一意の整数値を格納する。
int64_t nlib_atomic_and_fetch64(int64_t *ptr, int64_t val, int memorder)
アトミックな値の論理積の計算を行います。動作はgccの__atomic_and_fetch()に準じます。 ...
NLIB_CHECK_RESULT errno_t nlib_mq_open(nlib_mq *mq, const nlib_mq_attr *attr)
スレッド間でメッセージをやりとりするためのメッセージキューを作成します。
errno_t nlib_once(nlib_onceflag *flag, nlib_oncefunc func)
func を高々1回しか実行されないようします。
int64_t nlib_time
1970/01/01を起点(0)としてから100ns刻みで時刻を表現する型です。64bit符号付き整数です。 ...
Definition: Platform.h:533
errno_t nlib_condrwlock_wait_for(nlib_condrwlock *cond, nlib_rwlock *rwlock, nlib_duration duration, int rdlock)
rwlockをアンロックし、条件変数を待機します。実行が再開したらrwlockを再ロックします。 ...
NLIB_CHECK_RESULT errno_t nlib_cond_wait_for(nlib_cond *cond, nlib_mutex *mutex, nlib_duration duration)
mutexをアンロックし、条件変数をduration の間だけ待機します。実行が再開したらmutexを再ロックします。 ...
errno_t nlib_condrwlock_signal(nlib_condrwlock *cond)
リードライトロック用条件変数cond を待っているスレッドの1つの実行を再開させます。
void nlib_debug_break(void)
ブレークポイントになります。
NLIB_CHECK_RESULT errno_t nlib_fd_close(nlib_fd fd)
ファイルをクローズします。エラーを返した場合でもファイルディスクリプタは解放されます。 ...
errno_t nlib_thread_attr_setint(nlib_thread_attr *attr, int key, int value)
スレッドの属性オブジェクトのキーに対応する整数を設定する。
int nlib_atomic_compare_exchange64(int64_t *ptr, int64_t *expected, int64_t desired, int weak, int success_memorder, int failure_memorder)
アトミックな値の比較と入れ替えを行います。動作はgccの__atomic_compare_exchange_n()に準じます。 ...
int nlib_ctz(uint32_t x)
LSB(least significant bit)から見て連続する0ビットの数を返します。
NLIB_CHECK_RESULT size_t nlib_utf16nlen(const nlib_utf16_t *str, size_t maxsize) noexcept
nlib_strnlen()のUTF-16版です。
Definition: Platform.h:2197
errno_t nlib_tryonce(nlib_onceflag *flag, nlib_oncefunc func)
nlib_onceと概ね同じですが、func が他のスレッドで実行中だった場合、EBUSYを返します。 ...
void(* nlib_oncefunc)(void)
nlib_onceで実行される関数の型
Definition: Platform.h:769
errno_t nlib_physical_free(void *ptr, size_t size)
物理メモリの割り当てを解除します。
void nlib_thread_cleanup_pop(int exec)
クリーンアップハンドラが格納されているスタックの一番上のハンドラを削除します。
const char * nlib_strchr(const char *s, int c)
文字列の先頭から文字を検索します。
errno_t nlib_rwlock_tryrdlock(nlib_rwlock *rwlock)
読み込みロックを取得しクリティカルセクションに入ることを試みます。
errno_t nlib_tls_setvalue(nlib_tls tls, const void *value)
TLSスロットに値を格納します。
errno_t nlib_thread_getconcurrency(unsigned int *num_cpu)
ハードウェアスレッドの数を取得します。
NLIB_CHECK_RESULT errno_t nlib_mq_drop(nlib_mq mq, nlib_mq_msg *msg, int *prio)
キューに存在する最低の優先度のメッセージをキューから受信します。受信したメッセージはユーザーがデスト...
NLIB_CHECK_RESULT errno_t nlib_gen_random(void *buf, size_t size)
ランダムな値をsize バイト生成してbuf に格納します。
int64_t nlib_atomic_fetch_or64(int64_t *ptr, int64_t val, int memorder)
アトミックな値の論理和の計算を行います。動作はgccの__atomic_fetch_or()に準じます。 ...
NLIB_CHECK_RESULT errno_t nlib_fd_flush(nlib_fd fd)
ファイルディスクリプタへの書き込みをフラッシュします。
int32_t max_msg
メッセージキューの作成の際に最大メッセージ数を設定することができます。
Definition: Platform.h:799
int nlib_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
ログメッセージを出力します。
errno_t nlib_strncpy(char *s1, size_t s1max, const char *s2, size_t n)
N1078のstrncpy_sに相当する実装です。
errno_t nlib_cond_wait(nlib_cond *cond, nlib_mutex *mutex)
mutexをアンロックし、条件変数を待機します。実行が再開したらmutexを再ロックします。 ...
#define NLIB_ASSUME(cond)
cond が真であることを示してコンパイラに最適化のヒントを与えます。
Definition: Platform.h:393
NLIB_CHECK_RESULT void * nlib_malloc(size_t size)
C標準関数のmalloc()を呼び出すweak関数です。nlibはこの関数を経由してmalloc()を呼び出します。 ...
const void * nlib_memchr(const void *s, int c, size_t n)
メモリ領域[s, s + n)の先頭からn バイトを検索して、バイトc があるポインタを返します。 ...
errno_t nlib_is_dir(int *result, const char *native_path)
パスがディレクトリかどうかを検査します。パスが存在しない場合は*resultに0を設定し、0を返します。 ...
errno_t nlib_mempagesize(size_t *size)
ページサイズを取得します。
errno_t nlib_mutex_destroy(nlib_mutex *mutex)
mutexオブジェクトを破壊し、関連付けられているリソース(あれば)を解放します。
errno_t nlib_rwlock_trywrlock(nlib_rwlock *rwlock)
書き込みロックを取得しクリティカルセクションに入ることを試みます。
errno_t nlib_condrwlock_init(nlib_condrwlock *cond)
リードライトロック用条件変数を初期化します。
errno_t nlib_mq_readonly(nlib_mq mq)
ハンドルで示されるメッセージキューを受信専用にします。
int nlib_clz(uint32_t x)
MSB(most significant bit)から見て連続する0ビットの数を返します。
int nlib_popcnt(uint32_t x)
1となっているビットの数を返します。
pthread_key_t nlib_tls
TLSスロットのIDを示す型です。
Definition: Platform.h:571
uint32_t nlib_utf32_t
char32_tが利用できる場合はchar32_tに、そうでない場合はuint32_tにtypedefされます。 ...
Definition: Platform.h:2161
errno_t nlib_yield(void)
スレッドの実行権を手放す。
errno_t nlib_debug_backtrace(size_t *result, void **buffer, size_t count)
バックトレースをbuffer が指す配列に格納します。
errno_t nlib_write_stdout(size_t *result, const void *buf, size_t count)
標準出力に文字列を書き出します。
NLIB_CHECK_RESULT errno_t nlib_tls_alloc(nlib_tls *tls, nlib_tls_destructor destr)
TLSスロットに対する新しいIDを確保します。
int nlib_fd
(nlib独自の)ファイルディスクリプタで、32bit整数です。
Definition: Platform.h:1051
errno_t nlib_utf16ncpy(nlib_utf16_t *s1, size_t s1max, const nlib_utf16_t *s2, size_t n) noexcept
nlib_strcpy()のUTF-16版です。
Definition: Platform.h:2206
int32_t nlib_mq
メッセージキューに関連付けられるハンドルです。ハンドルがゼロクリア(memset()を利用してください)された...
Definition: Platform.h:783
void * nlib_atomic_loadptr(void *const *ptr, int memorder)
アトミックに値をロードします。動作はgccの__atomic_load_n()に準じます。
int64_t nlib_atomic_exchange64(int64_t *ptr, int64_t val, int memorder)
アトミックに値を入れ替えます。動作はgccの__atomic_exchange_n()に準じます。
NLIB_CHECK_RESULT errno_t nlib_mq_getattr(nlib_mq mq, nlib_mq_attr *attr)
ハンドルで示されるメッセージキューに設定されている属性を取得します。
デバッグレベルのメッセージを出力するときに指定します。
Definition: Platform.h:963
uint16_t nlib_utf16_t
char16_tが利用できる場合はchar16_tに、そうでない場合はuint16_tにtypedefされます。 ...
Definition: Platform.h:2160
NLIB_CHECK_RESULT int nlib_utf8_to_utf32char(nlib_utf32_t *utf32, const char *utf8) noexcept
UTF-8を1文字分のUTF-32に変換します。
errno_t nlib_utf16cplen(size_t *count, const nlib_utf16_t *str) noexcept
文字列中のコードポイントの数を取得します。
Definition: Platform.h:2240
NLIB_CHECK_RESULT errno_t nlib_mkdir(const char *native_path, unsigned int flags)
ディレクトリを作成する
int nlib_printf(const char *fmt,...)
printf()の代替です。
errno_t nlib_disk_freespace(const char *native_path, uint64_t *free_bytes_available, uint64_t *total_bytes, uint64_t *total_free_bytes)
指定されたパスが属するストレージの容量に関する情報を取得します。
int nlib_getversion(void)
nlibのバージョンを動的に取得します。
errno_t nlib_memset(void *buf, int ch, size_t n)
内部でmemset(buf, ch, n)相当の関数を呼び出します。
Definition: Platform.h:2117
errno_t nlib_utf32_to_utf8(size_t *utf8count, char *utf8, size_t buflen, const nlib_utf32_t *utf32) noexcept
UTF-32文字列からUTF-8文字列に変換します。
errno_t nlib_rwlock_wrunlock(nlib_rwlock *rwlock)
書き込みロックを解放します。
const void * nlib_memrchr(const void *s, int c, size_t n)
メモリ領域[s, s + n)の後方からn バイトを検索して、バイトc があるポインタを返します。 ...
int64_t nlib_atomic_sub_fetch64(int64_t *ptr, int64_t val, int memorder)
アトミックな値の減算を行います。動作はgccの__atomic_sub_fetch()に準じます。
errno_t nlib_memmove(void *s1, size_t s1max, const void *s2, size_t n)
N1078のmemmove_sに相当する実装です。
Definition: Platform.h:2108
errno_t nlib_thread_attr_destroy(nlib_thread_attr *attr)
スレッド初期化オブジェクトを破壊します。
int32_t nlib_atomic_fetch_xor32(int32_t *ptr, int32_t val, int memorder)
アトミックな値の排他的論理和の計算を行います。動作はgccの__atomic_fetch_xor()に準じます。 ...
const char * nlib_error_string(errno_t e)
nlibのエラー値に対応する文字列リテラルを返します。
struct nlib_barrier_ nlib_barrier
バリアオブジェクトの型です。
Definition: Platform.h:754
errno_t nlib_utf8_to_wide(size_t *wccount, wchar_t *wcstr, size_t buflen, const char *utf8)
UTF-8文字列からUTF-16/UTF-32文字列に変換します。
struct nlib_condrwlock_ nlib_condrwlock
リードライトロック用の条件変数の型です。
#define NLIB_NONNULL_1
1番目の引数にNULLを指定することができないことを示します。
Definition: Platform_unix.h:67
警告レベルのメッセージを出力するときに指定します。
Definition: Platform.h:965
int32_t nlib_atomic_sub_fetch32(int32_t *ptr, int32_t val, int memorder)
アトミックな値の減算を行います。動作はgccの__atomic_sub_fetch()に準じます。
errno_t nlib_physical_alloc(void *ptr, size_t size, int prot)
物理メモリを割り当てます。
int32_t nlib_atomic_fetch_sub32(int32_t *ptr, int32_t val, int memorder)
アトミックな値の減算を行います。動作はgccの__atomic_fetch_sub()に準じます。
NLIB_CHECK_RESULT errno_t nlib_semaphore_trywait(nlib_semaphore *sem)
セマフォカウントが0でなければ、セマフォカウントを1減少させる。
int32_t nlib_atomic_add_fetch32(int32_t *ptr, int32_t val, int memorder)
アトミックな値の加算を行います。動作はgccの__atomic_add_fetch()に準じます。
void nlib_atomic_storeptr(void **ptr, void *val, int memorder)
アトミックに値をストアします。動作はgccの__atomic_store_n()に準じます。
NLIB_CHECK_RESULT errno_t nlib_mq_send_until(nlib_mq mq, nlib_mq_msg msg, int prio, nlib_time abstime)
メッセージをキューにタイムアウトつきで送信します。
errno_t nlib_cond_destroy(nlib_cond *cond)
条件変数オブジェクトを破壊します。
errno_t nlib_epochtime(nlib_time *t)
現在時刻を取得します。
#define NLIB_NONNULL_4
4番目の引数にNULLを指定することができないことを示します。
Definition: Platform_unix.h:70
致命的なレベルのメッセージを出力するときに指定します。
Definition: Platform.h:967
errno_t nlib_snwprintf(size_t *count, wchar_t *buf, size_t size, const wchar_t *fmt,...)
より安全な形式のsnwprintfです。
NLIB_CHECK_RESULT size_t nlib_utf32nlen(const nlib_utf32_t *str, size_t maxsize) noexcept
nlib_strnlen()のUTF-32版です。
Definition: Platform.h:2222
errno_t nlib_thread_attr_getint(const nlib_thread_attr *attr, int key, int *value)
スレッドの属性オブジェクトのキーに対応する整数を取得する。
void nlib_atomic_thread_fence(int memorder)
指定されたメモリバリアを配置します。
メッセージキューの設定や現在の状態を格納する構造体です。
Definition: Platform.h:797
int32_t nlib_atomic_fetch_and32(int32_t *ptr, int32_t val, int memorder)
アトミックな値の論理積の計算を行います。動作はgccの__atomic_fetch_and()に準じます。 ...
errno_t nlib_dwprintf(nlib_fd fd, size_t *count, const wchar_t *fmt,...)
ファイルディスクリプタに出力するsnwprintf()です。
errno_t nlib_write_stderr(size_t *result, const void *buf, size_t count)
標準エラー出力に文字列を書き出します。
errno_t nlib_thread_attr_init(nlib_thread_attr *attr)
スレッド属性オブジェクトを初期化して、デフォルトに設定する。
void(* nlib_thread_func)(void *arg)
別スレッドで実行される関数
Definition: Platform.h:857
pthread_cond_t nlib_cond
条件変数オブジェクトの型です。
errno_t nlib_rwlock_tryrdlock_until(nlib_rwlock *rwlock, nlib_time abstime)
読み込みロックを取得しクリティカルセクションに入ることを試みます。タイムアウトします。 ...
size_t nlib_wcslen(const wchar_t *s)
内部でwcslen()を呼び出します。独自の実装が動作する場合もあります。
errno_t nlib_semaphore_init(nlib_semaphore *sem, int initial_count)
sem で指定されるセマフォオブジェクトを初期化する。
errno_t nlib_condrwlock_destroy(nlib_condrwlock *cond)
リードライトロック用条件変数を破壊します。
errno_t nlib_utf32ncpy(nlib_utf32_t *s1, size_t s1max, const nlib_utf32_t *s2, size_t n) noexcept
nlib_strcpy()のUTF-32版です。
Definition: Platform.h:2231
errno_t nlib_wcsncpy(wchar_t *s1, size_t s1max, const wchar_t *s2, size_t n)
N1078のwcsncpy_sに相当する実装です。
NLIB_CHECK_RESULT errno_t nlib_mutex_trylock(nlib_mutex *mutex)
mutexがロックされていない場合のみロックします。
errno_t nlib_sleep(nlib_duration t)
t の間スリープする。
errno_t nlib_semaphore_wait(nlib_semaphore *sem)
セマフォカウントが0でなくなるまで待って、セマフォカウントを1減少させる。
errno_t nlib_getenv(size_t *result, char *buf, size_t bufsize, const char *varname)
環境変数の値を文字列で取得します。
errno_t nlib_strcat(char *s1, size_t s1max, const char *s2)
N1078のstrcat_sに相当する実装です。
errno_t nlib_thread_attr_getptr(const nlib_thread_attr *attr, int key, void **value)
スレッドの属性オブジェクトのキーに対応するポインタを取得する。現在のところEINVALのみを返します。 ...
#define NLIB_ATOMIC_ACQUIRE
gccの__ATOMIC_ACQUIREやC++11のstd::memory_order_acquireに準じます。
NLIB_CHECK_RESULT void * nlib_realloc(void *ptr, size_t size)
C標準関数のrealloc()を呼び出すweak関数です。nlibはこの関数を経由してrealloc()を呼び出します。 ...
NLIB_CHECK_RESULT errno_t nlib_mq_send(nlib_mq mq, nlib_mq_msg msg, int prio)
メッセージをキューに送信します。
NLIB_CHECK_RESULT errno_t nlib_fd_pread(size_t *result, nlib_fd fd, void *buf, size_t count, nlib_offset offset)
指定したオフセットでファイルディスクリプタから読みこみます。ファイルディスクリプタのオフセットは変更...
int32_t nlib_spinlock
スピンロック変数の型です。NLIB_SPINLOCK_INITIALIZERにより静的に初期化して利用します。 ...
Definition: Platform.h:837
errno_t nlib_fd_sync(nlib_fd fd)
メモリにあるファイルの内容をデバイス上のものと同期させます。
errno_t nlib_thread_setpriority(nlib_thread thread, int priority)
スレッドの実行優先度を設定します。数値の意味は実装依存です。
NLIB_CHECK_RESULT int nlib_utf16_to_utf32char(nlib_utf32_t *utf32, nlib_utf16_t upper, nlib_utf16_t lower) noexcept
1つのコードポイントをUTF-16からUTF-32に変換します。
errno_t nlib_fd_native_handle(void **native_handle, nlib_fd fd)
ネイティブのファイルハンドル(に相当するもの)を取得する。
NLIB_CHECK_RESULT errno_t nlib_fd_read(size_t *result, nlib_fd fd, void *buf, size_t count)
ファイルディスクリプタから、(最大)count バイトをbuf に読むこみます。
errno_t nlib_rwlock_rdunlock(nlib_rwlock *rwlock)
読み込みロックを解放します。
errno_t nlib_swapendian_32(uint32_t *p, size_t count)
エンディアンを変換します。
int nlib_thread_id
スレッド毎にユニークな整数値
Definition: Platform.h:859
errno_t nlib_semaphore_post(nlib_semaphore *sem, int *previous_count)
セマフォカウントを1つ増加させる。
NLIB_CHECK_RESULT errno_t nlib_fd_pwrite(size_t *result, nlib_fd fd, const void *buf, size_t count, nlib_offset offset)
指定したオフセットでファイルディスクリプタに書きこみます。ファイルディスクリプタのオフセットは変更さ...
#define NLIB_ATOMIC_RELAXED
gccの__ATOMIC_RELAXEDやC++11のstd::memory_order_relaxedに準じます。
size_t nlib_strlen(const char *s)
内部でstrlen()を呼び出します。独自の実装が動作する場合もあります。
errno_t nlib_utf16cpy(nlib_utf16_t *s1, size_t s1max, const nlib_utf16_t *s2) noexcept
nlib_strcpy()のUTF-16版です。
Definition: Platform.h:2201
pthread_mutex_t nlib_mutex
ミューテックス変数の型です。
errno_t nlib_condrwlock_broadcast(nlib_condrwlock *cond)
リードライトロック用条件変数cond を待っているスレッド全ての実行を再開させます。
int nlib_log_print(int prio, const char *tag, const char *fmt,...)
ログメッセージを出力します。
errno_t nlib_thread_join(nlib_thread thread)
スレッドの終了を待ちます。
const void * nlib_memchr_range_not(const void *s, const char *range, size_t n)
メモリ領域[s, s + n)の先頭からn バイトを検索して、最初のrange に含まない文字へのポインタを返します。 ...
詳細レベルのメッセージを出力するときに指定します。
Definition: Platform.h:962
errno_t nlib_snprintf(size_t *count, char *buf, size_t size, const char *fmt,...)
より安全な形式のsnprintfです。
全ての優先度を指定します。nlib_log_attr_setint()で利用することができます。
Definition: Platform.h:971
#define NLIB_ALWAYS_INLINE
コンパイラに関数をインライン展開するように強く示します。
Definition: Platform_unix.h:59
int64_t nlib_atomic_xor_fetch64(int64_t *ptr, int64_t val, int memorder)
アトミックな値の排他的論理和の計算を行います。動作はgccの__atomic_xor_fetch()に準じます。 ...
int nlib_popcnt64(uint64_t x)
1となっているビットの数を返します。
NLIB_CHECK_RESULT errno_t nlib_thread_create(nlib_thread *thread, const nlib_thread_attr *attr, nlib_thread_func func, void *arg)
新しいスレッド作成して実行します。
errno_t nlib_wide_to_utf8(size_t *utf8count, char *utf8, size_t buflen, const wchar_t *wcstr)
UTF-16/UTF-32文字列からUTF-8文字列に変換します。
void(* nlib_tls_destructor)(void *tls_value)
スレッド終了時に呼び出されるTLSのデストラクタ関数の型です。
Definition: Platform.h:575
#define NLIB_NONNULL_3
3番目の引数にNULLを指定することができないことを示します。
Definition: Platform_unix.h:69
情報レベルのメッセージを出力するときに指定します。
Definition: Platform.h:964
NLIB_CHECK_RESULT errno_t nlib_fd_open(nlib_fd *fd, const char *native_path, unsigned int flags, const void *extra)
ファイルをオープンします。
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:51
int64_t nlib_atomic_add_fetch64(int64_t *ptr, int64_t val, int memorder)
アトミックな値の加算を行います。動作はgccの__atomic_add_fetch()に準じます。
NLIB_CHECK_RESULT size_t nlib_utf16len(const nlib_utf16_t *str) noexcept
ヌル文字を含まないnlib_utf16_tの数を数えます。
Definition: Platform.h:2194
errno_t nlib_swapendian_16(uint16_t *p, size_t count)
エンディアンを変換します。
errno_t nlib_thread_detach(nlib_thread thread)
実行中のスレッドをデタッチ状態にします。
NLIB_CHECK_RESULT errno_t nlib_fd_truncate(nlib_fd fd, nlib_offset length)
指定した長さにファイルを延長、もしくは切り詰める。
const void * nlib_memchr_gt(const void *s, int c, size_t n)
メモリ領域[s, s + n)の先頭からn バイトを検索して、バイトc より大きいの文字があるデータへのポインタを...
int32_t nlib_atomic_and_fetch32(int32_t *ptr, int32_t val, int memorder)
アトミックな値の論理積の計算を行います。動作はgccの__atomic_and_fetch()に準じます。 ...
int nlib_atomic_compare_exchange32(int32_t *ptr, int32_t *expected, int32_t desired, int weak, int success_memorder, int failure_memorder)
アトミックな値の比較と入れ替えを行います。動作はgccの__atomic_compare_exchange_n()に準じます。 ...
errno_t nlib_vsnwprintf(size_t *count, wchar_t *buf, size_t size, const wchar_t *fmt, va_list args)
より安全な形式のvswprintfで、各種vswprintfの動作の違いも吸収します。
errno_t nlib_utf16_to_utf8(size_t *utf8count, char *utf8, size_t buflen, const nlib_utf16_t *utf16) noexcept
UTF-16文字列からUTF-8文字列に変換します。
errno_t nlib_thread_getcpu(int *result)
呼び出したスレッドが実行されているCPUを取得します。
errno_t nlib_thread_getpriority(nlib_thread thread, int *priority)
スレッドの現在の実行優先度を取得します。数値の意味は実装依存です。
NLIB_CHECK_RESULT errno_t nlib_fd_writev(size_t *result, nlib_fd fd, const nlib_fd_iovec *iov, int iovcnt)
複数の非連続なバッファからfdに関連付けられたファイルへの書き込みを行います。
struct nlib_onceflag_ nlib_onceflag
nlib_onceで利用される構造体
Definition: Platform.h:767
errno_t nlib_thread_attr_setstack(nlib_thread_attr *attr, void *stack_addr, size_t stack_size)
スレッドの属性オブジェクトのスタック設定を設定する。 Platform implementation Win32 nlib独自の実装(設...
NLIB_CHECK_RESULT errno_t nlib_rmdir(const char *native_path)
ディレクトリを削除する
NLIB_CHECK_RESULT size_t nlib_utf32len(const nlib_utf32_t *str) noexcept
ヌル文字を含まないnlib_utf32_tの数を数えます。
Definition: Platform.h:2219
errno_t nlib_utf32cpy(nlib_utf32_t *s1, size_t s1max, const nlib_utf32_t *s2) noexcept
nlib_strcpy()のUTF-32版です。
Definition: Platform.h:2226
errno_t nlib_barrier_init(nlib_barrier *barrier, unsigned int count)
バリアオブジェクトを初期化します。
void nlib_atomic_store64(int64_t *ptr, int64_t val, int memorder)
アトミックに値をストアします。動作はgccの__atomic_store_n()に準じます。
NLIB_CHECK_RESULT errno_t nlib_strcplen_ex(size_t *count, size_t *len, const char *str)
文字列中のコードポイントの数と文字列長を取得します。
errno_t nlib_utf8_to_utf32(size_t *utf32count, nlib_utf32_t *utf32, size_t buflen, const char *utf8) noexcept
UTF-8文字列からUTF-32文字列に変換します。
int64_t nlib_offset
ファイルへのオフセットです。64bit整数です。
Definition: Platform.h:1050
unsigned int nlib_get_native_last_error(void)
最後に発生したネイティブのエラーコードを返します。
int nlib_ctz64(uint64_t x)
LSB(least significant bit)から見て連続する0ビットの数を返します。
void nlib_free(void *ptr)
C標準関数のfree()を呼び出すweak関数です。nlibはこの関数を経由してfree()を呼び出します。 ...
errno_t nlib_swapendian_64(uint64_t *p, size_t count)
エンディアンを変換します。
errno_t nlib_barrier_wait(nlib_barrier *barrier)
スレッドの待ち合わせを行います。
int32_t cur_msg
ロックフリーなキュー以外の場合、現在メッセージキューに存在するメッセージ数を取得できます。 ...
Definition: Platform.h:800
int32_t nlib_long_compatible_t
longと互換性のある整数型がtypedefされています。
Definition: Platform.h:424
int64_t nlib_atomic_or_fetch64(int64_t *ptr, int64_t val, int memorder)
アトミックな値の論理和の計算を行います。動作はgccの__atomic_or_fetch()に準じます。 ...
errno_t nlib_rwlock_init(nlib_rwlock *rwlock)
リードライトロックを初期化します。
#define NLIB_NONNULL_2
2番目の引数にNULLを指定することができないことを示します。
Definition: Platform_unix.h:68
size_t nlib_wcsnlen(const wchar_t *s, size_t maxsize)
N1078のwcsnlen_sに相当する実装です。
errno_t nlib_thread_attr_setptr(nlib_thread_attr *attr, int key, void *value)
スレッドの属性オブジェクトのキーに対応するポインタを設定する。現在のところEINVALのみを返します。 ...
errno_t nlib_munlock(void *addr, size_t len)
指定したメモリ領域がスワップアウトできるようにします。
errno_t nlib_wcscat(wchar_t *s1, size_t s1max, const wchar_t *s2)
N1078のwcscat_sに相当する実装です。
int32_t nlib_atomic_fetch_or32(int32_t *ptr, int32_t val, int memorder)
アトミックな値の論理和の計算を行います。動作はgccの__atomic_fetch_or()に準じます。 ...
void(* nlib_mq_msg_destructor)(nlib_mq_msg)
メッセージキューから取り出したメッセージのデストラクタ関数です。
Definition: Platform.h:795
errno_t nlib_tls_getvalue(nlib_tls tls, void **value)
TLSスロットから値を取り出します。
NLIB_CHECK_RESULT errno_t nlib_fd_seek(nlib_offset *result, nlib_fd fd, nlib_offset offset, int whence)
ファイルのオフセットを変更する。
errno_t nlib_strcpy(char *s1, size_t s1max, const char *s2)
N1078のstrcpy_sに相当する実装です。
errno_t nlib_rwlock_trywrlock_for(nlib_rwlock *rwlock, nlib_duration duration)
書き込みロックを取得しクリティカルセクションに入ることを試みます。タイムアウトします。 ...
int64_t nlib_atomic_fetch_xor64(int64_t *ptr, int64_t val, int memorder)
アトミックな値の排他的論理和の計算を行います。動作はgccの__atomic_fetch_xor()に準じます。 ...
errno_t nlib_mutex_recursive_init(nlib_mutex *mutex)
再帰ミューテックスを初期化します。
const void * nlib_memchr_mb(const void *s, size_t n)
メモリ領域[s, s + n)の先頭からn バイトを検索して、0x80以上のバイトが格納されている場所へのポインタを...
void nlib_thread_exit(void) NLIB_NORETURN
呼び出しスレッドを終了します。
int32_t nlib_atomic_fetch_add32(int32_t *ptr, int32_t val, int memorder)
アトミックな値の加算を行います。動作はgccの__atomic_fetch_add()に準じます。
errno_t nlib_mutex_init(nlib_mutex *mutex)
ミューテックスを初期化します。
errno_t nlib_rwlock_tryrdlock_for(nlib_rwlock *rwlock, nlib_duration duration)
読み込みロックを取得しクリティカルセクションに入ることを試みます。タイムアウトします。 ...
void nlib_atomic_store32(int32_t *ptr, int32_t val, int memorder)
アトミックに値をストアします。動作はgccの__atomic_store_n()に準じます。
int32_t flag
メッセージキューを作成する際の設定です。 値 説明 NLIB_MQ_BLOCK 空のキューを読み込んだり、一杯のキュ...
Definition: Platform.h:798
errno_t nlib_tls_free(nlib_tls tls)
TLSスロットに対応するIDを解放します。
int nlib_clz64(uint64_t x)
MSB(most significant bit)から見て連続する0ビットの数を返します。
errno_t nlib_rwlock_wrlock(nlib_rwlock *rwlock)
書き込みロックを取得しクリティカルセクションに入ります。取得できるまでブロックします。 ...
int32_t nlib_atomic_exchange32(int32_t *ptr, int32_t val, int memorder)
アトミックに値を入れ替えます。動作はgccの__atomic_exchange_n()に準じます。
NLIB_CHECK_RESULT int nlib_utf32char_to_utf16(nlib_utf16_t *upper, nlib_utf16_t *lower, nlib_utf32_t utf32) noexcept
1つのUTF-32文字をUTF-16に変換します。
NLIB_CHECK_RESULT errno_t nlib_cond_wait_until(nlib_cond *cond, nlib_mutex *mutex, nlib_time abstime)
mutexをアンロックし、条件変数をabstimeまで待機します。実行が再開したらmutexを再ロックします。 ...
errno_t nlib_thread_setname(const char *string_literal)
スレッドに名前をつけます。
errno_t nlib_semaphore_destroy(nlib_semaphore *sem)
セマフォオブジェクトを破壊する。
pthread_t nlib_thread
スレッドを指し示す識別子
int64_t nlib_duration
100ns刻みで時間を表現する型です。64bit符号付き整数です。
Definition: Platform.h:535
errno_t nlib_memcpy(void *s1, size_t s1max, const void *s2, size_t n)
N1078のmemcpy_sに相当する実装です。
Definition: Platform.h:2095
errno_t nlib_thread_attr_getstack(const nlib_thread_attr *attr, void **stack_addr, size_t *stack_size)
スレッドの属性オブジェクトのスタック設定を取得する。
void * nlib_mq_msg
メッセージキューに格納されるメッセージの型です。
Definition: Platform.h:789
#define NLIB_NORETURN
関数がリターンしないことを示します。
Definition: Platform_unix.h:65
int64_t nlib_atomic_load64(const int64_t *ptr, int memorder)
アトミックに値をロードします。動作はgccの__atomic_load_n()に準じます。
NLIB_CHECK_RESULT errno_t nlib_unlink(const char *native_path)
ファイルを削除する
NLIB_CHECK_RESULT errno_t nlib_fd_pwritev(size_t *result, nlib_fd fd, const nlib_fd_iovec *iov, int iovcnt, nlib_offset offset)
内部でpwrite()又はnlib_fd_pwrite()を使うこと以外は、nlib_fd_writev()と同様です。
NLIB_CHECK_RESULT errno_t nlib_utf32cplen(size_t *count, const nlib_utf32_t *str) noexcept
文字列中のコードポイントの数を取得します。
errno_t nlib_dprintf(nlib_fd fd, size_t *count, const char *fmt,...)
ファイルディスクリプタに出力するsnprintf()です。
errno_t nlib_mutex_recursive_timed_init(nlib_mutex *mutex)
再帰かつタイムアウト可能なミューテックスを初期化します。
#define NLIB_CHECK_RESULT
関数の呼び出し元が戻り値をチェックする必要があることを示します。
Definition: Platform_unix.h:64
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:24