nlib
Platform.h File Reference

C-based declaration of the basic API. More...

#include <stddef.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "nn/nlib/Platform_unix.h"
#include <nmmintrin.h>
#include <errno.h>

Go to the source code of this file.

Classes

struct  nlib_mq_attr
 Structure to store the settings and current status of a message queue. More...
 

Macros

#define RSIZE_MAX   0x7FFFFFFFL
 Defines a value somewhat smaller than the maximum value of size_t. More...
 
#define NLIB_NOEXCEPT   noexcept
 Defines noexcept geared to the environment, or the equivalent. More...
 
Attributes

A macro for handling differences in attributes in every compiler.

#define NLIB_WARN(exp)   ("WARNING: " exp)
 Outputs a warning. More...
 
#define NLIB_ASSUME(cond)   switch (0) case 0: default: if (cond) ; else __builtin_unreachable() /* NOLINT */
 Indicates that cond is true and provides tips for optimizing the compiler. More...
 

Typedefs

typedef int32_t nlib_long_compatible_t
 Defines an integer type that is compatible with long using typedef.
 
typedef uint32_t nlib_ulong_compatible_t
 Defines an integer type that is compatible with unsigned long using typedef.
 

Functions

const char * nlib_error_string (errno_t e)
 Returns a string literal corresponding to the error value of nlib. More...
 
unsigned int nlib_get_native_last_error (void)
 Returns the last generated native error code. More...
 
errno_t nlib_memcpy (void *s1, size_t s1max, const void *s2, size_t n)
 An implementation corresponding to N1078 memcpy_s. More...
 
errno_t nlib_memmove (void *s1, size_t s1max, const void *s2, size_t n)
 An implementation corresponding to N1078 memmove_s. More...
 
errno_t nlib_memset (void *buf, int ch, size_t n)
 Makes a function call corresponding to memset(buf, ch, n). More...
 
int nlib_memcmp (const void *buf1, const void *buf2, size_t n)
 Compares the n bytes from the starts of buf1 and buf2 as unsigned char data. More...
 
const void * nlib_memchr (const void *s, int c, size_t n)
 Searches the n bytes from the start of the memory region (s, s + n) and returns a pointer to byte c. More...
 
const void * nlib_memrchr (const void *s, int c, size_t n)
 Searches the n bytes from the end of memory region (s, s + n) and returns a pointer to byte c. More...
 
const void * nlib_memchr_not (const void *s, int c, size_t n)
 Searches the n bytes from the start of memory region(s, s + n) and returns a pointer that does not point to byte c. More...
 
const void * nlib_memchr_range_not (const void *s, const char *range, size_t n)
 Searches the n bytes from the start of memory region (s, s + n) and returns a pointer to a character not contained in range. More...
 
const void * nlib_memchr_lt (const void *s, int c, size_t n)
 Searches the n bytes from the start of memory region (s, s + n) and returns a pointer to data having a character less than byte c. More...
 
const void * nlib_memchr_gt (const void *s, int c, size_t n)
 Searches the n bytes from the start of memory region (s, s + n) and returns a pointer to data having a character larger than byte c. More...
 
const void * nlib_memchr_mb (const void *s, size_t n)
 Searches the n bytes from the start of memory region (s, s + n) and returns a pointer to the location where 0x80 or more bytes is stored. More...
 
const char * nlib_skipws (size_t *cnt_lf, const char **last_lf, const char *s, size_t n)
 Searches a string made up of n characters and returns the pointer to the first character found that is not a white-space character. More...
 
size_t nlib_strlen (const char *s)
 Internally calls strlen(). In some cases, it may operate as an independent implementation. More...
 
size_t nlib_strnlen (const char *s, size_t maxsize)
 An implementation corresponding to N1078 strnlen_s. More...
 
errno_t nlib_strcpy (char *s1, size_t s1max, const char *s2)
 An implementation corresponding to N1078 strcpy_s. More...
 
errno_t nlib_strncpy (char *s1, size_t s1max, const char *s2, size_t n)
 An implementation corresponding to N1078 strncpy_s. More...
 
const char * nlib_strchr (const char *s, int c)
 Searches for a character from the start of a string. More...
 
const char * nlib_strrchr (const char *s, int c)
 Searches for a character from the end of a string. More...
 
const char * nlib_strchr_mb (const char *s)
 Searches for a character from the start of a string and then returns either the null character or the pointer to 0x80-0xFF bytes. More...
 
size_t nlib_wcslen (const wchar_t *s)
 Makes a call to thewcslen function. In some cases, it may operate as an independent implementation. More...
 
size_t nlib_wcsnlen (const wchar_t *s, size_t maxsize)
 An implementation corresponding to N1078 wcsnlen_s. More...
 
errno_t nlib_wcscpy (wchar_t *s1, size_t s1max, const wchar_t *s2)
 An implementation corresponding to N1078 wcscpy_s. More...
 
errno_t nlib_wcsncpy (wchar_t *s1, size_t s1max, const wchar_t *s2, size_t n)
 An implementation corresponding to N1078 wcsncpy_s. More...
 
errno_t nlib_strcat (char *s1, size_t s1max, const char *s2)
 An implementation corresponding to N1078 strcat_s. More...
 
errno_t nlib_strncat (char *s1, size_t s1max, const char *s2, size_t n)
 An implementation corresponding to N1078 strncat_s. More...
 
errno_t nlib_wcscat (wchar_t *s1, size_t s1max, const wchar_t *s2)
 An implementation corresponding to N1078 wcscat_s. More...
 
errno_t nlib_wcsncat (wchar_t *s1, size_t s1max, const wchar_t *s2, size_t n)
 An implementation corresponding to N1078 wcsncat_s. More...
 
