Initial commit
This commit is contained in:
103
include/except.h
Normal file
103
include/except.h
Normal file
@@ -0,0 +1,103 @@
|
||||
#ifndef EXCEPT_INCLUDED
|
||||
#define EXCEPT_INCLUDED
|
||||
|
||||
#include <setjmp.h>
|
||||
struct Exception {
|
||||
const char *reason;
|
||||
};
|
||||
typedef struct Exception Exception;
|
||||
|
||||
struct Except_Frame {
|
||||
struct Except_Frame *prev;
|
||||
jmp_buf env;
|
||||
const char *file;
|
||||
int line;
|
||||
const struct Exception *exception;
|
||||
};
|
||||
typedef struct Except_Frame Except_Frame;
|
||||
|
||||
enum {
|
||||
EXCEPT_STATE_ENTERED=0,
|
||||
EXCEPT_STATE_RAISED,
|
||||
EXCEPT_STATE_HANDLED,
|
||||
EXCEPT_STATE_FINALIZED,
|
||||
EXCEPT_STATE_COUNT };
|
||||
|
||||
extern struct Except_Frame *except_stack;
|
||||
extern const struct Exception assert_failed;
|
||||
void except_raise(const struct Exception *e, const char *file, int line);
|
||||
|
||||
// Try Catch MACROS - LINUX
|
||||
#define RAISE(e) except_raise(&(e), __FILE__, __LINE__)
|
||||
#define RERAISE except_raise(except_frame.exception, \
|
||||
except_frame.file, except_frame.line)
|
||||
#define RETURN switch (except_stack = except_stack->prev,0) default: return
|
||||
#define TRY do { \
|
||||
volatile int except_flag; \
|
||||
Except_Frame except_frame; \
|
||||
except_frame.prev = except_stack; \
|
||||
except_stack = &except_frame; \
|
||||
except_flag = setjmp(except_frame.env); \
|
||||
if (except_flag == EXCEPT_STATE_ENTERED) {
|
||||
#define EXCEPT(e) \
|
||||
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
|
||||
} else if (except_frame.exception == &(e)) { \
|
||||
except_flag = EXCEPT_STATE_HANDLED;
|
||||
#define ELSE \
|
||||
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
|
||||
} else { \
|
||||
except_flag = EXCEPT_STATE_HANDLED;
|
||||
#define FINALLY \
|
||||
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
|
||||
} { \
|
||||
if (except_flag == EXCEPT_STATE_ENTERED) \
|
||||
except_flag = EXCEPT_STATE_FINALIZED;
|
||||
#define END_TRY \
|
||||
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
|
||||
} if (except_flag == EXCEPT_STATE_RAISED) RERAISE; \
|
||||
} while (0)
|
||||
|
||||
|
||||
// Try Catch MACROS - WIN32
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
|
||||
extern int except_index;
|
||||
extern void except_init(void);
|
||||
extern void except_push(Except_Frame *fp);
|
||||
extern void except_pop(void);
|
||||
|
||||
#define RAISE(e) except_raise(&(e), __FILE__, __LINE__)
|
||||
#define RERAISE except_raise(except_frame.exception, \
|
||||
except_frame.file, except_frame.line)
|
||||
#define RETURN switch (except_pop(),0) default: return
|
||||
#define TRY do { \
|
||||
volatile int except_flag; \
|
||||
Except_Frame except_frame; \
|
||||
if (except_index == -1) \
|
||||
except_init(); \
|
||||
except_push(&except_frame); \
|
||||
except_flag = setjmp(except_frame.env); \
|
||||
if (except_flag == EXCEPT_STATE_ENTERED) {
|
||||
#define EXCEPT(e) \
|
||||
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
|
||||
} else if (except_frame.exception == &(e)) { \
|
||||
except_flag = EXCEPT_STATE_HANDLED;
|
||||
#define ELSE \
|
||||
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
|
||||
} else { \
|
||||
except_flag = EXCEPT_STATE_HANDLED;
|
||||
#define FINALLY \
|
||||
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
|
||||
} { \
|
||||
if (except_flag == EXCEPT_STATE_ENTERED) \
|
||||
except_flag = EXCEPT_STATE_FINALIZED;
|
||||
#define END_TRY \
|
||||
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
|
||||
} if (except_flag == EXCEPT_STATE_RAISED) RERAISE; \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif // except.h
|
||||
33
include/mem.h
Normal file
33
include/mem.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef MEM_INCLUDED
|
||||
#define MEM_INCLUDED
|
||||
|
||||
#include "except.h" // Exceptions
|
||||
#include <stddef.h> // size_t
|
||||
|
||||
/* General Macros*/
|
||||
#define MEM_KB(x) ((size_t)(x) * 1024ULL)
|
||||
#define MEM_MB(x) ((size_t)(x) * 1024ULL * 1024ULL)
|
||||
#define MEM_GB(x) ((size_t)(x) * 1024ULL * 1024ULL * 1024ULL)
|
||||
#define MEM_SIZE(x) (ptrdiff_t)sizeof(x)
|
||||
#define MEM_COUNT(a) (MEM_SIZE(a) / MEM_SIZE(*(a)))
|
||||
#define MEM_LEN(s) (MEM_COUNT(s) - 1)
|
||||
|
||||
/* Default behavior is to throw exceptions and zero out memory*/
|
||||
enum MemFlags {
|
||||
NOZERO = 1 << 0, /* 0001 */
|
||||
SOFT_FAIL = 1 << 1, /* 0010 */
|
||||
HARD_FAIL = 1 << 2, /* 0100 */
|
||||
};
|
||||
|
||||
extern const Exception OOM; // Out of memory
|
||||
|
||||
extern void *mem_alloc (int flags, size_t nbytes, const char *file, int line);
|
||||
extern void mem_free(void *ptr, const char *file, int line);
|
||||
extern void *mem_resize(int flags, void *ptr, size_t nbytes, const char *file, int line);
|
||||
|
||||
#define ALLOC(flags, nbytes) mem_alloc((flags), (nbytes), __FILE__, __LINE__)
|
||||
#define FREE(ptr) ((void)(mem_free((ptr), __FILE__, __LINE__), (ptr) = 0))
|
||||
#define RESIZE(flags, ptr, nbytes) ((ptr) = mem_resize((flags), (ptr), \
|
||||
(nbytes), __FILE__, __LINE__))
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user