From a307f17f44b461f58441926fcbf87883f17ebe61 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 24 Feb 2019 17:53:46 +0100 Subject: Fixed CHECK_RESULT macro, use scoped allocator Scoped allocator gives us better performance and cleanup code for error cases is much cleaner --- include/misc.h | 2 +- include/parser.h | 4 +++- include/scoped_allocator.h | 27 +++++++++++++++++++++++++++ include/tokenizer.h | 1 - 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 include/scoped_allocator.h (limited to 'include') diff --git a/include/misc.h b/include/misc.h index 633ccfd..9cb2dde 100644 --- a/include/misc.h +++ b/include/misc.h @@ -12,7 +12,7 @@ do { \ #define cleanup_if_error(result) do { if((result) != 0) goto cleanup; } while(0) #if defined(__GNUC__) && __GNUC__ >= 4 -#define CHECK_RESULT __attribute__ ((CHECK_RESULT)) +#define CHECK_RESULT __attribute__ ((warn_unused_result)) #elif defined(_MSC_VER) && _MSC_VER >= 1700 #define CHECK_RESULT _Check_return_ #else diff --git a/include/parser.h b/include/parser.h index d66a096..e90871f 100644 --- a/include/parser.h +++ b/include/parser.h @@ -4,6 +4,7 @@ #include "buffer.h" #include "buffer_view.h" #include "tokenizer.h" +#include "scoped_allocator.h" #define PARSER_OK 0 /* General error */ @@ -11,8 +12,9 @@ #define PARSER_UNEXPECTED_TOKEN -2 typedef struct { - Buffer ast_objects; Tokenizer tokenizer; + ScopedAllocator allocator; + Buffer ast_objects; } Parser; CHECK_RESULT int parser_init(Parser *self); diff --git a/include/scoped_allocator.h b/include/scoped_allocator.h new file mode 100644 index 0000000..1193439 --- /dev/null +++ b/include/scoped_allocator.h @@ -0,0 +1,27 @@ +#ifndef AMALGAM_SCOPED_ALLOCATOR_H +#define AMALGAM_SCOPED_ALLOCATOR_H + +#include "misc.h" +#include "types.h" + +typedef struct ScopedAllocatorNode ScopedAllocatorNode; + +struct ScopedAllocatorNode { + char *data; + usize size; + ScopedAllocatorNode *next; +}; + +typedef struct { + ScopedAllocatorNode head; + ScopedAllocatorNode *current; +} ScopedAllocator; + +CHECK_RESULT int scoped_allocator_node_init(ScopedAllocatorNode *self); +void scoped_allocator_node_deinit(ScopedAllocatorNode *self); + +CHECK_RESULT int scoped_allocator_init(ScopedAllocator *self); +void scoped_allocator_deinit(ScopedAllocator *self); +CHECK_RESULT int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem); + +#endif \ No newline at end of file diff --git a/include/tokenizer.h b/include/tokenizer.h index f572860..9584542 100644 --- a/include/tokenizer.h +++ b/include/tokenizer.h @@ -32,7 +32,6 @@ typedef struct { } Tokenizer; CHECK_RESULT int tokenizer_init(Tokenizer *self, BufferView code); -void tokenizer_deinit(Tokenizer *self); CHECK_RESULT int tokenizer_next(Tokenizer *self, Token *token); CHECK_RESULT int tokenizer_accept(Tokenizer *self, Token expected_token); -- cgit v1.2.3