template<size_t N>
errno_t nlib_strcpy (char(&s1)[N], const char *s2) noexcept
 The function template version of nlib_strcpy.
 
template<size_t N>
errno_t nlib_strncpy (char(&s1)[N], const char *s2, size_t n) noexcept
 The function template version of nlib_strncpy.
 
template<size_t N>
errno_t nlib_wcscpy (wchar_t(&s1)[N], const wchar_t *s2) noexcept
 The function template version of nlib_wcscpy.
 
template<size_t N>
errno_t nlib_wcsncpy (wchar_t(&s1)[N], const wchar_t *s2, size_t n) noexcept
 The function template version of nlib_wcsncpy.
 
template<size_t N>
errno_t nlib_strcat (char(&s1)[N], const char *s2) noexcept
 The function template version of nlib_strcat.
 
template<size_t N>
errno_t nlib_strncat (char(&s1)[N], const char *s2, size_t n) noexcept
 The function template version of nlib_strncat.
 
template<size_t N>
errno_t nlib_wcscat (wchar_t(&s1)[N], const wchar_t *s2) noexcept
 The function template version of nlib_wcscat.
 
template<size_t N>
errno_t nlib_wcsncat (wchar_t(&s1)[N], const wchar_t *s2, size_t n) noexcept
 The function template version of nlib_wcsncat.
 
Random Value Generation
NLIB_CHECK_RESULT errno_t nlib_gen_random (void *buf, size_t size)
 Generates a random value of size bytes and stores it in buf. More...
 
Allocation of Memory From the Operating System
errno_t nlib_mempagesize (size_t *size)
 Gets the page size. More...
 
errno_t nlib_virtual_alloc (void **ptr, size_t size)
 Allocates virtual memory address space. More...
 
errno_t nlib_virtual_free (void *ptr, size_t size)
 Frees the allocated virtual memory address space. More...
 
errno_t nlib_physical_alloc (void *ptr, size_t size, int prot)
 Allocates physical memory. More...
 
errno_t nlib_physical_free (void *ptr, size_t size)
 Frees the allocated physical memory. More...
 
errno_t nlib_mlock (void *addr, size_t len)
 The specified memory region is not swapped out. More...
 
errno_t nlib_munlock (void *addr, size_t len)
 The specified memory region can be swapped out. More...
 
Mutex
errno_t nlib_mutex_init (nlib_mutex *mutex)
 Initializes a mutex. More...
 
errno_t nlib_mutex_recursive_init (nlib_mutex *mutex)
 Initializes a recursive mutex. More...
 
errno_t nlib_mutex_recursive_timed_init (nlib_mutex *mutex)
 Initializes a mutex that is recursive and can time out. More...
 
errno_t nlib_mutex_lock (nlib_mutex *mutex)
 Locks the specified mutex. More...
 
NLIB_CHECK_RESULT errno_t nlib_mutex_trylock (nlib_mutex *mutex)
 Locks mutex, but only if it is not locked. More...
 
NLIB_CHECK_RESULT errno_t nlib_mutex_trylock_for (nlib_mutex *mutex, nlib_duration delta)
 Locks the specified mutex. Times out. More...
 
errno_t nlib_mutex_unlock (nlib_mutex *mutex)
 Unlocks the specified mutex. More...
 
errno_t nlib_mutex_destroy (nlib_mutex *mutex)
 Destroys the specified mutex object and frees any associated resources. More...
 
Semaphores
errno_t nlib_semaphore_init (nlib_semaphore *sem, int initial_count)
 Initializes the semaphore object specified by sem. More...
 
errno_t nlib_semaphore_wait (nlib_semaphore *sem)
 Waits until the semaphore count is no longer 0 and decrements the semaphore count by 1. More...
 
NLIB_CHECK_RESULT errno_t nlib_semaphore_trywait (nlib_semaphore *sem)
 Decrements the semaphore count by 1 if the count is not 0. More...
 
NLIB_CHECK_RESULT errno_t nlib_semaphore_trywait_for (nlib_semaphore *sem, nlib_duration duration)
 Decrements the semaphore count by 1 if the count is not 0. If 0, waits for the period specified by duration. More...
 
errno_t nlib_semaphore_post (nlib_semaphore *sem, int *previous_count)
 Increments the semaphore count by 1. More...
 
errno_t nlib_semaphore_post_ex (nlib_semaphore *sem, int release_count, int *previous_count)
 Increments the semaphore count by the amount specified by releaseCount. More...
 
errno_t nlib_semaphore_destroy (nlib_semaphore *sem)
 Destroys the semaphore count. More...
 
Condition Variables
errno_t nlib_cond_init (nlib_cond *cond)
 Initializes a condition variable. More...
 
errno_t nlib_cond_signal (nlib_cond *cond)
 Resumes the execution of one thread that is waiting for condition variable cond. More...
 
errno_t nlib_cond_broadcast (nlib_cond *cond)
 Resumes the execution of all threads that are waiting for the conditional variable cond. More...
 
errno_t nlib_cond_wait (nlib_cond *cond, nlib_mutex *mutex)
 Unlocks mutex and waits for a condition variable. It then relocks mutex after execution resumes. More...
 
NLIB_CHECK_RESULT errno_t nlib_cond_wait_for (nlib_cond *cond, nlib_mutex *mutex, nlib_duration duration)
 Unlocks mutex and waits for just the duration amount of time for a condition variable. It then relocks mutex after execution resumes. More...
 
NLIB_CHECK_RESULT errno_t nlib_cond_wait_until (nlib_cond *cond, nlib_mutex *mutex, nlib_time abstime)
 Unlocks mutex and waits until abstime for a condition variable. It then relocks mutex after execution resumes. More...
 
