ELinks 0.20.0
cutils.h File Reference
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <sys/time.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
#include <errno.h>
#include <pthread.h>
#include <limits.h>
#include <unistd.h>
Include dependency graph for cutils.h:
This graph shows which files directly or indirectly include this file:

Data Structures

struct  DynBuf

Macros

#define likely(x)
#define unlikely(x)
#define no_inline   __attribute__((noinline))
#define __maybe_unused   __attribute__((unused))
#define offsetof(type, field)
#define countof(x)
#define endof(x)
#define container_of(ptr, type, member)
#define minimum_length(n)
#define JS_PRINTF_FORMAT
#define JS_PRINTF_FORMAT_ATTR(format_param, dots_param)
#define JS__PATH_MAX   8192
#define UTF8_CHAR_LEN_MAX   4
#define JS_HAVE_THREADS   1
#define JS_ONCE_INIT   PTHREAD_ONCE_INIT
#define JS_X87_FPCW_SAVE_AND_ADJUST(cw)
#define JS_X87_FPCW_RESTORE(cw)
#define NANOSEC   ((uint64_t) 1e9)

Typedefs

typedef void * DynBufReallocFunc(void *opaque, void *ptr, size_t size)
typedef struct DynBuf DynBuf
typedef pthread_once_t js_once_t
typedef pthread_mutex_t js_mutex_t
typedef pthread_cond_t js_cond_t
typedef pthread_t js_thread_t
typedef void(* exchange_f) (void *a, void *b, size_t size)
typedef int(* cmp_f) (const void *, const void *, void *opaque)

Enumerations

enum  {
  UTF8_PLAIN_ASCII = 0 , UTF8_NON_ASCII = 1 , UTF8_HAS_16BIT = 2 , UTF8_HAS_NON_BMP1 = 4 ,
  UTF8_HAS_ERRORS = 8
}
enum  { JS_THREAD_CREATE_DETACHED = 1 }

Functions

