utils 按作用拆成 log/macros/memory 三个模块
This commit is contained in:
133
src/utils.h
133
src/utils.h
@@ -1,131 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
/* Compatibility umbrella header. Prefer including headers from src/base in new
|
||||
* code. */
|
||||
|
||||
#define ssizeof(foo) (ssize_t)sizeof(foo)
|
||||
#define countof(foo) (ssizeof(foo) / ssizeof(foo[0]))
|
||||
|
||||
#define p_alloc_nr(x) (((x) + 16) * 3 / 2)
|
||||
#define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count)))
|
||||
#define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
|
||||
#define p_realloc(pp, count) xrealloc((void *)(pp), sizeof(**(pp)) * (count))
|
||||
#define p_copy(src, count) xmemcopy((src), sizeof(*(src)) * (count))
|
||||
|
||||
#define p_delete(mem_p) \
|
||||
do { \
|
||||
void **__ptr = (void **)(mem_p); \
|
||||
free(*__ptr); \
|
||||
*(void **)__ptr = nullptr; \
|
||||
} while (0)
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define likely(expr) __builtin_expect(!!(expr), 1)
|
||||
#define unlikely(expr) __builtin_expect((expr), 0)
|
||||
#else
|
||||
#define likely(expr) expr
|
||||
#define unlikely(expr) expr
|
||||
#endif
|
||||
|
||||
static inline char *p_strdup(const char *text) {
|
||||
char *r = strdup(text);
|
||||
if (!r) abort();
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline char *p_strdup_nullable(const char *text) {
|
||||
if (!text) return nullptr;
|
||||
return p_strdup(text);
|
||||
}
|
||||
|
||||
static inline void *__attribute__((malloc)) xmalloc(ssize_t size) {
|
||||
void *ptr;
|
||||
|
||||
if (size <= 0) return nullptr;
|
||||
|
||||
ptr = calloc(1, size);
|
||||
|
||||
if (!ptr) abort();
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static inline void *xmemcopy(const void *src, size_t n) {
|
||||
if (!n) return nullptr;
|
||||
|
||||
void *mem = xmalloc(n);
|
||||
void *ret = memcpy(mem, src, n);
|
||||
if (ret != mem) abort();
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline void xrealloc(void **ptr, ssize_t newsize) {
|
||||
if (newsize <= 0)
|
||||
p_delete(ptr);
|
||||
else {
|
||||
*ptr = realloc(*ptr, newsize);
|
||||
if (!*ptr) abort();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief nullptr resistant strlen.
|
||||
*
|
||||
* Unlike it's libc sibling, a_strlen returns a ssize_t, and supports its
|
||||
* argument being nullptr.
|
||||
*
|
||||
* @param s the string.
|
||||
* @return the string length (or 0 if s is nullptr).
|
||||
*/
|
||||
static inline ssize_t a_strlen(const char *s) { return s ? strlen(s) : 0; }
|
||||
|
||||
#define fatal(format, ...) \
|
||||
_fatal(__LINE__, __FUNCTION__, __FILE__, format, ##__VA_ARGS__)
|
||||
void _fatal(int line, const char *function, const char *file,
|
||||
const char *format, ...) __attribute__((noreturn))
|
||||
__attribute__((format(printf, 4, 5)));
|
||||
|
||||
#define warn(format, ...) \
|
||||
_warn(__LINE__, __FUNCTION__, __FILE__, format, ##__VA_ARGS__)
|
||||
void _warn(int line, const char *function, const char *file, const char *format,
|
||||
...) __attribute__((format(printf, 4, 5)));
|
||||
|
||||
const char *current_time_str(void);
|
||||
|
||||
#if defined(RELEASE)
|
||||
#define logger(format, ...)
|
||||
#elif defined(LOG_FILE)
|
||||
static inline void logger(const char *format, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
FILE *log_file = fopen(LOG_FILE, "a+t");
|
||||
vfprintf(log_file, format, ap);
|
||||
va_end(ap);
|
||||
fclose(log_file);
|
||||
}
|
||||
#else
|
||||
#define logger(format, ...) printf(format, ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#ifdef MIN
|
||||
#undef MIN
|
||||
#endif
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#ifdef MAX
|
||||
#undef MAX
|
||||
#endif
|
||||
#ifndef MAX
|
||||
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
static constexpr size_t INIT_CAPACITY = 4;
|
||||
static inline size_t next_capacity(size_t capacity) {
|
||||
if (capacity) return capacity * 2;
|
||||
return INIT_CAPACITY;
|
||||
}
|
||||
#include "base/log.h"
|
||||
#include "base/macros.h"
|
||||
#include "base/memory.h"
|
||||
|
||||
Reference in New Issue
Block a user