errno_t nlib_cond_destroy (nlib_cond *cond)
 Destroys a condition variable object. More...
 
Debugging and Debug Output
errno_t nlib_write_stdout (size_t *result, const void *buf, size_t count)
 Writes a string to standard output. More...
 
errno_t nlib_write_stderr (size_t *result, const void *buf, size_t count)
 Writes a string to standard error output. More...
 
void nlib_debug_break (void)
 A breakpoint. More...
 
errno_t nlib_debug_backtrace (size_t *result, void **buffer, size_t count)
 Stores backtraces in the array specified by buffer. More...
 
errno_t nlib_debug_backtrace_gettext (char *str, size_t strbufsize, void *const *buf, size_t count)
 Creates string information from the data obtained using the nlib_debug_backtrace function. More...
 
errno_t nlib_getenv (size_t *result, char *buf, size_t bufsize, const char *varname)
 Gets the value for the environment variable as a string. More...
 
malloc
NLIB_CHECK_RESULT void * nlib_malloc (size_t size)
 A weak function that calls the C standard function malloc. nlib calls malloc via this function. More...
 
void nlib_free (void *ptr)
 A weak function that calls the C standard function free. nlib calls free via this function. More...
 
NLIB_CHECK_RESULT void * nlib_calloc (size_t nmemb, size_t size)
 A weak function that calls the C standard function calloc. nlib calls calloc via this function. More...
 
NLIB_CHECK_RESULT void * nlib_realloc (void *ptr, size_t size)
 A weak function that calls the C standard function realloc. nlib calls realloc via this function. More...
 
void nlib_free_size (void *ptr, size_t size)
 Frees memory of a specified size. The default action is to call the nlib_free function. More...
 
NLIB_CHECK_RESULT void * nlib_memalign (size_t alignment, size_t size)
 A weak function that calls the C standard function memalign. nlib calls memalign via this function. More...
 
count leading zeros, count trailing zeros, popcnt
int nlib_clz64 (uint64_t x)
 Returns the number of consecutive zero bits, with respect to the most significant bit (MSB). More...
 
int nlib_ctz64 (uint64_t x)
 Returns the number of consecutive zero bits, with respect to the least significant bit (LSB). More...
 
int nlib_clz (uint32_t x)
 Returns the number of consecutive zero bits, with respect to the most significant bit (MSB). More...
 
int nlib_ctz (uint32_t x)
 Returns the number of consecutive zero bits, with respect to the least significant bit (LSB). More...
 
int nlib_popcnt (uint32_t x)
 Returns the number of bits that are 1. More...
 
int nlib_popcnt64 (uint64_t x)
 Returns the number of bits that are 1. More...
 
int nlib_popcnt16 (uint16_t x)
 Returns the number of bits that are 1. More...
 

Version

Macros and functions for getting the nlib version information.

#define NLIB_VERSION_YEAR   2015
 Defines an integer that corresponds to the year portion of the version number.
 
#define NLIB_VERSION_YEAR_SHORT   15
 Defines an integer that corresponds to the lower two digits of the year portion of the version number.
 
#define NLIB_VERSION_DATE   0908
 Defines an integer that corresponds to the month portion of the version number.
 
#define NLIB_VERSION   20150908
 Defines the version number. Defines numerical values that correspond to the year, month, and day of the release.
 
int nlib_getversion (void)
 Dynamically gets the nlib version. More...
 

printf

Functions for handling differences with the standard library printf.

#define PRIdS   __PRIS_PREFIX "d"
 Used when a size_t type is shown by printf. Corresponds to %zd.
 
#define PRIxS   __PRIS_PREFIX "x"
 Used when a size_t type is shown by printf. Corresponds to %zx.
 
#define PRIuS   __PRIS_PREFIX "u"
 Used when a size_t type is shown by printf. Corresponds to %zu.
 
#define PRIXS   __PRIS_PREFIX "X"
 Used when a size_t type is shown by printf. Corresponds to %zX.
 
#define PRIoS   __PRIS_PREFIX "o"
 Used when a size_t type is shown by printf. Corresponds to %zo.
 
errno_t nlib_vsnprintf (size_t *count, char *buf, size_t size, const char *fmt, va_list args)
 A safer form of vsnprintf, with some differences from standard vsnprintf behavior. More...
 
errno_t nlib_snprintf (size_t *count, char *buf, size_t size, const char *fmt,...)
 A safer form of snprintf.
 
errno_t nlib_vdprintf (nlib_fd fd, size_t *count, const char *fmt, va_list args)
 The version of the vsnprintf function that outputs to a file descriptor.
 
errno_t nlib_dprintf (nlib_fd fd, size_t *count, const char *fmt,...)
 The version of the snprintf function that outputs to a file descriptor.
 
int nlib_printf (const char *fmt,...)
 The substitute for the printf function.
 
errno_t nlib_vsnwprintf (size_t *count, wchar_t *buf, size_t size, const wchar_t *fmt, va_list args)
 A safer form of vswprintf, with some differences from standard vswprintf behavior. More...
 
errno_t nlib_snwprintf (size_t *count, wchar_t *buf, size_t size, const wchar_t *fmt,...)
 A safer form of snwprintf.
 
errno_t nlib_vdwprintf (nlib_fd fd, size_t *count, const wchar_t *fmt, va_list args)
 The version of the vsnwprintf function that outputs to a file descriptor.
 
errno_t nlib_dwprintf (nlib_fd fd, size_t *count, const wchar_t *fmt,...)
 The version of the snwprintf function that outputs to a file descriptor.
 
int nlib_wprintf (const wchar_t *fmt,...)
 The substitute for the wprintf function.
 