static void js__pstrcpy (char *buf, int buf_size, const char *str)
static char * js__pstrcat (char *buf, int buf_size, const char *s)
static int js__strstart (const char *str, const char *val, const char **ptr)
static int js__has_suffix (const char *str, const char *suffix)
static uint8_t is_be (void)
static int max_int (int a, int b)
static int min_int (int a, int b)
static uint32_t max_uint32 (uint32_t a, uint32_t b)
static uint32_t min_uint32 (uint32_t a, uint32_t b)
static int64_t max_int64 (int64_t a, int64_t b)
static int64_t min_int64 (int64_t a, int64_t b)
static uint32_t hash32 (uint32_t a)
static int clz32 (unsigned int a)
static int clz64 (uint64_t a)
static int ctz32 (unsigned int a)
static int ctz64 (uint64_t a)
static uint64_t get_u64 (const uint8_t *tab)
static int64_t get_i64 (const uint8_t *tab)
static void put_u64 (uint8_t *tab, uint64_t val)
static uint32_t get_u32 (const uint8_t *tab)
static int32_t get_i32 (const uint8_t *tab)
static void put_u32 (uint8_t *tab, uint32_t val)
static uint32_t get_u16 (const uint8_t *tab)
static int32_t get_i16 (const uint8_t *tab)
static void put_u16 (uint8_t *tab, uint16_t val)
static uint32_t get_u8 (const uint8_t *tab)
static int32_t get_i8 (const uint8_t *tab)
static void put_u8 (uint8_t *tab, uint8_t val)
static uint16_t bswap16 (uint16_t x)
static uint32_t bswap32 (uint32_t v)
static uint64_t bswap64 (uint64_t v)
static double fromfp16 (uint16_t v)
static uint16_t tofp16 (double d)
static int isfp16nan (uint16_t v)
static int isfp16zero (uint16_t v)
static void dbuf_init (DynBuf *s)
static void dbuf_init2 (DynBuf *s, void *opaque, DynBufReallocFunc *realloc_func)
static int dbuf_claim (DynBuf *s, size_t len)
static int dbuf_put (DynBuf *s, const void *data, size_t len)
static int dbuf_put_self (DynBuf *s, size_t offset, size_t len)
static int __dbuf_putc (DynBuf *s, uint8_t c)
static int __dbuf_put_u16 (DynBuf *s, uint16_t val)
static int __dbuf_put_u32 (DynBuf *s, uint32_t val)
static int __dbuf_put_u64 (DynBuf *s, uint64_t val)
static int dbuf_putstr (DynBuf *s, const char *str)
static int dbuf_putc (DynBuf *s, uint8_t val)
static int dbuf_put_u16 (DynBuf *s, uint16_t val)
static int dbuf_put_u32 (DynBuf *s, uint32_t val)
static int dbuf_put_u64 (DynBuf *s, uint64_t val)
static int JS_PRINTF_FORMAT_ATTR (2, 3) dbuf_printf(DynBuf *s
static int JS_PRINTF_FORMAT const char static void dbuf_free (DynBuf *s)
static bool dbuf_error (DynBuf *s)
static void dbuf_set_error (DynBuf *s)
static int utf8_scan (const char *buf, size_t len, size_t *plen)
static size_t utf8_encode_len (uint32_t c)
static size_t utf8_encode (uint8_t buf[minimum_length(UTF8_CHAR_LEN_MAX)], uint32_t c)
static uint32_t utf8_decode_len (const uint8_t *p, size_t max_len, const uint8_t **pp)
static uint32_t utf8_decode (const uint8_t *p, const uint8_t **pp)
static size_t utf8_decode_buf8 (uint8_t *dest, size_t dest_len, const char *src, size_t src_len)
static size_t utf8_decode_buf16 (uint16_t *dest, size_t dest_len, const char *src, size_t src_len)
static size_t utf8_encode_buf8 (char *dest, size_t dest_len, const uint8_t *src, size_t src_len)
static size_t utf8_encode_buf16 (char *dest, size_t dest_len, const uint16_t *src, size_t src_len)
static bool is_surrogate (uint32_t c)
static bool is_hi_surrogate (uint32_t c)
static bool is_lo_surrogate (uint32_t c)
static uint32_t get_hi_surrogate (uint32_t c)
static uint32_t get_lo_surrogate (uint32_t c)
static uint32_t from_surrogate (uint32_t hi, uint32_t lo)
static int from_hex (int c)
static uint8_t is_upper_ascii (uint8_t c)
static uint8_t to_upper_ascii (uint8_t c)
static void rqsort (void *base, size_t nmemb, size_t size, int(*cmp)(const void *, const void *, void *), void *arg)
static uint64_t float64_as_uint64 (double d)
static double uint64_as_float64 (uint64_t u64)
static int64_t js__gettimeofday_us (void)
static uint64_t js__hrtime_ns (void)
static size_t js__malloc_usable_size (const void *ptr)
static int js_exepath (char *buffer, size_t *size)
static void js_once (js_once_t *guard, void(*callback)(void))
static void js_mutex_init (js_mutex_t *mutex)
static void js_mutex_destroy (js_mutex_t *mutex)
static void js_mutex_lock (js_mutex_t *mutex)
static void js_mutex_unlock (js_mutex_t *mutex)
static void js_cond_init (js_cond_t *cond)
static void js_cond_destroy (js_cond_t *cond)
static void js_cond_signal (js_cond_t *cond)
static void js_cond_broadcast (js_cond_t *cond)
static void js_cond_wait (js_cond_t *cond, js_mutex_t *mutex)
static int js_cond_timedwait (js_cond_t *cond, js_mutex_t *mutex, uint64_t timeout)
static int js_thread_create (js_thread_t *thrd, void(*start)(void *), void *arg, int flags)
static int js_thread_join (js_thread_t thrd)
static void * dbuf_default_realloc (void *opaque, void *ptr, size_t size)
 va_start (ap, fmt)
 va_end (ap)
 if (len<(int) sizeof(buf))
 vsnprintf ((char *)(s->buf+s->size), s->allocated_size - s->size, fmt, ap)
static void exchange_bytes (void *a, void *b, size_t size)
static void exchange_one_byte (void *a, void *b, size_t size)
static void exchange_int16s (void *a, void *b, size_t size)
static void exchange_one_int16 (void *a, void *b, size_t size)
static void exchange_int32s (void *a, void *b, size_t size)
static void exchange_one_int32 (void *a, void *b, size_t size)
static void exchange_int64s (void *a, void *b, size_t size)
static void exchange_one_int64 (void *a, void *b, size_t size)
static void exchange_int128s (void *a, void *b, size_t size)
static void exchange_one_int128 (void *a, void *b, size_t size)
static exchange_f exchange_func (const void *base, size_t size)
static void heapsortx (void *base, size_t nmemb, size_t size, cmp_f cmp, void *opaque)
static void * med3 (void *a, void *b, void *c, cmp_f cmp, void *opaque)
static void rqsort (void *base, size_t nmemb, size_t size, cmp_f cmp, void *opaque)

Variables

static int JS_PRINTF_FORMAT const char * fmt
static int JS_PRINTF_FORMAT const char char buf [128]
int len = vsnprintf(buf, sizeof(buf), fmt, ap)
 else
s size = len
 return

Macro Definition Documentation

◆ __maybe_unused

#define __maybe_unused   __attribute__((unused))

◆ container_of

#define container_of ( ptr,
type,
member )
Value:
((type *)((uint8_t *)(ptr) - offsetof(type, member)))
#define offsetof(type, field)
Definition cutils.h:92
const char * type
Definition download.c:1972

