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

56 lines
1.3 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
#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-07-05 01:23:12 +00:00
} \
2021-06-17 21:32:24 +00:00
\
__attribute__((__used__)) return_type name args
// Macro To Reset Environmental Variables To Pre-MCPI State
#define RESET_ENVIRONMENTAL_VARIABLE(name) \
{ \
char *original_env_value = getenv("ORIGINAL_" name); \
if (original_env_value != NULL) { \
setenv(name, original_env_value, 1); \
} else { \
unsetenv(name); \
} \
}
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);
#ifdef __cplusplus
2021-06-17 21:32:24 +00:00
}
2022-03-14 23:09:25 +00:00
#endif