template<size_t N>
errno_t nlib_vsnprintf (size_t *count, char(&buf)[N], const char *fmt, va_list args) noexcept
 The function template version of nlib_vsnprintf.
 
template<size_t N>
errno_t nlib_snprintf (size_t *count, char(&buf)[N], const char *fmt,...) noexcept
 The function template version of nlib_snprintf.
 
template<size_t N>
errno_t nlib_vsnwprintf (size_t *count, wchar_t(&buf)[N], const wchar_t *fmt, va_list args) noexcept
 The function template version of nlib_vsnwprintf.
 
template<size_t N>
errno_t nlib_snwprintf (size_t *count, wchar_t(&buf)[N], const wchar_t *fmt,...) noexcept
 The function template version of nlib_snwprintf.
 

Time and Duration

Functions for getting times and durations, and sleeping based on times and durations.

typedef int64_t nlib_time
 The type expressing the time in increments of 100 ns from the zero starting point of 1970-01-01. A 64-bit signed integer.
 
typedef int64_t nlib_duration
 The type expressing the time in increments of 100 ns. A 64-bit signed integer.
 
errno_t nlib_epochtime (nlib_time *t)
 Gets the current time. More...
 
errno_t nlib_ticktime (nlib_duration *t)
 Gets the elapsed time since the system was last started. More...
 
errno_t nlib_sleep (nlib_duration t)
 Sleeps for the duration of t. More...
 

TLS

Thread local storage (TLS) is a mechanism for accessing different memory for each thread.

typedef pthread_key_t nlib_tls
 The type for TLS slot IDs.
 
typedef void(* nlib_tls_destructor) (void *tls_value)
 The type for the TLS destructor function called when the thread is ended. More...
 
NLIB_CHECK_RESULT errno_t nlib_tls_alloc (nlib_tls *tls, nlib_tls_destructor destr)
 Allocates a new ID for the specified TLS slot. More...
 
errno_t nlib_tls_free (nlib_tls tls)
 Frees the ID corresponding to the TLS slot. More...
 
errno_t nlib_tls_setvalue (nlib_tls tls, const void *value)
 Stores a value in a TLS slot. More...
 
errno_t nlib_tls_getvalue (nlib_tls tls, void **value)
 Gets the value from a TLS slot. More...
 

Spinlock

#define NLIB_SPINLOCK_INITIALIZER   (0)
 Macro for statically initializing nlib_spinlock.
 
typedef int32_t nlib_spinlock
 Spinlock variable type. Used by statically initializing with NLIB_SPINLOCK_INITIALIZER. More...
 

Read-Write Locks

A mechanism for allowing multiple readers to access a read-write lock at the same time, and for allowing only exclusive access by the writer when updating.

typedef struct nlib_rwlock_ nlib_rwlock
 The type for a read-write lock object. More...
 
errno_t nlib_rwlock_init (nlib_rwlock *rwlock)
 Initializes a read-write lock object. More...
 
errno_t nlib_rwlock_destroy (nlib_rwlock *rwlock)
 Destroys a read-write lock object. More...
 
errno_t nlib_rwlock_rdlock (nlib_rwlock *rwlock)
 Gets the read lock, and enters the critical section. Blocks until it can get a lock. More...
 
errno_t nlib_rwlock_tryrdlock (nlib_rwlock *rwlock)
 Gets the read lock, and attempts to enter the critical section. More...
 
errno_t nlib_rwlock_tryrdlock_for (nlib_rwlock *rwlock, nlib_duration duration)
 Gets the read lock, and attempts to enter the critical section. Times out. More...
 
errno_t nlib_rwlock_tryrdlock_until (nlib_rwlock *rwlock, nlib_time abstime)
 Gets the read lock, and attempts to enter the critical section. Times out. More...
 
errno_t nlib_rwlock_rdunlock (nlib_rwlock *rwlock)
 Releases the read lock. More...
 
errno_t nlib_rwlock_wrlock (nlib_rwlock *rwlock)
 Gets a write lock, and enters the critical section. Blocks until it can get a lock. More...
 
errno_t nlib_rwlock_trywrlock (nlib_rwlock *rwlock)
 Gets a write lock, and attempts to enter the critical section. More...
 
errno_t nlib_rwlock_trywrlock_for (nlib_rwlock *rwlock, nlib_duration duration)
 Gets a write lock, and attempts to enter the critical section. Times out. More...
 
errno_t nlib_rwlock_trywrlock_until (nlib_rwlock *rwlock, nlib_time abstime)
 Gets a write lock, and attempts to enter the critical section. Times out. More...
 
errno_t nlib_rwlock_wrunlock (nlib_rwlock *rwlock)
 Releases a write lock. More...
 

Conditional Variable for Read-Write Locks

A conditional variable available for nlib_rwlock.

#define NLIB_RWLOCK_INITIALIZER
 Constant for statically initializing nlib_rwlock. More...
 
typedef struct nlib_condrwlock_ nlib_condrwlock
 Type of the conditional variable for read-write locks. More...
 
errno_t nlib_condrwlock_init (nlib_condrwlock *cond)
 Initializes a read-write lock conditional variable. More...
 
errno_t nlib_condrwlock_destroy (nlib_condrwlock *cond)
 Destroys a read-write lock conditional variable. More...
 
errno_t nlib_condrwlock_signal (nlib_condrwlock *cond)
 Resumes the execution of one thread that is waiting for the read-write lock conditional variable cond. More...
 
errno_t nlib_condrwlock_broadcast (nlib_condrwlock *cond)
 Resumes the execution of all threads that are waiting for the read-write lock conditional variable cond. More...
 