◆ countof

#define countof ( x)
Value:
(sizeof(x) / sizeof((x)[0]))

◆ endof

#define endof ( x)
Value:
((x) + countof(x))
#define countof(x)
Definition attr.c:20

◆ JS__PATH_MAX

#define JS__PATH_MAX   8192

◆ JS_HAVE_THREADS

#define JS_HAVE_THREADS   1

◆ JS_ONCE_INIT

#define JS_ONCE_INIT   PTHREAD_ONCE_INIT

◆ JS_PRINTF_FORMAT

#define JS_PRINTF_FORMAT

◆ JS_PRINTF_FORMAT_ATTR

#define JS_PRINTF_FORMAT_ATTR ( format_param,
dots_param )
Value:
__attribute__((format(printf, format_param, dots_param)))

◆ JS_X87_FPCW_RESTORE

#define JS_X87_FPCW_RESTORE ( cw)

◆ JS_X87_FPCW_SAVE_AND_ADJUST

#define JS_X87_FPCW_SAVE_AND_ADJUST ( cw)

◆ likely

#define likely ( x)
Value:
__builtin_expect(!!(x), 1)

◆ minimum_length

#define minimum_length ( n)
Value:
static n
int n
Definition general.c:931

◆ NANOSEC

#define NANOSEC   ((uint64_t) 1e9)

◆ no_inline

#define no_inline   __attribute__((noinline))

◆ offsetof

#define offsetof ( type,
field )
Value:
((size_t) &((type *)0)->field)

◆ unlikely

#define unlikely ( x)
Value:
__builtin_expect(!!(x), 0)

◆ UTF8_CHAR_LEN_MAX

#define UTF8_CHAR_LEN_MAX   4

Typedef Documentation

◆ cmp_f

typedef int(* cmp_f) (const void *, const void *, void *opaque)

◆ DynBuf

typedef struct DynBuf DynBuf

◆ DynBufReallocFunc

typedef void * DynBufReallocFunc(void *opaque, void *ptr, size_t size)

◆ exchange_f

typedef void(* exchange_f) (void *a, void *b, size_t size)

◆ js_cond_t

typedef pthread_cond_t js_cond_t

◆ js_mutex_t

typedef pthread_mutex_t js_mutex_t

◆ js_once_t

typedef pthread_once_t js_once_t

◆ js_thread_t

typedef pthread_t js_thread_t

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
JS_THREAD_CREATE_DETACHED 

◆ anonymous enum

anonymous enum
Enumerator
UTF8_PLAIN_ASCII 
UTF8_NON_ASCII 
UTF8_HAS_16BIT 
UTF8_HAS_NON_BMP1 
UTF8_HAS_ERRORS 

Function Documentation

◆ __dbuf_put_u16()

int __dbuf_put_u16 ( DynBuf * s,
uint16_t val )
inlinestatic

◆ __dbuf_put_u32()

int __dbuf_put_u32 ( DynBuf * s,
uint32_t val )
inlinestatic

