minecraft-pi-reborn/libreborn/include/libreborn/util.h

80 lines
2.0 KiB
C
Raw Permalink Normal View History

2021-06-17 21:32:24 +00:00
#pragma once
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <dlfcn.h>
#include "log.h"
// Check Memory Allocation
#define ALLOC_CHECK(obj) \
{ \
if (obj == NULL) { \
2022-04-15 01:12:42 +00:00
ERR("Memory Allocation Failed"); \
2021-06-17 21:32:24 +00:00
} \
}
// Hook Library Function
2024-04-02 23:22:01 +00:00
#ifdef __cplusplus
#define hooked_function_setup extern "C"
#else
#define hooked_function_setup
#endif
2021-06-17 21:32:24 +00:00
#define HOOK(name, return_type, args) \
typedef return_type (*name##_t)args; \
static name##_t real_##name = NULL; \
\
__attribute__((__unused__)) static void ensure_##name() { \
if (!real_##name) { \
dlerror(); \
real_##name = (name##_t) dlsym(RTLD_NEXT, #name); \
if (!real_##name) { \
ERR("Error Resolving Symbol: " #name ": %s", dlerror()); \
2021-06-17 21:32:24 +00:00
} \
} \
2021-07-05 01:23:12 +00:00
} \
2021-06-17 21:32:24 +00:00
\
2024-04-02 23:22:01 +00:00
hooked_function_setup __attribute__((__used__)) return_type name args
2021-06-17 21:32:24 +00:00
2022-03-14 23:09:25 +00:00
#ifdef __cplusplus
extern "C" {
#endif
2021-06-17 21:32:24 +00:00
// Safe Version Of pipe()
2022-03-14 23:09:25 +00:00
void safe_pipe2(int pipefd[2], int flags);
// Check If Two Percentages Are Different Enough To Be Logged
int is_progress_difference_significant(int32_t new_val, int32_t old_val);
2022-03-14 23:09:25 +00:00
2022-09-25 23:35:51 +00:00
// Lock File
int lock_file(const char *file);
void unlock_file(const char *file, int fd);
2023-09-17 19:51:35 +00:00
// Access Configuration At Runtime
const char *reborn_get_version();
int reborn_is_headless();
int reborn_is_server();
2024-02-01 03:13:20 +00:00
// Customize VTable
#define CUSTOM_VTABLE(name, parent) \
void _setup_##name##_vtable(parent##_vtable *vtable); \
2024-02-01 03:13:20 +00:00
static parent##_vtable *get_##name##_vtable() { \
static parent##_vtable *vtable = NULL; \
/* Allocate VTable */ \
if (vtable == NULL) { \
/* Init */ \
vtable = dup_##parent##_vtable(parent##_vtable_base); \
ALLOC_CHECK(vtable); \
/* Setup */ \
_setup_##name##_vtable(vtable); \
} \
/* Return */ \
return vtable; \
} \
/* User-Defined Setup Code */ \
void _setup_##name##_vtable(parent##_vtable *vtable)
2024-02-01 03:13:20 +00:00
2022-03-14 23:09:25 +00:00
#ifdef __cplusplus
2021-06-17 21:32:24 +00:00
}
2022-03-14 23:09:25 +00:00
#endif