errno_t nlib_condrwlock_wait (nlib_condrwlock *cond, nlib_rwlock *rwlock, int rdlock)
 Unlocks rwlock and waits for a conditional variable. It then locks rwlock again after the execution resumes. More...
 
errno_t nlib_condrwlock_wait_for (nlib_condrwlock *cond, nlib_rwlock *rwlock, nlib_duration duration, int rdlock)
 Unlocks rwlock and waits for a conditional variable. It then locks rwlock again after the execution resumes. More...
 
errno_t nlib_condrwlock_wait_until (nlib_condrwlock *cond, nlib_rwlock *rwlock, nlib_time abstime, int rdlock)
 Unlocks rwlock and waits for a conditional variable. It then locks rwlock again after the execution resumes. More...
 

Barriers

Synchronizes multiple threads to the end of a particular task. Can be used repeatedly.

typedef struct nlib_barrier_ nlib_barrier
 The type for a barrier object. More...
 
errno_t nlib_barrier_init (nlib_barrier *barrier, unsigned int count)
 Initializes a barrier object. More...
 
errno_t nlib_barrier_destroy (nlib_barrier *barrier)
 Destroys a barrier object. More...
 
errno_t nlib_barrier_wait (nlib_barrier *barrier)
 Waits for a thread. More...
 

One-Time Execution

A mechanism for guaranteeing that a particular process is executed only once.

#define NLIB_ONCE_INIT   { 0 }
 The value for statically initializing nlib_onceflag.
 
typedef struct nlib_onceflag_ nlib_onceflag
 The structure to use with nlib_once.
 
typedef void(* nlib_oncefunc) (void)
 The type for functions to execute with nlib_once.
 
errno_t nlib_once (nlib_onceflag *flag, nlib_oncefunc func)
 Ensures that func is executed only one time at most. More...
 
errno_t nlib_tryonce (nlib_onceflag *flag, nlib_oncefunc func)
 Basically the same as nlib_once, but returns EBUSY if func is running on another thread. More...
 

Message Queue

See nlib_mq_open() for the features of nlib message queue.

typedef int32_t nlib_mq
 Handle associated with a message queue. If the handle is cleared to zero (usubg memset()), it will always be an invalid handle.
 
typedef void * nlib_mq_msg
 Type of messages stored in a message queue.
 
typedef void(* nlib_mq_msg_destructor) (nlib_mq_msg)
 Destructor function for messages taken from a message queue.
 
NLIB_CHECK_RESULT errno_t nlib_mq_open (nlib_mq *mq, const nlib_mq_attr *attr)
 Creates a message queue to be used to exchange messages across threads. More...
 
NLIB_CHECK_RESULT errno_t nlib_mq_getattr (nlib_mq mq, nlib_mq_attr *attr)
 Obtains the attribute set to the message queue indicated with a handle. More...
 
errno_t nlib_mq_close (nlib_mq mq)
 Closes the message queue indicated with a handle. More...
 
errno_t nlib_mq_readonly (nlib_mq mq)
 Sets the message queue indicated with a handle as receive-only. More...
 
NLIB_CHECK_RESULT errno_t nlib_mq_send (nlib_mq mq, nlib_mq_msg msg, int prio)
 Sends a message to a queue. More...
 
NLIB_CHECK_RESULT errno_t nlib_mq_send_until (nlib_mq mq, nlib_mq_msg msg, int prio, nlib_time abstime)
 Sends a messages with a time-out set to the queue. More...
 
NLIB_CHECK_RESULT errno_t nlib_mq_receive (nlib_mq mq, nlib_mq_msg *msg, int *prio)
 Receives a message from a queue. It is the user's responsibility to delete the received messages using a destructor function. More...
 
NLIB_CHECK_RESULT errno_t nlib_mq_receive_until (nlib_mq mq, nlib_mq_msg *msg, int *prio, nlib_time abstime)
 Receives a message with a time-out set from a queue. It is the user's responsibility to delete the received messages using a destructor function. More...
 
NLIB_CHECK_RESULT errno_t nlib_mq_drop (nlib_mq mq, nlib_mq_msg *msg, int *prio)
 Receives a message with the lowest priority from a queue. It is the user's responsibility to delete the received messages using a destructor function. More...
 

Threads

#define NLIB_THREAD_INVALID   (nlib_thread)(0)
 Value indicating an invalid thread.
 
typedef struct nlib_thread_attr_ nlib_thread_attr
 The thread attribute to apply to a newly created thread.
 
typedef void(* nlib_thread_func) (void *arg)
 A function to be run on a different thread. More...
 
typedef int nlib_thread_id
 A unique integer value for each thread.
 
errno_t nlib_yield (void)
 Relinquishes thread execution rights. More...
 
NLIB_CHECK_RESULT errno_t nlib_thread_create (nlib_thread *thread, const nlib_thread_attr *attr, nlib_thread_func func, void *arg)
 Creates and executes a new thread. More...
 
errno_t nlib_thread_join (nlib_thread thread)
 Waits for the thread to terminate. More...
 
errno_t nlib_thread_detach (nlib_thread thread)
 Detaches an executing thread. More...
 
errno_t nlib_thread_self (nlib_thread *thread)
 Stores the nlib_thread value corresponding to the executing thread. More...
 
errno_t nlib_thread_getconcurrency (unsigned int *num_cpu)
 Gets the number of hardware threads. More...
 
errno_t nlib_thread_getid (nlib_thread_id *id)
 Stores a unique integer value corresponding to the executing thread. More...
 
int nlib_thread_equal (nlib_thread th1, nlib_thread th2)
 Checks whether two threads point to the same thread. More...
 
errno_t nlib_thread_getcpu (int *result)
 Gets the CPU on which the called thread is executing. More...
 