◆ __dbuf_put_u64()

int __dbuf_put_u64 ( DynBuf * s,
uint64_t val )
inlinestatic

◆ __dbuf_putc()

int __dbuf_putc ( DynBuf * s,
uint8_t c )
inlinestatic

◆ bswap16()

uint16_t bswap16 ( uint16_t x)
inlinestatic

◆ bswap32()

uint32_t bswap32 ( uint32_t v)
inlinestatic

◆ bswap64()

uint64_t bswap64 ( uint64_t v)
inlinestatic

◆ clz32()

int clz32 ( unsigned int a)
inlinestatic

◆ clz64()

int clz64 ( uint64_t a)
inlinestatic

◆ ctz32()

int ctz32 ( unsigned int a)
inlinestatic

◆ ctz64()

int ctz64 ( uint64_t a)
inlinestatic

◆ dbuf_claim()

int dbuf_claim ( DynBuf * s,
size_t len )
inlinestatic

◆ dbuf_default_realloc()

void * dbuf_default_realloc ( void * opaque,
void * ptr,
size_t size )
static

◆ dbuf_error()

bool dbuf_error ( DynBuf * s)
inlinestatic

◆ dbuf_free()

void dbuf_free ( DynBuf * s)
inlinestatic

◆ dbuf_init()

void dbuf_init ( DynBuf * s)
inlinestatic

◆ dbuf_init2()

void dbuf_init2 ( DynBuf * s,
void * opaque,
DynBufReallocFunc * realloc_func )
inlinestatic

◆ dbuf_put()

int dbuf_put ( DynBuf * s,
const void * data,
size_t len )
inlinestatic

◆ dbuf_put_self()

int dbuf_put_self ( DynBuf * s,
size_t offset,
size_t len )
inlinestatic

◆ dbuf_put_u16()

int dbuf_put_u16 ( DynBuf * s,
uint16_t val )
inlinestatic

◆ dbuf_put_u32()

int dbuf_put_u32 ( DynBuf * s,
uint32_t val )
inlinestatic

◆ dbuf_put_u64()

int dbuf_put_u64 ( DynBuf * s,
uint64_t val )
inlinestatic

◆ dbuf_putc()

int dbuf_putc ( DynBuf * s,
uint8_t val )
inlinestatic

◆ dbuf_putstr()

int dbuf_putstr ( DynBuf * s,
const char * str )
inlinestatic

◆ dbuf_set_error()

void dbuf_set_error ( DynBuf * s)
inlinestatic

◆ exchange_bytes()

void exchange_bytes ( void * a,
void * b,
size_t size )
static

◆ exchange_func()

exchange_f exchange_func ( const void * base,
size_t size )
inlinestatic

◆ exchange_int128s()

void exchange_int128s ( void * a,
void * b,
size_t size )
static

◆ exchange_int16s()

void exchange_int16s ( void * a,
void * b,
size_t size )
static

◆ exchange_int32s()

void exchange_int32s ( void * a,
void * b,
size_t size )
static

◆ exchange_int64s()

void exchange_int64s ( void * a,
void * b,
size_t size )
static

◆ exchange_one_byte()

void exchange_one_byte ( void * a,
void * b,
size_t size )
static

◆ exchange_one_int128()

void exchange_one_int128 ( void * a,
void * b,
size_t size )
static

◆ exchange_one_int16()

void exchange_one_int16 ( void * a,
void * b,
size_t size )
static

◆ exchange_one_int32()

void exchange_one_int32 ( void * a,
void * b,
size_t size )
static

◆ exchange_one_int64()

void exchange_one_int64 ( void * a,
void * b,
size_t size )
static

◆ float64_as_uint64()

uint64_t float64_as_uint64 ( double d)
inlinestatic

◆ from_hex()

int from_hex ( int c)
inlinestatic

◆ from_surrogate()

uint32_t from_surrogate ( uint32_t hi,
uint32_t lo )
inlinestatic

◆ fromfp16()

double fromfp16 ( uint16_t v)
inlinestatic

◆ get_hi_surrogate()

uint32_t get_hi_surrogate ( uint32_t c)
inlinestatic

◆ get_i16()

