common.h (1285B)
1#ifndef COMMON_H 2#define COMMON_H 3 4#define _GNU_SOURCE 5 6#include <stdlib.h> 7#include <stdint.h> 8#include <stdbool.h> 9#include <stdio.h> 10#include <assert.h> 11#include <unistd.h> 12 13 14/* round columns to a multiple of this */ 15#define CONFIG_ROUND_COLS 0x8 16 17/* mmap files larger than this */ 18#define CONFIG_LARGE_FILESIZE (256 * (1 << 20)) /* 256 megabytes */ 19 20/* microseconds to wait for the rest of what could be an escape sequence */ 21#define CONFIG_WAIT_ESCAPE (10000) /* 10 milliseconds */ 22 23 24typedef uint8_t byte; 25 26void die(char const *s) __attribute__((noreturn)); /* hyx.c */ 27void pdie(char const *s) __attribute__((noreturn)); /* hyx.c */ 28 29static inline size_t min(size_t x, size_t y) 30 { return x < y ? x : y; } 31static inline size_t max(size_t x, size_t y) 32 { return x > y ? x : y; } 33static inline size_t absdiff(size_t x, size_t y) 34 { return x > y ? x - y : y - x; } 35 36unsigned long bit_length(unsigned long n); 37 38void *malloc_strict(size_t len); 39void *realloc_strict(void *ptr, size_t len); 40 41void *mmap_strict(void *addr, size_t len, int prot, int flags, int fildes, off_t off); 42void munmap_strict(void *addr, size_t len); 43 44off_t lseek_strict(int fildes, off_t offset, int whence); 45 46char *fgets_retry(char *s, int size, FILE *stream); 47 48uint64_t monotonic_microtime(); 49 50#endif