errno_t nlib_thread_setaffinity (nlib_thread thread, uint32_t affinity)
 Sets a processor affinity mask for the specified thread. More...
 
errno_t nlib_thread_setname (const char *string_literal)
 Attaches a name to the thread. More...
 
errno_t nlib_thread_attr_init (nlib_thread_attr *attr)
 Initializes a thread attribute object and sets it to the default. More...
 
errno_t nlib_thread_attr_setint (nlib_thread_attr *attr, int key, int value)
 Sets an integer corresponding to the key of the thread attribute object. More...
 
errno_t nlib_thread_attr_getint (const nlib_thread_attr *attr, int key, int *value)
 Gets the integer corresponding to the key of the thread attribute object. More...
 
errno_t nlib_thread_attr_setptr (nlib_thread_attr *attr, int key, void *value)
 Sets a pointer corresponding to the key of the thread attribute object. As of now, returns EINVAL only. More...
 
errno_t nlib_thread_attr_getptr (const nlib_thread_attr *attr, int key, void **value)
 Gets the pointer corresponding to the key of the thread attribute object. As of now, returns EINVAL only. More...
 
errno_t nlib_thread_attr_setstack (nlib_thread_attr *attr, void *stack_addr, size_t stack_size)
 Sets a stack setting for thread attribute objects.

Platform Implementation
Win32 nlib independent implementation (The set value is ignored)
Linux pthread_attr_getstack()
FreeBSD pthread_attr_getstack()
OS X pthread_attr_getstack()
CAFE nlib independent implementation
CTR nlib independent implementation
More...
 
errno_t nlib_thread_attr_getstack (const nlib_thread_attr *attr, void **stack_addr, size_t *stack_size)
 Obtains a stack setting for thread attribute objects. More...
 
errno_t nlib_thread_attr_destroy (nlib_thread_attr *attr)
 Destroys a thread-initialization object. More...
 
errno_t nlib_thread_getpriority (nlib_thread thread, int *priority)
 Gets the current execution priority of the thread. The meaning of the numerical value is implementation-dependent. More...
 
errno_t nlib_thread_setpriority (nlib_thread thread, int priority)
 Sets the execution priority of the thread. The meaning of the numerical value is implementation-dependent. More...
 
errno_t nlib_thread_priority_min (int *priority)
 Gets the smallest numerical value that can be specified for the execution priority. More...
 
errno_t nlib_thread_priority_max (int *priority)
 Gets the largest numerical value that can be specified for the execution priority. More...
 
errno_t nlib_thread_priority_default (int *priority)
 Gets the default numerical value that can be specified for the execution priority. More...
 
void nlib_thread_exit (void) NLIB_NORETURN
 Ends the called thread. More...
 
void nlib_thread_cleanup_push (void(*fn)(void *), void *arg)
 Pushes fn to a dedicated stack. More...
 
void nlib_thread_cleanup_pop (int exec)
 Deletes the handler at the top of the stack storing the cleanup handler. More...
 
void nlib_thread_exit_cpp () NLIB_NORETURN
 Ends the called thread. More...
 

Logging

Outputs logs to standard output, syslog, and files.

enum  nlib_log_priority { ,
  NLIB_LOG_VERBOSE,
  NLIB_LOG_DEBUG,
  NLIB_LOG_INFO,
  NLIB_LOG_WARN,
  NLIB_LOG_ERROR,
  NLIB_LOG_FATAL ,
  NLIB_LOG_LEVEL_EQUAL_OR_ABOVE = 0x10,
  NLIB_LOG_LEVEL_EQUAL_OR_BELOW = 0x20,
  NLIB_LOG_LEVEL_ALL = 0x30
}
 Defines the priority (level category) for output. More...
 
int nlib_log_print (int prio, const char *tag, const char *fmt,...)
 Outputs log messages. More...
 
int nlib_log_vprint (int prio, const char *tag, const char *fmt, va_list ap)
 Outputs log messages. More...
 
errno_t nlib_log_attr_setint (int prio, int key, int value)
 Specifies where to output the log for each level of priority. More...
 

Files

#define NLIB_FD_O_RDONLY   (0x0000)
 Used for the flags parameter of the nlib_fd_open function.
 
#define NLIB_FD_O_WRONLY   (0x0001)
 Used for the flags parameter of the nlib_fd_open function.
 
#define NLIB_FD_O_RDWR   (0x0002)
 Used for the flags parameter of the nlib_fd_open function.
 
#define NLIB_FD_O_CREAT   (0x0100)
 Used for the flags parameter of the nlib_fd_open function.
 
#define NLIB_FD_O_TRUNC   (0x0800)
 Used for the flags parameter of the nlib_fd_open function.
 
#define NLIB_FD_O_APPEND   (0x0008)
 Used for the flags parameter of the nlib_fd_open function.
 
#define NLIB_SEEK_SET   0
 Used for the whence parameter of the nlib_fd_seek function.
 
#define NLIB_SEEK_CUR   1
 Used for the whence parameter of the nlib_fd_seek function.
 
#define NLIB_FD_INVALID   (-1)
 A macro defining invalid file descriptors.
 
typedef int64_t nlib_offset
 The offset to the file. A 64-bit integer.
 
typedef int nlib_fd
 The original file descriptor of nlib (a 32-bit integer value). More...
 
NLIB_CHECK_RESULT errno_t nlib_fd_open (nlib_fd *fd, const char *native_path, unsigned int flags, const void *extra)
 Opens a file. More...
 
NLIB_CHECK_RESULT errno_t nlib_fd_close (nlib_fd fd)
 Closes a file. The file descriptor will be released even if an error is returned. More...
 
