16 #ifndef INCLUDE_NN_NLIB_PLATFORM_H_ 17 #define INCLUDE_NN_NLIB_PLATFORM_H_ 20 # error Sorry, ILP64 not supported 24 #define NLIB_VERSION_YEAR 2017 25 #define NLIB_VERSION_YEAR_SHORT 17 26 #define NLIB_VERSION_DATE 0516 27 #define NLIB_VERSION 20170516 30 # ifndef __STDC_FORMAT_MACROS 32 # pragma message(__FILE__ ": __STDC_FORMAT_MACROS not defined, compile may fail") 33 # elif !defined(NN_PLATFORM_CTR) && !defined(CAFE) 34 # warning __STDC_FORMAT_MACROS not defined, compile may fail 36 # define __STDC_FORMAT_MACROS 39 #ifndef __STDC_WANT_LIB_EXT1__ 40 # define __STDC_WANT_LIB_EXT1__ 1 44 # define NLIB_UNUSED(x) (void)(x) 48 # define __has_builtin(x) 0 52 # define __has_feature(x) 0 55 #ifndef __has_cpp_attribute 56 # define __has_cpp_attribute(x) 0 59 #ifndef __has_attribute 60 # define __has_attribute(x) 0 63 #ifndef __has_declspec_attribute 64 # define __has_declspec_attribute(x) 0 68 # define __has_include(x) 0 71 #if __has_feature(thread_sanitizer) 72 # define NLIB_NO_TSAN __attribute__((no_sanitize("thread"))) 77 #if __has_feature(address_sanitizer) 78 # define NLIB_NO_ASAN __attribute__((no_sanitize("address"))) 83 #if __has_feature(memory_sanitizer) 84 # define NLIB_NO_MSAN __attribute__((no_sanitize("memory"))) 93 #if !defined(NLIB_THREAD_AA_) && defined(__clang__) && \ 94 (!defined(SWIG)) && __has_attribute(capability) 95 #define NLIB_THREAD_AA_(x) __attribute__((x)) 97 #ifdef NLIB_THREAD_AA_ 98 #undef NLIB_THREAD_AA_ 100 #define NLIB_THREAD_AA_(x) 103 #define NLIB_CAPABILITY(x) NLIB_THREAD_AA_(capability(x)) 104 #define NLIB_SCOPED_CAPABILITY NLIB_THREAD_AA_(scoped_lockable) 105 #define NLIB_GUARDED_BY(x) NLIB_THREAD_AA_(guarded_by(x)) 106 #define NLIB_PT_GUARDED_BY(x) NLIB_THREAD_AA_(pt_guarded_by(x)) 107 #define NLIB_ACQUIRED_BEFORE(...) NLIB_THREAD_AA_(acquired_before(__VA_ARGS__)) 108 #define NLIB_ACQUIRED_AFTER(...) NLIB_THREAD_AA_(acquired_after(__VA_ARGS__)) 109 #define NLIB_REQUIRES(...) NLIB_THREAD_AA_(requires_capability(__VA_ARGS__)) 110 #define NLIB_REQUIRES_SHARED(...) NLIB_THREAD_AA_(requires_shared_capability(__VA_ARGS__)) 111 #define NLIB_ACQUIRE(...) NLIB_THREAD_AA_(acquire_capability(__VA_ARGS__)) 112 #define NLIB_ACQUIRE_SHARED(...) NLIB_THREAD_AA_(acquire_shared_capability(__VA_ARGS__)) 113 #define NLIB_RELEASE(...) NLIB_THREAD_AA_(release_capability(__VA_ARGS__)) 114 #define NLIB_RELEASE_SHARED(...) NLIB_THREAD_AA_(release_shared_capability(__VA_ARGS__)) 115 #define NLIB_TRY_ACQUIRE(...) NLIB_THREAD_AA_(try_acquire_capability(__VA_ARGS__)) 116 #define NLIB_TRY_ACQUIRE_SHARED(...) NLIB_THREAD_AA_(try_acquire_shared_capability(__VA_ARGS__)) 117 #define NLIB_EXCLUDES(...) NLIB_THREAD_AA_(locks_excluded(__VA_ARGS__)) 118 #define NLIB_ASSERT_CAPABILITY(x) NLIB_THREAD_AA_(assert_capability(x)) 119 #define NLIB_ASSERT_SHARED_CAPABILITY(x) NLIB_THREAD_AA_(assert_shared_capability(x)) 120 #define NLIB_RETURN_CAPABILITY(x) NLIB_THREAD_AA_(lock_returned(x)) 121 #define NLIB_NO_THREAD_SAFETY_ANALYSIS NLIB_THREAD_AA_(no_thread_safety_analysis) 129 #if defined(NLIB_RENAME_CAPI) 130 #include "nn/nlib/Platform_rename.h" 135 #define NLIB_CAPI(x) x 138 #define NLIB_STRINGIFY_(s) #s 139 #define NLIB_STRINGIFY(s) NLIB_STRINGIFY_(s) 141 #if defined(_MSC_VER) 142 # include "nn/nlib/Platform_win32.h" 143 #elif defined(__linux__) || \ 144 defined(__FreeBSD__) || \ 145 defined(__CYGWIN__) || \ 146 (defined(__APPLE__) && defined(__MACH__)) 150 # include "nn/nlib/Platform_unix.h" 151 #elif defined(NN_PLATFORM_CTR) 152 # include "nn/nlib/Platform_ctr.h" 154 # include "nn/nlib/Platform_cafe.h" 155 #elif defined(__NX__) 156 # include "nn/nlib/Platform_nx.h" 159 #if defined(_MSC_VER) && defined(n_EXPORTS) 160 #undef NLIB_VIS_PUBLIC 161 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT 164 #ifndef __analysis_assume 165 # define __analysis_assume(expr) 168 #ifndef _Printf_format_string_ 169 # define _Printf_format_string_ 172 #if defined(__ARM_NEON__) || defined(__aarch64__) 193 #if defined(NLIB_SSE41) || defined(NLIB_NEON) 196 # include <arm_neon.h> 199 # include <smmintrin.h> 202 # include <nmmintrin.h> 206 #if defined(__ARM_ACLE) 207 #include <arm_acle.h> 214 # define RSIZE_MAX 0x7FFFFFFFL 216 # define RSIZE_MAX 0x7FFFFFFFFFFFFFFFLL 221 # define NLIB_WARN(exp) ("WARNING: " exp) 231 #ifndef __PRIS_PREFIX 232 # define __PRIS_PREFIX "z" 236 # define PRIdS __PRIS_PREFIX "d" 240 # define PRIxS __PRIS_PREFIX "x" 244 # define PRIuS __PRIS_PREFIX "u" 248 # define PRIXS __PRIS_PREFIX "X" 252 # define PRIoS __PRIS_PREFIX "o" 256 # define NLIB_ASSUME(cond) switch (0) case 0: default: if (cond) ; else __builtin_unreachable() 259 #ifdef NLIB_NONNULL_ENABLED 260 # define NLIB_EINVAL_IFNULL(p) 262 # define NLIB_EINVAL_IFNULL(p) if (!p) return EINVAL 265 #ifndef NLIB_C_INLINE 266 # define NLIB_C_INLINE __inline 278 #include <inttypes.h> 281 #ifdef NLIB_CXX11_NEW_CHARACTER_TYPES 306 #elif __cplusplus >= 201103L 315 #if defined(__LP64__) && __LP64__ == 1 325 #ifndef NLIB_VIS_PUBLIC_ALT 326 #define NLIB_VIS_PUBLIC_ALT NLIB_VIS_PUBLIC 329 #ifndef NLIB_EXPIMP_TEMPLATE 330 # define NLIB_EXPIMP_TEMPLATE(x) NLIB_STATIC_ASSERT(sizeof(char) == 1) 360 #if defined(__INTELLISENSE__) 361 #define NLIB_ATOMIC_RELAXED (0) 362 #define NLIB_ATOMIC_ACQUIRE (1) 363 #define NLIB_ATOMIC_RELEASE (2) 364 #define NLIB_ATOMIC_ACQ_REL (3) 365 #define NLIB_ATOMIC_SEQ_CST (7) 371 int32_t val,
int memorder);
377 int success_memorder,
378 int failure_memorder);
406 int64_t desired,
int weak,
407 int success_memorder,
int failure_memorder);
434 int weak,
int success_memorder,
int failure_memorder);
442 #ifndef NLIB_TIMESPEC_HAS_NATIVE 461 #define NLIB_TO_TIMESPEC(tm, t) \ 462 (tm)->tv_sec = (time_t)((t) / (1000 * 10000)); \ 463 (tm)->tv_nsec = ((long)((t) % (1000 * 10000)) * 100) // NOLINT 465 #define NLIB_FROM_TIMESPEC(tm, t) \ 466 t = (int64_t)((tm)->tv_sec) * (1000 * 10000) + ((tm)->tv_nsec / 100) 472 NLIB_TO_TIMESPEC(tm, t);
480 NLIB_TO_TIMESPEC(tm, d);
486 NLIB_FROM_TIMESPEC(tm, d);
490 #if !defined(NLIB_TIMER_HAS_NATIVE) 492 #elif defined(_MSC_VER) 494 #elif defined(__linux__) 500 struct nlib_timerspec_ {
501 nlib_duration due_time;
502 nlib_duration interval;
504 typedef struct nlib_timerspec_ nlib_timerspec;
506 struct nlib_timerspec {
512 void* param, uint32_t flags);
514 nlib_timerspec* old_value);
519 #define NLIB_TIMER_SHORTTERM_TASK 0x00000001 520 #define NLIB_TIMER_LONGTERM_TASK 0x00000002 540 #define NLIB_PHYSICAL_ALLOC_PROT_NONE 0 541 #define NLIB_PHYSICAL_ALLOC_PROT_READ 1 542 #define NLIB_PHYSICAL_ALLOC_PROT_WRITE 2 543 #define NLIB_PHYSICAL_ALLOC_PROT_EXEC 4 549 #define NLIB_TLS_INVALID (nlib_tls)(-1) 551 #ifdef NLIB_PTHREAD_nlib_tls_alloc 570 #ifdef NLIB_PTHREAD_nlib_tls_alloc 572 return pthread_key_create(tls, destr);
575 #ifdef NLIB_PTHREAD_nlib_tls_free 577 return pthread_key_delete(tls);
582 #ifdef NLIB_PTHREAD_nlib_tls_setvalue 588 #ifdef NLIB_PTHREAD_nlib_tls_setvalue 590 return pthread_setspecific(tls, value);
594 #ifdef NLIB_PTHREAD_nlib_tls_getvalue 601 #ifdef NLIB_PTHREAD_nlib_tls_getvalue 603 *value = pthread_getspecific(tls);
612 #ifdef NLIB_PTHREAD_nlib_mutex_init 620 #ifdef NLIB_PTHREAD_nlib_mutex_init 622 return pthread_mutex_init(mutex, NULL);
628 NLIB_NONNULL NLIB_EXCLUDES(*mutex);
631 NLIB_NONNULL NLIB_EXCLUDES(*mutex);
633 #ifdef NLIB_PTHREAD_nlib_mutex_lock 646 #ifdef NLIB_PTHREAD_nlib_mutex_lock 648 return pthread_mutex_lock(mutex);
653 #ifdef NLIB_PTHREAD_nlib_mutex_trylock 660 #ifdef NLIB_PTHREAD_nlib_mutex_trylock 662 return pthread_mutex_trylock(mutex);
668 nlib_duration delta) NLIB_NONNULL NLIB_TRY_ACQUIRE(0, *mutex);
669 #ifdef NLIB_PTHREAD_nlib_mutex_unlock 675 #ifdef NLIB_PTHREAD_nlib_mutex_unlock 677 return pthread_mutex_unlock(mutex);
681 #ifdef NLIB_PTHREAD_nlib_mutex_destroy 688 #ifdef NLIB_PTHREAD_nlib_mutex_destroy 690 return pthread_mutex_destroy(mutex);
695 const struct timespec* tm)
696 NLIB_TRY_ACQUIRE(0, *mutex) {
698 NLIB_FROM_TIMESPEC(tm, delta);
721 nlib_duration duration;
722 NLIB_FROM_TIMESPEC(tm, duration);
730 #ifdef NLIB_PTHREAD_nlib_cond_init 738 #ifdef NLIB_PTHREAD_nlib_cond_init 740 return pthread_cond_init(cond, NULL);
744 #ifdef NLIB_PTHREAD_nlib_cond_signal 750 #ifdef NLIB_PTHREAD_nlib_cond_signal 752 return pthread_cond_signal(cond);
756 #ifdef NLIB_PTHREAD_nlib_cond_broadcast 762 #ifdef NLIB_PTHREAD_nlib_cond_broadcast 764 return pthread_cond_broadcast(cond);
768 #ifdef NLIB_PTHREAD_nlib_cond_wait 795 NLIB_NONNULL NLIB_REQUIRES(*mutex);
796 #ifdef NLIB_PTHREAD_nlib_cond_wait 799 return pthread_cond_wait(cond, mutex);
807 nlib_duration duration) NLIB_NONNULL NLIB_REQUIRES(*mutex);
812 nlib_time abstime) NLIB_NONNULL NLIB_REQUIRES(*mutex);
814 #ifdef NLIB_PTHREAD_nlib_cond_destroy 820 #ifdef NLIB_PTHREAD_nlib_cond_destroy 822 return pthread_cond_destroy(cond);
828 NLIB_REQUIRES(*mutex) {
830 NLIB_FROM_TIMESPEC(tm, d);
836 const struct timespec* tm) NLIB_REQUIRES(*mutex) {
838 NLIB_FROM_TIMESPEC(tm, d);
845 #ifndef NLIB_RWLOCK_HAS_NATIVE 846 struct nlib_rwlock_ {
851 NLIB_CAPABILITY(
"mutex")
854 #define NLIB_RWLOCK_INITIALIZER { \ 856 { NLIB_RECURSIVE_TIMED_MUTEX_INITIALIZER, NLIB_RECURSIVE_TIMED_MUTEX_INITIALIZER }, \ 857 NLIB_COND_INITIALIZER } 860 #ifdef NLIB_PTHREAD_nlib_rwlock_init 867 #ifdef NLIB_PTHREAD_nlib_rwlock_init 869 return pthread_rwlock_init(rwlock, NULL);
873 #ifdef NLIB_PTHREAD_nlib_rwlock_destroy 879 #ifdef NLIB_PTHREAD_nlib_rwlock_destroy 881 return pthread_rwlock_destroy(rwlock);
885 #ifdef NLIB_PTHREAD_nlib_rwlock_rdlock 891 #ifdef NLIB_PTHREAD_nlib_rwlock_rdlock 893 NLIB_NO_THREAD_SAFETY_ANALYSIS {
894 return pthread_rwlock_rdlock(rwlock);
898 #ifdef NLIB_PTHREAD_nlib_rwlock_tryrdlock 904 NLIB_NONNULL NLIB_TRY_ACQUIRE_SHARED(0, *rwlock);
905 #ifdef NLIB_PTHREAD_nlib_rwlock_tryrdlock 907 NLIB_NO_THREAD_SAFETY_ANALYSIS {
908 return pthread_rwlock_tryrdlock(rwlock);
914 NLIB_NONNULL NLIB_TRY_ACQUIRE_SHARED(0, *rwlock);
917 NLIB_NONNULL NLIB_TRY_ACQUIRE_SHARED(0, *rwlock);
919 #ifdef NLIB_PTHREAD_nlib_rwlock_rdunlock 925 NLIB_NONNULL NLIB_RELEASE_SHARED(*rwlock);
926 #ifdef NLIB_PTHREAD_nlib_rwlock_rdunlock 928 NLIB_NO_THREAD_SAFETY_ANALYSIS {
929 return pthread_rwlock_unlock(rwlock);
933 #ifdef NLIB_PTHREAD_nlib_rwlock_wrlock 939 #ifdef NLIB_PTHREAD_nlib_rwlock_wrlock 941 NLIB_NO_THREAD_SAFETY_ANALYSIS {
942 return pthread_rwlock_wrlock(rwlock);
946 #ifdef NLIB_PTHREAD_nlib_rwlock_trywrlock 952 #ifdef NLIB_PTHREAD_nlib_rwlock_trywrlock 954 NLIB_NO_THREAD_SAFETY_ANALYSIS {
955 return pthread_rwlock_trywrlock(rwlock);
961 NLIB_NONNULL NLIB_TRY_ACQUIRE(0, *rwlock);
964 NLIB_NONNULL NLIB_TRY_ACQUIRE(0, *rwlock);
966 #ifdef NLIB_PTHREAD_nlib_rwlock_wrunlock 972 #ifdef NLIB_PTHREAD_nlib_rwlock_wrunlock 974 NLIB_NO_THREAD_SAFETY_ANALYSIS {
975 return pthread_rwlock_unlock(rwlock);
981 NLIB_TRY_ACQUIRE_SHARED(0, *rwlock) {
983 NLIB_FROM_TIMESPEC(tm, d);
989 NLIB_TRY_ACQUIRE_SHARED(0, *rwlock) {
991 NLIB_FROM_TIMESPEC(tm, d);
997 NLIB_TRY_ACQUIRE(0, *rwlock) {
999 NLIB_FROM_TIMESPEC(tm, d);
1003 static NLIB_C_INLINE
1005 NLIB_TRY_ACQUIRE(0, *rwlock) {
1007 NLIB_FROM_TIMESPEC(tm, d);
1012 #if defined(_MSC_VER) && defined(NLIB_RWLOCK_HAS_NATIVE) 1013 typedef struct nlib_condrwlock_ {
1014 CONDITION_VARIABLE cond;
1016 #define NLIB_CONDRWLOCK_INITIALIZER { CONDITION_VARIABLE_INIT } 1018 typedef struct nlib_condrwlock_ {
1022 #define NLIB_CONDRWLOCK_INITIALIZER { NLIB_COND_INITIALIZER, NLIB_MUTEX_INITIALIZER } 1030 nlib_rwlock* __restrict rwlock,
1033 nlib_rwlock* __restrict rwlock,
1034 nlib_duration duration,
1037 nlib_rwlock* __restrict rwlock,
1042 nlib_rwlock* rwlock,
1043 const struct timespec* tm,
1046 NLIB_FROM_TIMESPEC(tm, d);
1051 nlib_rwlock* rwlock,
1052 const struct timespec* tm,
1055 NLIB_FROM_TIMESPEC(tm, d);
1062 #ifndef NLIB_BARRIER_HAS_NATIVE 1063 struct nlib_barrier_ {
1071 #ifdef NLIB_PTHREAD_nlib_barrier_init 1077 #ifdef NLIB_PTHREAD_nlib_barrier_init 1079 return pthread_barrier_init(barrier, NULL, count);
1083 #ifdef NLIB_PTHREAD_nlib_barrier_destroy 1089 #ifdef NLIB_PTHREAD_nlib_barrier_destroy 1091 return pthread_barrier_destroy(barrier);
1100 #ifndef NLIB_ONCE_HAS_NATIVE 1101 struct nlib_onceflag_ {
1105 #define NLIB_ONCE_INIT { 0 } 1114 #elif defined(_MSC_VER) 1116 #define NLIB_ONCE_INIT INIT_ONCE_STATIC_INIT 1119 #elif defined(__APPLE__) || defined(__FreeBSD__) 1121 #define NLIB_ONCE_INIT 0 1126 #define NLIB_ONCE_INIT PTHREAD_ONCE_INIT 1130 return pthread_once(flag, func);
1140 typedef struct nlib_mq_ {
1146 #define NLIB_MQ_BLOCK 0 1147 #define NLIB_MQ_NONBLOCK 1 1148 #define NLIB_MQ_LOCKFREE 2 1159 typedef struct nlib_mq_attr_ {
1189 #if defined(__x86_64__) || defined(__i386__) || defined(_M_IX86) || defined(_M_AMD64) 1191 #elif defined(__ARM_ACLE) 1198 #define NLIB_THREAD_INVALID (nlib_thread)(0) // NOLINT 1200 #ifndef NLIB_SPINLOCK_HAS_NATIVE 1204 #ifndef NLIB_THREAD_ATTR_HAS_NATIVE 1205 struct nlib_thread_attr_ {
1216 struct nlib_thread_attr_ {
1217 pthread_attr_t attr;
1234 #ifdef NLIB_PTHREAD_nlib_thread_join 1240 #ifdef NLIB_PTHREAD_nlib_thread_join 1242 return pthread_join(thread, NULL);
1246 #ifdef NLIB_PTHREAD_nlib_thread_detach 1252 #ifdef NLIB_PTHREAD_nlib_thread_detach 1254 return pthread_detach(thread);
1258 #ifdef NLIB_PTHREAD_nlib_thread_self 1264 #ifdef NLIB_PTHREAD_nlib_thread_self 1266 *thread = pthread_self();
1274 #ifdef NLIB_PTHREAD_nlib_thread_equal 1280 #ifdef NLIB_PTHREAD_nlib_thread_equal 1282 return pthread_equal(th1, th2);
1290 #ifdef NLIB_PTHREAD_nlib_thread_getname 1296 #ifdef NLIB_PTHREAD_nlib_thread_getname 1298 return pthread_getname_np(thread, name, len);
1323 void** __restrict stack_addr,
size_t* __restrict stack_size)
1327 #define NLIB_THREAD_ATTR_KEY_DETACHSTATE (1) 1328 #define NLIB_THREAD_ATTR_KEY_STACKSIZE (2) 1329 #define NLIB_THREAD_ATTR_KEY_PRIORITY (4) 1330 #define NLIB_THREAD_ATTR_KEY_AFFINITY (5) 1331 #define NLIB_THREAD_ATTR_KEY_EXPLICIT_SCHED (6) 1339 #ifndef NN_PLATFORM_CTR 1347 #elif defined(pthread_cleanup_push) 1348 # define nlib_thread_cleanup_push(fn, arg) pthread_cleanup_push(fn, arg) 1349 # define nlib_thread_cleanup_pop(exec) pthread_cleanup_pop(exec) 1350 #elif !defined(NN_PLATFORM_CTR) 1351 struct nlib_thread_cleanup_handler_ {
1352 void (*func)(
void*);
1354 struct nlib_thread_cleanup_handler_* next;
1356 #define nlib_thread_cleanup_push(fn, arg) switch (0) case 0: default: { \ 1357 struct nlib_thread_cleanup_handler_ _thread_cleanup_handler = { fn, arg, NULL }; \ 1358 nlib_thread_cleanup_push_(&_thread_cleanup_handler) 1359 #define nlib_thread_cleanup_pop(exec) nlib_thread_cleanup_pop_(exec); } 1361 NLIB_VIS_PUBLIC void nlib_thread_cleanup_push_(
struct nlib_thread_cleanup_handler_* handler);
1383 void*
const* __restrict buf,
size_t count)
NLIB_NONNULL;
1389 kNlibLogUnknown = 0,
1401 NLIB_LOG_UNKNOWN = kNlibLogUnknown,
1402 NLIB_LOG_DEAFULT = kNlibLogDefault,
1409 NLIB_LOG_SILENT = kNlibLogSilent,
1415 typedef enum nlib_log_key {
1416 kNlibLogAttrUnknown = 0,
1419 kNlibLogAttrMsvcTrace,
1423 NLIB_LOG_ATTR_UNKNOWN = kNlibLogAttrUnknown,
1424 NLIB_LOG_ATTR_STDOUT = kNlibLogAttrStdout,
1425 NLIB_LOG_ATTR_STDERR = kNlibLogAttrStderr,
1426 NLIB_LOG_ATTR_MSVC_TRACE = kNlibLogAttrMsvcTrace,
1427 NLIB_LOG_ATTR_SYSLOG = kNlibLogAttrSyslog,
1428 NLIB_LOG_ATTR_NLIB_FD = kNlibLogAttrNlibFd,
1429 NLIB_LOG_ATTR_MAX = kNlibLogAttrMax
1432 #ifndef NLIB_ATTRIBUTE_PRINTF 1433 # define NLIB_ATTRIBUTE_PRINTF(x, y) __attribute__((format(printf, x, y))) 1437 const char* __restrict fmt, ...)
1446 #ifndef NLIB_FD_O_RDONLY 1450 # define NLIB_FD_O_RDONLY O_RDONLY 1453 #ifndef NLIB_FD_O_WRONLY 1457 # define NLIB_FD_O_WRONLY O_WRONLY 1460 #ifndef NLIB_FD_O_RDWR 1464 # define NLIB_FD_O_RDWR O_RDWR 1467 #ifndef NLIB_FD_O_APPEND 1471 # define NLIB_FD_O_APPEND O_APPEND 1474 #ifndef NLIB_FD_O_CREAT 1478 # define NLIB_FD_O_CREAT O_CREAT 1481 #ifndef NLIB_FD_O_TRUNC 1485 # define NLIB_FD_O_TRUNC O_TRUNC 1488 #ifndef NLIB_FD_O_EXCL 1492 # define NLIB_FD_O_EXCL O_EXCL 1495 #ifndef NLIB_SEEK_SET 1499 # define NLIB_SEEK_SET SEEK_SET 1502 #ifndef NLIB_SEEK_CUR 1506 # define NLIB_SEEK_CUR SEEK_CUR 1514 #define NLIB_FD_INVALID (-1) 1521 errno_t nlib_fd_open(nlib_fd* fd,
const char* native_path,
unsigned int flags, ...) NLIB_NONNULL_1;
1530 errno_t nlib_fd_read(
size_t* __restrict result, nlib_fd fd,
void* __restrict buf,
size_t count)
1555 #if !defined(NLIB_IOVEC_HAS_NATIVE) 1556 struct nlib_fd_iovec_ {
1560 typedef struct nlib_fd_iovec_ nlib_fd_iovec;
1562 typedef struct iovec nlib_fd_iovec;
1565 errno_t nlib_fd_readv(
size_t* __restrict result, nlib_fd fd,
const nlib_fd_iovec* __restrict iov,
1580 unsigned int flags);
1584 errno_t nlib_rename(
const char* __restrict old_path,
const char* __restrict new_path);
1589 typedef struct nlib_dir_ nlib_dir;
1590 typedef struct nlib_dirent_ {
1611 uint64_t* __restrict free_bytes_available,
1612 uint64_t* __restrict total_bytes,
1613 uint64_t* __restrict total_free_bytes);
1619 struct nlib_fileid_ {
1620 #if defined(_MSC_VER) 1622 #elif defined(NLIB_UNIX) 1629 typedef struct nlib_fileid_ nlib_fileid;
1637 #if defined(_MSC_VER) || defined(NLIB_UNIX) 1638 #include "nn/nlib/Platform_socket.h" 1649 # warning Do not define EDQUOT. nlib may not work correctly. 1653 #ifndef NLIB_SKIP_ERRNO_DEFINE 1655 #if !defined(__CYGWIN__) && !defined(__linux__) && !defined(__FreeBSD__) && !defined(__NX__) 1656 #include "nn/nlib/Platform_errno.h" 1663 #ifndef NLIB_SPINLOCK_HAS_NATIVE 1664 #define NLIB_SPINLOCK_INITIALIZER (0) 1666 #ifndef NN_PLATFORM_CTR 1679 #if defined(__arm__) 1680 #if __has_builtin(__builtin_arm_ldrex) 1681 int R5 = __builtin_arm_ldrex(lock);
1683 int R5 = __ldrex(lock);
1686 #if __has_builtin(__builtin_arm_strex) 1687 if (__builtin_arm_strex(1, lock) == 0) {
1689 if (__strex(1, lock) == 0) {
1691 #if !defined(NN_PLATFORM_CTR) 1699 nlib_spinlock_lock_(lock);
1701 int32_t expected = 0;
1704 nlib_spinlock_lock_(lock);
1709 #if defined(__arm__) 1710 #if __has_builtin(__builtin_arm_ldrex) 1711 int R5 = __builtin_arm_ldrex(lock);
1713 int R5 = __ldrex(lock);
1716 #if __has_builtin(__builtin_arm_strex) 1717 if (__builtin_arm_strex(1, lock) == 0) {
1719 if (__strex(1, lock) == 0) {
1721 #if !defined(NN_PLATFORM_CTR) 1731 int32_t expected = 0;
1740 #if defined(NN_PLATFORM_CTR) 1743 int32_t expected = 1;
1747 nlib_spinlock_unlock_(lock);
1749 #if defined(__arm__) 1761 _Printf_format_string_
const char* __restrict fmt, va_list args)
1765 _Printf_format_string_
const char* __restrict fmt, ...)
1769 _Printf_format_string_
const char* __restrict fmt, va_list args)
1773 _Printf_format_string_
const char* __restrict fmt, ...)
1777 int nlib_printf(_Printf_format_string_
const char* fmt, ...)
1782 _Printf_format_string_
const wchar_t* __restrict fmt, va_list args)
1786 _Printf_format_string_
const wchar_t* __restrict fmt, ...)
1790 _Printf_format_string_
const wchar_t* __restrict fmt, va_list args)
1794 _Printf_format_string_
const wchar_t* __restrict fmt, ...)
NLIB_NONNULL_3;
1799 errno_t nlib_vsnprintf_fallback(
size_t* __restrict count,
char* __restrict buf,
size_t size,
1800 _Printf_format_string_
const char* __restrict fmt, va_list args)
1803 errno_t nlib_snprintf_fallback(
size_t* __restrict count,
char* __restrict buf,
1804 size_t size, _Printf_format_string_
const char* __restrict fmt, ...)
1807 errno_t nlib_vsnwprintf_fallback(
size_t* __restrict count,
wchar_t* __restrict buf,
size_t size,
1808 _Printf_format_string_
const wchar_t* __restrict fmt, va_list args)
1811 errno_t nlib_snwprintf_fallback(
size_t* __restrict count,
wchar_t* __restrict buf,
size_t size,
1812 _Printf_format_string_
const wchar_t* __restrict fmt, ...)
1820 static errno_t nlib_memcpy(
void* __restrict s1,
size_t s1max,
const void* __restrict s2,
size_t n)
1825 void*
nlib_memccpy(
void* __restrict dest,
size_t dest_size,
const void* __restrict src,
1828 #ifdef NLIB_LIBC_nlib_memcmp 1834 #ifdef NLIB_LIBC_nlib_memcmp 1835 static NLIB_C_INLINE
int nlib_memcmp(
const void* buf1,
const void* buf2,
size_t n) {
1836 return memcmp(buf1, buf2, n);
1840 NLIB_VIS_PUBLIC_ALT
const void*
nlib_memchr(
const void* s,
int c,
size_t n)
1842 NLIB_VIS_PUBLIC_ALT
const void*
nlib_memrchr(
const void* s,
int c,
size_t n)
1844 NLIB_VIS_PUBLIC_ALT
const void*
nlib_memchr_not(
const void* s,
int c,
size_t n)
1849 NLIB_VIS_PUBLIC_ALT
const void*
nlib_memchr_lt(
const void* s,
int c,
size_t n)
1851 NLIB_VIS_PUBLIC_ALT
const void*
nlib_memchr_gt(
const void* s,
int c,
size_t n)
1854 NLIB_VIS_PUBLIC_ALT
const void*
nlib_memchr_mb(
const void* s,
size_t n)
1857 const char* __restrict
set,
size_t n)
1860 const char* __restrict
set,
size_t n)
1865 const char*
nlib_skipws(
size_t* __restrict cnt_lf,
const char** __restrict last_lf,
1868 #ifdef NLIB_LIBC_nlib_strlen 1874 #ifdef NLIB_LIBC_nlib_strlen 1875 static NLIB_C_INLINE
size_t nlib_strlen(
const char* s) {
return strlen(s); }
1878 #ifdef NLIB_LIBC_nlib_strnlen 1884 #ifdef NLIB_LIBC_nlib_strnlen 1885 static NLIB_C_INLINE
size_t nlib_strnlen(
const char* s,
size_t maxsize) {
1886 #if defined(_MSC_VER) || defined(__STDC_LIB_EXT1__) 1887 return strnlen_s(s, maxsize);
1889 return strnlen(s, maxsize);
1894 #ifdef NLIB_LIBC_nlib_strcpy 1900 #ifdef NLIB_LIBC_nlib_strcpy 1901 static NLIB_C_INLINE
1903 #if defined(_MSC_VER) || defined(__STDC_LIB_EXT1__) 1904 return strcpy_s(s1, s1max, s2);
1911 #ifdef NLIB_LIBC_nlib_strncpy 1916 errno_t nlib_strncpy(
char* __restrict s1,
size_t s1max,
const char* __restrict s2,
size_t n)
1918 #ifdef NLIB_LIBC_nlib_strncpy 1919 static NLIB_C_INLINE
1920 errno_t nlib_strncpy(
char* __restrict s1,
size_t s1max,
const char* __restrict s2,
size_t n) {
1921 #if defined(_MSC_VER) || defined(__STDC_LIB_EXT1__) 1922 return strncpy_s(s1, s1max, s2, n);
1929 #ifdef NLIB_LIBC_nlib_strchr 1935 #ifdef NLIB_LIBC_nlib_strchr 1936 static NLIB_C_INLINE
const char*
nlib_strchr(
const char* s,
int c) {
return strchr(s, c); }
1939 #ifdef NLIB_LIBC_nlib_strrchr 1945 #ifdef NLIB_LIBC_nlib_strrchr 1946 static NLIB_C_INLINE
const char*
nlib_strrchr(
const char* s,
int c) {
return strrchr(s, c); }
1951 static NLIB_C_INLINE
const char* nlib_strchr_mb(
const char* s) {
1955 return (
const char*)p;
1961 #ifdef NLIB_LIBC_nlib_wcslen 1967 #ifdef NLIB_LIBC_nlib_wcslen 1968 static NLIB_C_INLINE
size_t nlib_wcslen(
const wchar_t* s) {
return wcslen(s); }
1971 #ifdef NLIB_LIBC_nlib_wcsnlen 1977 #ifdef NLIB_LIBC_nlib_wcsnlen 1978 static NLIB_C_INLINE
size_t nlib_wcsnlen(
const wchar_t* s,
size_t maxsize) {
1979 #if defined(_MSC_VER) || defined(__STDC_LIB_EXT1__) 1980 return wcsnlen_s(s, maxsize);
1982 return wcsnlen(s, maxsize);
1987 #ifdef NLIB_LIBC_nlib_wcscpy 1992 errno_t nlib_wcscpy(
wchar_t* __restrict s1,
size_t s1max,
const wchar_t* __restrict s2)
1994 #ifdef NLIB_LIBC_nlib_wcscpy 1995 static NLIB_C_INLINE
1996 errno_t nlib_wcscpy(
wchar_t* __restrict s1,
size_t s1max,
const wchar_t* __restrict s2) {
1997 #if defined(_MSC_VER) || defined(__STDC_LIB_EXT1__) 1998 return wcscpy_s(s1, s1max, s2);
2005 #ifdef NLIB_LIBC_nlib_wcsncpy 2010 errno_t nlib_wcsncpy(
wchar_t* __restrict s1,
size_t s1max,
const wchar_t* __restrict s2,
size_t n)
2012 #ifdef NLIB_LIBC_nlib_wcsncpy 2013 static NLIB_C_INLINE
2014 errno_t nlib_wcsncpy(
wchar_t* __restrict s1,
size_t s1max,
const wchar_t* __restrict s2,
size_t n) {
2015 #if defined(_MSC_VER) || defined(__STDC_LIB_EXT1__) 2016 return wcsncpy_s(s1, s1max, s2, n);
2059 static NLIB_C_INLINE
2060 errno_t nlib_strto_int8(int8_t* result,
const char* nptr,
char** endptr,
int base) {
2064 if (e != 0 && e != ERANGE)
return e;
2065 if (tmp > 127 || tmp < -128) {
2066 *result = tmp < 0 ? -128 : 127;
2069 *result = (int8_t)tmp;
2072 static NLIB_C_INLINE
2073 errno_t nlib_strto_int16(int16_t* result,
const char* nptr,
char** endptr,
int base) {
2077 if (e != 0 && e != ERANGE)
return e;
2078 if (tmp > 32767 || tmp < -32768) {
2079 *result = tmp < 0 ? -32768 : 32767;
2082 *result = (int16_t)tmp;
2085 static NLIB_C_INLINE
2086 errno_t nlib_strto_uint8(uint8_t* result,
const char* nptr,
char** endptr,
int base) {
2090 if (e != 0 && e != ERANGE)
return e;
2095 *result = (uint8_t)tmp;
2098 static NLIB_C_INLINE
2099 errno_t nlib_strto_uint16(uint16_t* result,
const char* nptr,
char** endptr,
int base) {
2103 if (e != 0 && e != ERANGE)
return e;
2108 *result = (uint16_t)tmp;
2121 const wchar_t* __restrict from,
size_t from_size)
2125 wchar_t* __restrict to,
size_t to_size,
2126 const nlib_utf8_t* __restrict from,
size_t from_size)
2133 size_t* __restrict supplementary_codepoint_count,
2134 size_t* __restrict len,
2138 size_t* __restrict supplementary_codepoint_count,
2139 size_t* __restrict from_read,
2181 const nlib_utf8_t* __restrict from,
size_t from_size)
2191 const nlib_utf8_t* __restrict from,
size_t from_size)
2195 size_t nlib_utf16len_(
const uint16_t* str)
NLIB_NONNULL;
2197 size_t nlib_utf16nlen_(
const uint16_t* str,
size_t maxsize)
NLIB_NONNULL;
2201 errno_t nlib_utf16ncpy_(uint16_t* s1,
size_t s1max,
const uint16_t* s2,
size_t n)
NLIB_NONNULL;
2204 return nlib_utf16len_((
const uint16_t*)str);
2208 return nlib_utf16nlen_((
const uint16_t*)str, maxsize);
2212 return nlib_utf16cpy_((uint16_t*)s1, s1max, (
const uint16_t*)s2);
2216 return nlib_utf16ncpy_((uint16_t*)s1, s1max, (
const uint16_t*)s2, n);
2220 size_t nlib_utf32len_(
const uint32_t* str)
NLIB_NONNULL;
2222 size_t nlib_utf32nlen_(
const uint32_t* str,
size_t maxsize)
NLIB_NONNULL;
2226 errno_t nlib_utf32ncpy_(uint32_t* s1,
size_t s1max,
const uint32_t* s2,
size_t n)
NLIB_NONNULL;
2229 return nlib_utf32len_((
const uint32_t*)str);
2233 return nlib_utf32nlen_((
const uint32_t*)str, maxsize);
2237 return nlib_utf32cpy_((uint32_t*)s1, s1max, (
const uint32_t*)s2);
2241 return nlib_utf32ncpy_((uint32_t*)s1, s1max, (
const uint32_t*)s2, n);
2249 return nlib_utf16cplen_ex_(count, NULL, (
const uint16_t*)str);
2253 return nlib_utf16cplen_ex_(count, len, (
const uint16_t*)str);
2276 NLIB_VIS_PUBLIC_ALT
void nlib_free(
void* ptr);
2299 # define NLIB_MEMCPY(a, b, c) memcpy((a), (b), (c)) 2302 #ifndef NLIB_MEMMOVE 2303 # define NLIB_MEMMOVE(a, b, c) memmove((a), (b), (c)) 2307 # define NLIB_MEMSET(a, b, c) memset((a), (b), (c)) 2327 return (
'0' <= ch && ch <=
'9') || (
'A' <= ch && ch <=
'Z') || (
'a' <= ch && ch <=
'z');
2330 return (
'A' <= ch && ch <=
'Z') || (
'a' <= ch && ch <=
'z');
2332 static NLIB_C_INLINE
int nlib_isblank(
int ch) {
return ch ==
' ' || ch ==
'\t'; }
2333 static NLIB_C_INLINE
int nlib_iscntrl(
int ch) {
return (ch >= 0 && ch <= 0x1F) || ch == 0x7F; }
2334 static NLIB_C_INLINE
int nlib_isdigit(
int ch) {
return (
'0' <= ch && ch <=
'9'); }
2335 static NLIB_C_INLINE
int nlib_isgraph(
int ch) {
return ch >= 0x21 && ch <= 0x7E; }
2336 static NLIB_C_INLINE
int nlib_islower(
int ch) {
return (ch >=
'a' && ch <=
'z'); }
2337 static NLIB_C_INLINE
int nlib_isprint(
int ch) {
return ch >= 0x20 && ch <= 0x7E; }
2338 static NLIB_C_INLINE
int nlib_ispunct(
int ch) {
return (ch >= 0x00 && ch <= 0x20) || ch == 0x7F; }
2340 return ((ch) ==
' ' || (ch) ==
'\t' || (ch) ==
'\n');
2342 static NLIB_C_INLINE
int nlib_isupper(
int ch) {
return (ch >=
'A' && ch <=
'Z'); }
2344 return (
unsigned int)(ch -
'0') < 10u ||
2345 (
unsigned int)((ch | 0x20) -
'a') < 6u;
2348 return (ch >=
'A' && ch <=
'Z') ? ch + (
'a' -
'A') : ch;
2351 return (ch >=
'a' && ch <=
'z') ? ch - (
'a' -
'A') : ch;
2356 static NLIB_C_INLINE
2358 #if defined(_MSC_VER) || defined(__STDC_LIB_EXT1__) 2359 return memcpy_s(s1, s1max, s2, n);
2361 #ifndef NLIB_NONNULL_ENABLED 2362 if (!s1 || !s2)
return ERANGE;
2365 NLIB_MEMSET(s1, 0, s1max);
2368 NLIB_MEMCPY(s1, s2, n);
2374 static NLIB_C_INLINE
2376 #if defined(_MSC_VER) || defined(__STDC_LIB_EXT1__) 2377 return memmove_s(s1, s1max, s2, n);
2379 #ifndef NLIB_NONNULL_ENABLED 2380 if (!s1 || !s2)
return ERANGE;
2382 if (s1max < n)
return ERANGE;
2383 NLIB_MEMMOVE(s1, s2, n);
2388 static NLIB_C_INLINE
2390 NLIB_EINVAL_IFNULL(buf);
2391 NLIB_MEMSET(buf, ch, n);
2398 #if defined(NLIB_SSE42) 2400 return _mm_popcnt_u32(x);
2403 return _mm_popcnt_u32(x);
2407 return (
int)_mm_popcnt_u64(x);
2409 uint32_t lo = (uint32_t)(x & 0xFFFFFFFFU);
2410 uint32_t hi = (uint32_t)((x >> 32) & 0xFFFFFFFFU);
2411 return _mm_popcnt_u32(lo) + _mm_popcnt_u32(hi);
2414 #elif defined(NLIB_NEON) 2416 uint8x8_t x0 = vcnt_u8(vreinterpret_u8_u64(vcreate_u64(x)));
2418 return vaddv_u8(x0);
2420 uint8x8_t x1 = vpadd_u8(x0, x0);
2421 return vget_lane_u8(x1, 0);
2425 uint8x8_t x0 = vcnt_u8(vreinterpret_u8_u64(vcreate_u64(x)));
2427 return vaddv_u8(x0);
2429 uint8x8_t x1 = vpadd_u8(x0, x0);
2430 uint8x8_t x2 = vpadd_u8(x1, x1);
2431 return vget_lane_u8(x2, 0);
2435 uint8x8_t x0 = vcnt_u8(vreinterpret_u8_u64(vcreate_u64(x)));
2437 return vaddv_u8(x0);
2439 uint8x8_t x1 = vpadd_u8(x0, x0);
2440 uint8x8_t x2 = vpadd_u8(x1, x1);
2441 uint8x8_t x3 = vpadd_u8(x2, x2);
2442 return vget_lane_u8(x3, 0);
2448 return _nlib_popcnt_array[(x >> 24) & 0xFF] + _nlib_popcnt_array[(x >> 16) & 0xFF] +
2449 _nlib_popcnt_array[(x >> 8) & 0xFF] + _nlib_popcnt_array[(x)& 0xFF];
2452 return _nlib_popcnt_array[(x >> 56) & 0xFF] + _nlib_popcnt_array[(x >> 48) & 0xFF] +
2453 _nlib_popcnt_array[(x >> 40) & 0xFF] + _nlib_popcnt_array[(x >> 32) & 0xFF] +
2454 _nlib_popcnt_array[(x >> 24) & 0xFF] + _nlib_popcnt_array[(x >> 16) & 0xFF] +
2455 _nlib_popcnt_array[(x >> 8) & 0xFF] + _nlib_popcnt_array[(x)& 0xFF];
2458 return _nlib_popcnt_array[(x >> 8) & 0xFF] + _nlib_popcnt_array[(x)& 0xFF];
2470 #if defined(_MSC_VER) 2473 return _BitScanReverse(&cnt, x) ? (int)(31 - cnt) : 32;
2478 return _BitScanForward(&cnt, x) ? cnt : 32;
2483 return _BitScanReverse64(&cnt, x) ? (int)(63 - cnt) : 64;
2487 DWORD dw = (DWORD)(x >> 32);
2488 if (_BitScanReverse(&cnt, dw)) {
2489 return (
int)(31 - cnt);
2492 return _BitScanReverse(&cnt, dw) ?
2493 (int)(63 - cnt) : 64;
2500 return _BitScanForward64(&cnt, x) ? cnt : 64;
2503 DWORD dw = (DWORD)(x);
2504 if (_BitScanForward(&cnt, dw)) {
2507 dw = (DWORD)(x >> 32);
2508 return _BitScanForward(&cnt, dw) ?
2509 (int)(32 + cnt) : 64;
2518 unsigned int dw = (
unsigned int)(x >> 32);
2523 dw = (
unsigned int)(x);
2529 #elif defined(NN_PLATFORM_CTR) 2541 static size_t nlib_strlcpy(
char* __restrict s1,
const char* __restrict s2,
size_t s1max)
2543 static NLIB_C_INLINE
2544 size_t nlib_strlcpy(
char* __restrict s1,
const char* __restrict s2,
size_t s1max) {
2545 #if defined(__FreeBSD__) 2546 return strlcpy(s1, s2, s1max);
2550 NLIB_MEMCPY(s1, s2, len + 1);
2552 NLIB_MEMCPY(s1, s2, s1max - 1);
2553 s1[s1max - 1] =
'\0';
2563 #if __has_builtin(__builtin_bitreverse32) 2564 return __builtin_bitreverse32(x);
2565 #elif __has_builtin(__builtin_arm_rbit) 2566 return __builtin_arm_rbit(x);
2567 #elif defined(__arm__) && !defined(NN_PLATFORM_CTR) 2570 x = ((x & 0x55555555U) << 1) | ((x >> 1) & 0x55555555U);
2571 x = ((x & 0x33333333U) << 2) | ((x >> 2) & 0x33333333U);
2572 x = ((x & 0x0F0F0F0FU) << 4) | ((x >> 4) & 0x0F0F0F0FU);
2574 x = _byteswap_ulong(x);
2575 #elif defined(CAFE) || defined(NN_PLATFORM_CTR) 2576 x = (x << 24) | ((x & 0xFF00) << 8) |
2577 ((x >> 8) & 0xFF00) | (x >> 24);
2579 x = __builtin_bswap32(x);
2586 #if __has_builtin(__builtin_bitreverse64) 2587 return __builtin_bitreverse64(x);
2588 #elif __has_builtin(__builtin_arm_rbit64) 2589 return __builtin_arm_rbit64(x);
2590 #elif __has_builtin(__builtin_arm_rbit) 2591 return __builtin_arm_rbit(x >> 32) |
2592 (((uint64_t)__builtin_arm_rbit(x)) << 32);
2593 #elif defined(__arm__) && !defined(NN_PLATFORM_CTR) 2594 return __rbit(x >> 32) |
2595 (((uint64_t)__rbit(x)) << 32);
2597 x = ((x & 0x5555555555555555ULL) << 1) | ((x >> 1) & 0x5555555555555555ULL);
2598 x = ((x & 0x3333333333333333ULL) << 2) | ((x >> 2) & 0x3333333333333333ULL);
2599 x = ((x & 0x0F0F0F0F0F0F0F0FULL) << 4) | ((x >> 4) & 0x0F0F0F0F0F0F0F0FULL);
2601 x = _byteswap_uint64(x);
2602 #elif defined(CAFE) || defined(NN_PLATFORM_CTR) 2605 ((x & 0xFF00U) << 40) |
2606 ((x & 0xFF0000U) << 24) |
2607 ((x & 0xFF000000U) << 8) |
2608 ((x >> 8) & 0xFF000000U) |
2609 ((x >> 24) & 0xFF0000U) |
2610 ((x >> 40) & 0xFF00U) |
2613 x = __builtin_bswap64(x);
2627 #if defined(_MSC_VER) 2628 #if defined(n_EXPORTS) 2629 #undef NLIB_VIS_PUBLIC 2630 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 2631 #elif defined(nx_misc_EXPORTS) 2632 # undef NLIB_VIS_PUBLIC 2633 # define NLIB_VIS_PUBLIC NLIB_WINEXPORT 2637 #endif // INCLUDE_NN_NLIB_PLATFORM_H_
メッセージキューの設定や現在の状態を格納する構造体です。