~q3cpma/misc-tools

ref: 313cd570e4b81c5f6749356698ce617c65181b61 misc-tools/misc.h -rw-r--r-- 2.6 KiB
313cd570q3cpma README nit 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#if __STDC_VERSION__ >= 201112L
#	define NORETURN _Noreturn
#else
#	define NORETURN
#endif

// Use the format(printf) attribute only if using GCC/clang
#if defined(__GNUC__) || defined(__clang__)
#	define ATTR_FMT_PRINTF(fmt_index, chk_index) \
		__attribute__ ((format (printf, fmt_index, chk_index)))
#else
#	define ATTR_FMT_PRINTF(fmt_index, chk_index)
#endif

#if defined(__GNUC__) || defined(__clang__)
#	define ATTR_FMT_SCANF(fmt_index, chk_index) \
		__attribute__ ((format (scanf, fmt_index, chk_index)))
#else
#	define ATTR_FMT_SCANF(fmt_index, chk_index)
#endif

#define ARRAY_SIZE(a) (sizeof(a) / sizeof (a)[0])

// gensym implementation (generate a pseudo-unique variable name in a macro)
// could use a reserved name syntax (leading __ or _[A-Z]) but not needed
#define GENSYM__(line, x) gensym_ ## l ## line ## _ ## x
#define GENSYM_(line, x) GENSYM__(line, x)
#define GENSYM(x) GENSYM_(__LINE__, x)

#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)

#if defined(__GNUC__) || defined(__clang__)
#	define LIKELY(x)   (__builtin_expect(!!(x), 1))
#	define UNLIKELY(x) (__builtin_expect(!!(x), 0))
#else
#	define LIKELY(x)   (x)
#	define UNLIKELY(x) (x)
#endif


/* printf a message to stderr followed by a newline before exit(1) */
NORETURN void die(const char *fmt, ...) ATTR_FMT_PRINTF(1, 2);

/* getdelim() wrapper removing the delimiter if found at the end of the line
   and dying when encountering an error */
ssize_t xgetdelim(char **lineptr, size_t *n, int delim, FILE *stream);

static inline ssize_t xgetline(char **lineptr, size_t *n, FILE *stream)
{
	return xgetdelim(lineptr, n, '\n', stream);
}

/* Set SA_RESTART for all signals not having term/core as default handler */
void signals_nointerrupt(void);

/* integer log2 for u32; return 0 when x is 0 */
uint32_t log2_u32(uint32_t x);

/* Print a string as C string literal; uses C11's u8 notation if any byte > 127
 * is encountered (e.g. u8"café") */
void fprintf_cstrlit(FILE *stream, const char *str, size_t len);

/* Portable asprintf */
int pasprintf(char **s, const char *fmt, ...) ATTR_FMT_PRINTF(2, 3);

/* Wrappers around standard functions that die() in case of error */
float         xstrtof(const char *str);
unsigned long xstrtoul(const char *str, int base);
FILE *        xfopen(const char *path, const char *mode);
void          xfclose(FILE *stream);
void *        xmalloc(size_t size);
void *        xcalloc(size_t nmemb, size_t size);
void *        xrealloc(void *ptr, size_t size);
void *        xreallocarray(void *ptr, size_t nmemb, size_t size);