first commit
This commit is contained in:
105
include/except.h
Normal file
105
include/except.h
Normal file
@@ -0,0 +1,105 @@
|
||||
#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 assertion_failed;
|
||||
void except_raise(const Exception *e, const char *file, int line);
|
||||
|
||||
#undef assert
|
||||
#ifdef NDEBUG
|
||||
#define assert(e) ((void)0)
|
||||
#else
|
||||
extern void asserted(int e);
|
||||
#define assert(e) ((void)((e)||(RAISE(assertion_failed),0)))
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
extern DWORD 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 == TLS_OUT_OF_INDEXES) \
|
||||
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)
|
||||
#else
|
||||
#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)
|
||||
#endif
|
||||
|
||||
|
||||
#endif // except.h
|
||||
44
include/link.h
Normal file
44
include/link.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#ifndef LINK_INCLUDED
|
||||
#define LINK_INCLUDED
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct Node {
|
||||
uint64_t id; // For Example
|
||||
struct Node *next; // Link
|
||||
};
|
||||
typedef struct Node Node;
|
||||
|
||||
// Syntactic Sugar
|
||||
typedef struct Node * List;
|
||||
typedef struct Node * Stack;
|
||||
typedef struct Node * Queue;
|
||||
|
||||
// Alloc a new node, can RAISE oom Exception.
|
||||
extern struct Node *node_alloc( uint64_t id );
|
||||
|
||||
// Callback function prototype
|
||||
typedef void (*Callback)(Node *);
|
||||
extern void list_for_each(List *l, Callback callback);
|
||||
extern void list_free(List *l);
|
||||
extern void list_print(List *l);
|
||||
// CRUD by Index. 0 is the beginning, -1 will be treated as the end.
|
||||
extern bool list_insert_at(List *l, struct Node *n, int index);
|
||||
extern uint64_t list_delete_at(List *l, int index);
|
||||
extern uint64_t list_search_at(List *l, int index);
|
||||
|
||||
// Read and Delete by ID(or whatever).
|
||||
extern bool list_search_id(List *l, uint64_t id);
|
||||
extern bool list_delete_id(List *l, uint64_t id);
|
||||
|
||||
// Stacks
|
||||
extern bool stack_push(Stack *s, struct Node *n);
|
||||
extern uint64_t stack_pop(Stack *s);
|
||||
|
||||
// Queues
|
||||
extern bool q_enqueue(Queue *q, struct Node *n);
|
||||
extern uint64_t q_dequeue(Queue *q);
|
||||
|
||||
|
||||
#endif
|
||||
31
include/mem.h
Normal file
31
include/mem.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#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)
|
||||
|
||||
|
||||
extern const Exception oom; // Out of memory
|
||||
|
||||
extern void *mem_alloc (size_t nbytes,const char *file, int line);
|
||||
extern void *mem_calloc(size_t count, size_t nbytes, const char *file, int line);
|
||||
extern void mem_free(void *ptr, const char *file, int line);
|
||||
extern void *mem_resize(void *ptr, size_t nbytes, const char *file, int line);
|
||||
|
||||
#define ALLOC(nbytes) mem_alloc((nbytes), __FILE__, __LINE__)
|
||||
#define CALLOC(count, nbytes) mem_calloc((count), (nbytes), __FILE__, __LINE__)
|
||||
#define NEW(p) ((p) = ALLOC((size_t)sizeof *(p)))
|
||||
#define NEW0(p) ((p) = CALLOC(1, (size_t)sizeof *(p)))
|
||||
#define FREE(ptr) ((void)(mem_free((ptr), __FILE__, __LINE__), (ptr) = 0))
|
||||
#define RESIZE(ptr, nbytes) ((ptr) = mem_resize((ptr), (nbytes), __FILE__, __LINE__))
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user