NLIB_CHECK_RESULT errno_t nlib_fd_read (size_t *result, nlib_fd fd, void *buf, size_t count)
 Reads (up to) count bytes from the file descriptor into buf. More...
 
NLIB_CHECK_RESULT errno_t nlib_fd_write (size_t *result, nlib_fd fd, const void *buf, size_t count)
 Writes (up to) count bytes from buf to the file descriptor. More...
 
NLIB_CHECK_RESULT errno_t nlib_fd_seek (nlib_offset *result, nlib_fd fd, nlib_offset offset, int whence)
 Changes the file offset. More...
 
NLIB_CHECK_RESULT errno_t nlib_fd_pread (size_t *result, nlib_fd fd, void *buf, size_t count, nlib_offset offset)
 Reads the file descriptor from the specified offset. The offset for the file descriptor will not be changed. More...
 
NLIB_CHECK_RESULT errno_t nlib_fd_pwrite (size_t *result, nlib_fd fd, const void *buf, size_t count, nlib_offset offset)
 Writes to the file descriptor at the specified offset. The offset for the file descriptor will not be changed. More...
 
NLIB_CHECK_RESULT errno_t nlib_fd_truncate (nlib_fd fd, nlib_offset length)
 Extends or truncates the file to be the specified size. More...
 
NLIB_CHECK_RESULT errno_t nlib_fd_getsize (nlib_offset *size, nlib_fd fd)
 Gets the file size. More...
 
NLIB_CHECK_RESULT errno_t nlib_fd_flush (nlib_fd fd)
 Flushes the write to the file descriptor. More...
 
errno_t nlib_fd_sync (nlib_fd fd)
 Synchronizes the content of a file in memory with what is on the device. More...
 
errno_t nlib_fd_native_handle (void **native_handle, nlib_fd fd)
 Gets (the equivalent of) the native file handle. More...
 
NLIB_CHECK_RESULT errno_t nlib_fd_readv (size_t *result, nlib_fd fd, const nlib_fd_iovec *iov, int iovcnt)
 Loads multiple non-continuous buffers from the file associated with fd. More...
 
NLIB_CHECK_RESULT errno_t nlib_fd_writev (size_t *result, nlib_fd fd, const nlib_fd_iovec *iov, int iovcnt)
 Writes from multiple non-continuous buffers to the file associated with fd. More...
 
NLIB_CHECK_RESULT errno_t nlib_fd_preadv (size_t *result, nlib_fd fd, const nlib_fd_iovec *iov, int iovcnt, nlib_offset offset)
 Same as the nlib_fd_readv function except when the pread or nlib_fd_pread function is used internally.
 
NLIB_CHECK_RESULT errno_t nlib_fd_pwritev (size_t *result, nlib_fd fd, const nlib_fd_iovec *iov, int iovcnt, nlib_offset offset)
 Same as the nlib_fd_writev function except when the pwrite or nlib_fd_pwrite function is used internally.
 
NLIB_CHECK_RESULT errno_t nlib_unlink (const char *native_path)
 Deletes a file. More...
 
NLIB_CHECK_RESULT errno_t nlib_mkdir (const char *native_path, unsigned int flags)
 Creates a directory. More...
 
NLIB_CHECK_RESULT errno_t nlib_rmdir (const char *native_path)
 Deletes a directory. More...
 
NLIB_CHECK_RESULT errno_t nlib_rename (const char *old_path, const char *new_path)
 Renames a file. More...
 
NLIB_CHECK_RESULT errno_t nlib_dir_open (nlib_dir *dir, const char *native_path)
 Opens a directory. More...
 
errno_t nlib_dir_close (nlib_dir dir)
 Closes a directory. More...
 
NLIB_CHECK_RESULT errno_t nlib_dir_read (nlib_dirent *ent, nlib_dir dir)
 Reads one directory entry, if there are any. More...
 
errno_t nlib_is_dir (int *result, const char *native_path)
 Checks whether the path is for a directory. Sets 0 in *result and returns 0 if no path exists. More...
 
errno_t nlib_exist_path (int *result, const char *native_path)
 Checks whether the path exists. More...
 
errno_t nlib_disk_freespace (const char *native_path, uint64_t *free_bytes_available, uint64_t *total_bytes, uint64_t *total_free_bytes)
 Gets information related to the capacity of the storage region to which the specified path belongs. More...
 

Unicode

typedef uint16_t nlib_utf16_t
 Uses typedef to define as char16_t if that can be used. If not, it uses typedef to define as uint16_t.
 
typedef uint32_t nlib_utf32_t
 Uses typedef to define as char32_t if that can be used. If not, it uses typedef to define as uint32_t.
 
errno_t nlib_wide_to_utf8 (size_t *utf8count, char *utf8, size_t buflen, const wchar_t *wcstr)
 Converts a UTF-16/UTF-32 string into a UTF-8 string. More...
 
errno_t nlib_utf8_to_wide (size_t *wccount, wchar_t *wcstr, size_t buflen, const char *utf8)
 Converts a UTF-8 string into a UTF-16/UTF-32 string. More...
 
NLIB_CHECK_RESULT errno_t nlib_strcplen (size_t *count, const char *str)
 Gets the number of code points in the string. More...
 
NLIB_CHECK_RESULT errno_t nlib_strcplen_ex (size_t *count, size_t *len, const char *str)
 Gets the number of code points in the string and the string length. More...
 
NLIB_CHECK_RESULT errno_t nlib_wcscplen (size_t *count, const wchar_t *str)
 Gets the number of code points in the string. More...
 
errno_t nlib_swapendian_16 (uint16_t *p, size_t count)
 Swaps the endianness. More...
 
errno_t nlib_swapendian_32 (uint32_t *p, size_t count)
 Swaps the endianness. More...
 