int32_t get_i16 ( const uint8_t * tab)
inlinestatic

◆ get_i32()

int32_t get_i32 ( const uint8_t * tab)
inlinestatic

◆ get_i64()

int64_t get_i64 ( const uint8_t * tab)
inlinestatic

◆ get_i8()

int32_t get_i8 ( const uint8_t * tab)
inlinestatic

◆ get_lo_surrogate()

uint32_t get_lo_surrogate ( uint32_t c)
inlinestatic

◆ get_u16()

uint32_t get_u16 ( const uint8_t * tab)
inlinestatic

◆ get_u32()

uint32_t get_u32 ( const uint8_t * tab)
inlinestatic

◆ get_u64()

uint64_t get_u64 ( const uint8_t * tab)
inlinestatic

◆ get_u8()

uint32_t get_u8 ( const uint8_t * tab)
inlinestatic

◆ hash32()

uint32_t hash32 ( uint32_t a)
inlinestatic

◆ heapsortx()

void heapsortx ( void * base,
size_t nmemb,
size_t size,
cmp_f cmp,
void * opaque )
static

◆ if()

if ( )

◆ is_be()

uint8_t is_be ( void )
inlinestatic

◆ is_hi_surrogate()

bool is_hi_surrogate ( uint32_t c)
inlinestatic

◆ is_lo_surrogate()

bool is_lo_surrogate ( uint32_t c)
inlinestatic

◆ is_surrogate()

bool is_surrogate ( uint32_t c)
inlinestatic

◆ is_upper_ascii()

uint8_t is_upper_ascii ( uint8_t c)
inlinestatic

◆ isfp16nan()

int isfp16nan ( uint16_t v)
inlinestatic

◆ isfp16zero()

int isfp16zero ( uint16_t v)
inlinestatic

◆ js__gettimeofday_us()

int64_t js__gettimeofday_us ( void )
inlinestatic

◆ js__has_suffix()

int js__has_suffix ( const char * str,
const char * suffix )
inlinestatic

◆ js__hrtime_ns()

uint64_t js__hrtime_ns ( void )
inlinestatic

◆ js__malloc_usable_size()

size_t js__malloc_usable_size ( const void * ptr)
inlinestatic

◆ js__pstrcat()

char * js__pstrcat ( char * buf,
int buf_size,
const char * s )
inlinestatic

◆ js__pstrcpy()

void js__pstrcpy ( char * buf,
int buf_size,
const char * str )
inlinestatic

◆ js__strstart()

int js__strstart ( const char * str,
const char * val,
const char ** ptr )
inlinestatic

◆ js_cond_broadcast()

void js_cond_broadcast ( js_cond_t * cond)
inlinestatic

◆ js_cond_destroy()

void js_cond_destroy ( js_cond_t * cond)
inlinestatic

◆ js_cond_init()

void js_cond_init ( js_cond_t * cond)
inlinestatic

◆ js_cond_signal()

void js_cond_signal ( js_cond_t * cond)
inlinestatic

◆ js_cond_timedwait()

int js_cond_timedwait ( js_cond_t * cond,
js_mutex_t * mutex,
uint64_t timeout )
inlinestatic

◆ js_cond_wait()

void js_cond_wait ( js_cond_t * cond,
js_mutex_t * mutex )
inlinestatic

◆ js_exepath()

int js_exepath ( char * buffer,
size_t * size )
inlinestatic

◆ js_mutex_destroy()

void js_mutex_destroy ( js_mutex_t * mutex)
inlinestatic

◆ js_mutex_init()

void js_mutex_init ( js_mutex_t * mutex)
inlinestatic

◆ js_mutex_lock()

void js_mutex_lock ( js_mutex_t * mutex)
inlinestatic

◆ js_mutex_unlock()

void js_mutex_unlock ( js_mutex_t * mutex)
inlinestatic

◆ js_once()

void js_once ( js_once_t * guard,
void(* callback )(void) )
inlinestatic

◆ JS_PRINTF_FORMAT_ATTR()

int JS_PRINTF_FORMAT_ATTR ( 2 ,
3  )
inlinestatic

◆ js_thread_create()

int js_thread_create ( js_thread_t * thrd,
void(* start )(void *),
void * arg,
int flags )
inlinestatic

