utils 按作用拆成 log/macros/memory 三个模块

This commit is contained in:
2026-03-23 19:05:39 +08:00
parent fa93b3d733
commit 67f0c169f5
15 changed files with 179 additions and 175 deletions

43
src/base/log.c Normal file
View File

@@ -0,0 +1,43 @@
#include "base/log.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "app.h"
const char *current_time_str(void) {
static char buffer[25];
time_t now;
struct tm tm;
time(&now);
localtime_r(&now, &tm);
if (!strftime(buffer, sizeof(buffer), "%Y-%m-%d %T", &tm)) buffer[0] = '\0';
return buffer;
}
void _fatal(int line, const char *fct, const char *file, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
const char *format = "(EE) [%s] %s\n%s:%d:%s:\n";
fprintf(stderr, format, APP_NAME, current_time_str(), file, line, fct);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
void _warn(int line, const char *fct, const char *file, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
const char *format = "(WW) [%s] %s\n%s:%d:%s:\n";
fprintf(stderr, format, APP_NAME, current_time_str(), file, line, fct);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
}

32
src/base/log.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#include <stdarg.h>
#include <stdio.h>
#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

29
src/base/macros.h Normal file
View File

@@ -0,0 +1,29 @@
#pragma once
#include <stddef.h>
#include <sys/types.h>
#define ssizeof(foo) (ssize_t)sizeof(foo)
#define countof(foo) (ssizeof(foo) / ssizeof(foo[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
#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

73
src/base/memory.h Normal file
View File

@@ -0,0 +1,73 @@
#pragma once
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "base/macros.h"
#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)
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();
}
}
/*
* Unlike strlen(), a_strlen() accepts nullptr and returns 0 in that case.
*/
static inline ssize_t a_strlen(const char *s) { return s ? strlen(s) : 0; }
static constexpr size_t INIT_CAPACITY = 4;
static inline size_t next_capacity(size_t capacity) {
if (capacity) return capacity * 2;
return INIT_CAPACITY;
}