errno_t nlib_swapendian_64 (uint64_t *p, size_t count)
 Swaps the endianness. More...
 
NLIB_CHECK_RESULT int nlib_utf16_to_utf32char (nlib_utf32_t *utf32, nlib_utf16_t upper, nlib_utf16_t lower) noexcept
 Converts a single code point from UTF-16 into UTF-32. More...
 
NLIB_CHECK_RESULT int nlib_utf32char_to_utf16 (nlib_utf16_t *upper, nlib_utf16_t *lower, nlib_utf32_t utf32) noexcept
 Converts a single UTF-32 character into UTF-16. More...
 
NLIB_CHECK_RESULT int nlib_utf8_to_utf32char (nlib_utf32_t *utf32, const char *utf8) noexcept
 Converts UTF-8 into one character's worth of UTF-32. More...
 
NLIB_CHECK_RESULT int nlib_utf32char_to_utf8 (char(&utf8)[4], nlib_utf32_t utf32) noexcept
 Converts the UTF-32 for one character into UTF-8. More...
 
errno_t nlib_utf16_to_utf8 (size_t *utf8count, char *utf8, size_t buflen, const nlib_utf16_t *utf16) noexcept
 Converts a UTF-16 string into a UTF-8 string. More...
 
errno_t nlib_utf8_to_utf16 (size_t *utf16count, nlib_utf16_t *utf16, size_t buflen, const char *utf8) noexcept
 Converts a UTF-8 string into a UTF-16 string. The UTF-16 string is null-terminated. More...
 
errno_t nlib_utf32_to_utf8 (size_t *utf8count, char *utf8, size_t buflen, const nlib_utf32_t *utf32) noexcept
 Converts a UTF-32 string into a UTF-8 string. More...
 
errno_t nlib_utf8_to_utf32 (size_t *utf32count, nlib_utf32_t *utf32, size_t buflen, const char *utf8) noexcept
 Converts a UTF-8 string into a UTF-32 string. More...
 
NLIB_CHECK_RESULT size_t nlib_utf16len (const nlib_utf16_t *str) noexcept
 Counts the number of nlib_utf16_t-type characters, not including the null character. More...
 
NLIB_CHECK_RESULT size_t nlib_utf16nlen (const nlib_utf16_t *str, size_t maxsize) noexcept
 The UTF-16 version of the nlib_strnlen function. More...
 
errno_t nlib_utf16cpy (nlib_utf16_t *s1, size_t s1max, const nlib_utf16_t *s2) noexcept
 The UTF-16 version of the nlib_strcpy function. More...
 
errno_t nlib_utf16ncpy (nlib_utf16_t *s1, size_t s1max, const nlib_utf16_t *s2, size_t n) noexcept
 The UTF-16 version of the nlib_strcpy function. More...
 
NLIB_CHECK_RESULT size_t nlib_utf32len (const nlib_utf32_t *str) noexcept
 Counts the number of nlib_utf32_t-type characters, not including the null character. More...
 
NLIB_CHECK_RESULT size_t nlib_utf32nlen (const nlib_utf32_t *str, size_t maxsize) noexcept
 The UTF-32 version of the nlib_strnlen function. More...
 
errno_t nlib_utf32cpy (nlib_utf32_t *s1, size_t s1max, const nlib_utf32_t *s2) noexcept
 The UTF-32 version of the nlib_strcpy function. More...
 
errno_t nlib_utf32ncpy (nlib_utf32_t *s1, size_t s1max, const nlib_utf32_t *s2, size_t n) noexcept
 The UTF-32 version of the nlib_strcpy function. More...
 
errno_t nlib_utf16cplen (size_t *count, const nlib_utf16_t *str) noexcept
 Gets the number of code points in the string. More...
 
errno_t nlib_utf16cplen_ex (size_t *count, size_t *len, const nlib_utf16_t *str) noexcept
 Gets the number of code points in the string. More...
 
NLIB_CHECK_RESULT errno_t nlib_utf32cplen (size_t *count, const nlib_utf32_t *str) noexcept
 Gets the number of code points in the string. More...
 
template<size_t N>
NLIB_CHECK_RESULT errno_t nlib_wide_to_utf8 (size_t *result, char(&utf8)[N], const wchar_t *wcstr) noexcept
 The function template version of nlib_wide_to_utf8.
 
template<size_t N>
NLIB_CHECK_RESULT errno_t nlib_utf8_to_wide (size_t *result, wchar_t(&wcstr)[N], const char *utf8) noexcept
 The function template version of nlib_utf8_to_wide.
 
template<size_t N>
NLIB_CHECK_RESULT errno_t nlib_utf16_to_utf8 (size_t *utf8count, char(&utf8)[N], const nlib_utf16_t *utf16) noexcept
 The function template version of nlib_utf16_to_utf8.
 
template<size_t N>
NLIB_CHECK_RESULT errno_t nlib_utf8_to_utf16 (size_t *utf16count, nlib_utf16_t(&utf16)[N], const char *utf8) noexcept
 The function template version of nlib_utf8_to_utf16.
 
template<size_t N>
NLIB_CHECK_RESULT errno_t nlib_utf32_to_utf8 (size_t *utf8count, char(&utf8)[N], const nlib_utf32_t *utf32) noexcept
 The function template version of nlib_utf32_to_utf8.
 
template<size_t N>
NLIB_CHECK_RESULT errno_t nlib_utf8_to_utf32 (size_t *utf32count, nlib_utf32_t(&utf32)[N], const char *utf8) noexcept
 The function template version of nlib_utf8_to_utf32.
 

Detailed Description

C-based declaration of the basic API.

Definition in file Platform.h.