◆ js_thread_join()

int js_thread_join ( js_thread_t thrd)
inlinestatic

◆ max_int()

int max_int ( int a,
int b )
inlinestatic

◆ max_int64()

int64_t max_int64 ( int64_t a,
int64_t b )
inlinestatic

◆ max_uint32()

uint32_t max_uint32 ( uint32_t a,
uint32_t b )
inlinestatic

◆ med3()

void * med3 ( void * a,
void * b,
void * c,
cmp_f cmp,
void * opaque )
inlinestatic

◆ min_int()

int min_int ( int a,
int b )
inlinestatic

◆ min_int64()

int64_t min_int64 ( int64_t a,
int64_t b )
inlinestatic

◆ min_uint32()

uint32_t min_uint32 ( uint32_t a,
uint32_t b )
inlinestatic

◆ put_u16()

void put_u16 ( uint8_t * tab,
uint16_t val )
inlinestatic

◆ put_u32()

void put_u32 ( uint8_t * tab,
uint32_t val )
inlinestatic

◆ put_u64()

void put_u64 ( uint8_t * tab,
uint64_t val )
inlinestatic

◆ put_u8()

void put_u8 ( uint8_t * tab,
uint8_t val )
inlinestatic

◆ rqsort() [1/2]

void rqsort ( void * base,
size_t nmemb,
size_t size,
cmp_f cmp,
void * opaque )
inlinestatic

◆ rqsort() [2/2]

void rqsort ( void * base,
size_t nmemb,
size_t size,
int(* cmp )(const void *, const void *, void *),
void * arg )
inlinestatic

◆ to_upper_ascii()

uint8_t to_upper_ascii ( uint8_t c)
inlinestatic

◆ tofp16()

uint16_t tofp16 ( double d)
inlinestatic

◆ uint64_as_float64()

double uint64_as_float64 ( uint64_t u64)
inlinestatic

◆ utf8_decode()

uint32_t utf8_decode ( const uint8_t * p,
const uint8_t ** pp )
inlinestatic

◆ utf8_decode_buf16()

size_t utf8_decode_buf16 ( uint16_t * dest,
size_t dest_len,
const char * src,
size_t src_len )
inlinestatic

◆ utf8_decode_buf8()

size_t utf8_decode_buf8 ( uint8_t * dest,
size_t dest_len,
const char * src,
size_t src_len )
inlinestatic

◆ utf8_decode_len()

uint32_t utf8_decode_len ( const uint8_t * p,
size_t max_len,
const uint8_t ** pp )
inlinestatic

◆ utf8_encode()

size_t utf8_encode ( uint8_t buf[minimum_length(UTF8_CHAR_LEN_MAX)],
uint32_t c )
inlinestatic

◆ utf8_encode_buf16()

size_t utf8_encode_buf16 ( char * dest,
size_t dest_len,
const uint16_t * src,
size_t src_len )
inlinestatic

◆ utf8_encode_buf8()

size_t utf8_encode_buf8 ( char * dest,
size_t dest_len,
const uint8_t * src,
size_t src_len )
inlinestatic

◆ utf8_encode_len()

size_t utf8_encode_len ( uint32_t c)
inlinestatic

◆ utf8_scan()

int utf8_scan ( const char * buf,
size_t len,
size_t * plen )
inlinestatic

◆ va_end()

va_end ( ap )

◆ va_start()

va_start ( ap ,
fmt  )

◆ vsnprintf()

vsnprintf ( (char *  )(s->buf+s->size),
s->allocated_size - s-> size,
fmt ,
ap  )

Variable Documentation

◆ buf

int JS_PRINTF_FORMAT const char char buf[128]
Initial value:
{
va_list ap

◆ else

else
Initial value:
{
if (dbuf_claim(s, len + 1))
return -1
static int dbuf_claim(DynBuf *s, size_t len)
Definition cutils.h:763
int len
Definition cutils.h:844
const char * s
Definition general.c:932

◆ fmt

int JS_PRINTF_FORMAT const char * fmt

◆ len

len = vsnprintf(buf, sizeof(buf), fmt, ap)

◆ return

return

◆ size

s size = len