From fe1db529a23f29b160008b7ad11b77abc11161ee Mon Sep 17 00:00:00 2001 From: overflowerror Date: Sat, 11 Mar 2017 18:00:23 +0100 Subject: [PATCH] new directory structure + new allocation method --- Makefile | 33 +++++++++ crap.h | 5 ++ example.c | 6 ++ .../exceptions.c => exceptions/stdex.c | 25 +++++-- .../exceptions.h => exceptions/stdex.h | 6 +- memory/memory.c => memory.c | 10 +-- memory/memory.h => memory.h | 0 memory/Makefile | 38 ----------- memory/example.c | 20 ------ misc/unused.h => misc.h | 4 +- oop/oop.c => oop.c | 7 +- oop/oop.h => oop.h | 0 oop/Makefile | 30 -------- oop/example.c | 68 ------------------- strbuilder/Makefile | 34 ---------- strbuilder/example.c | 20 ------ strbuilder/strbuilder.c | 21 ++++-- strbuilder/strbuilder.h | 2 +- try/try.c => try.c | 8 +-- try/try.h => try.h | 12 ++-- try/Makefile | 35 ---------- try/example.c | 24 ------- 22 files changed, 107 insertions(+), 301 deletions(-) create mode 100644 Makefile create mode 100644 crap.h create mode 100644 example.c rename try/exceptions/exceptions.c => exceptions/stdex.c (68%) rename try/exceptions/exceptions.h => exceptions/stdex.h (94%) rename memory/memory.c => memory.c (80%) rename memory/memory.h => memory.h (100%) delete mode 100644 memory/Makefile delete mode 100644 memory/example.c rename misc/unused.h => misc.h (51%) rename oop/oop.c => oop.c (92%) rename oop/oop.h => oop.h (100%) delete mode 100644 oop/Makefile delete mode 100644 oop/example.c delete mode 100644 strbuilder/Makefile delete mode 100644 strbuilder/example.c rename try/try.c => try.c (93%) rename try/try.h => try.h (79%) delete mode 100644 try/Makefile delete mode 100644 try/example.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..51d4102 --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +# +# Makefile for crap-libs +# +# Author: overflowerror +# + +CC = gcc +DEFS = -D_XOPEN_SOURCE=500 -D_BSD_SOURCE +CFLAGS = -Wall -Wextra -g -std=c99 -DENDEBUG $(DEFS) -fPIC +LDFLAGS = $(DEFS) -rdynamic +LIBFLAGS = -shared $(DEFS) + +.PHONY: all clean + +all: libcrap.so + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +example: example.o memory.o try.o oop.o exceptions/stdex.o + $(CC) $(LDFLAGS) -o $@ $^ + +libcrap.so: memory.o try.o oop.o exceptions/stdex.o + $(CC) $(LIBFLAGS) -o $@ $^ + +example.o: example.c try.h +memory.o: memory.c memory.h oop.h exceptions/stdex.h +try.o: try.c try.h oop.h exceptions/stdex.h +oop.o: oop.h misc.h +exceptions/stdex.o: exceptions/stdex.c exceptions/stdex.h + +clean: + rm -f *.o exceptions/*.o example diff --git a/crap.h b/crap.h new file mode 100644 index 0000000..83a2824 --- /dev/null +++ b/crap.h @@ -0,0 +1,5 @@ +#include "oop.h" +#include "try.h" +#include "memory.h" +#include "exceptions/stdex.h" +#include "misc.h" diff --git a/example.c b/example.c new file mode 100644 index 0000000..2fdf566 --- /dev/null +++ b/example.c @@ -0,0 +1,6 @@ +#include "crap.h" + +int main(void) { + + return 0; +} diff --git a/try/exceptions/exceptions.c b/exceptions/stdex.c similarity index 68% rename from try/exceptions/exceptions.c rename to exceptions/stdex.c index dffc53f..73ee5e0 100644 --- a/try/exceptions/exceptions.c +++ b/exceptions/stdex.c @@ -1,6 +1,10 @@ -#include "exceptions.h" +#include "stdex.h" +#include "../memory.h" +#include "../try.h" #include +#include +#include class_t Exception_class; void method(Exception, destruct)(Exception_t* this) { @@ -12,7 +16,9 @@ void method(Exception, populate)(Exception_t* obj, class_t c) { add_method(obj, Exception, destruct); } Exception_t* method(Exception, construct)(const char* msg) { - Exception_t* obj = malloc(sizeof(Exception_t)); + throws(OutOfMemoryException_t); + + sr_(Exception_t* obj = allocate_object(Exception_t), NULL); populate(Exception)(obj, Exception_class); obj->msg = msg; @@ -28,10 +34,14 @@ void method(NullPointerException, destruct)(NullPointerException_t* this) { void method(NullPointerException, populate)(NullPointerException_t* obj, class_t c) { populate(Exception)((Exception_t*) obj, c); + obj->super.msg = "Unexpected Null-pointer."; + add_method(obj, NullPointerException, destruct); } NullPointerException_t* method(NullPointerException, construct)() { - NullPointerException_t* obj = malloc(sizeof(Exception_t)); + throws(OutOfMemoryException_t); + + sr_(NullPointerException_t* obj = allocate_object(NullPointerException_t), NULL); populate(NullPointerException)(obj, NullPointerException_class); return obj; @@ -45,10 +55,17 @@ void method(OutOfMemoryException, destruct)(OutOfMemoryException_t* this) { void method(OutOfMemoryException, populate)(OutOfMemoryException_t* obj, class_t c) { populate(Exception)((Exception_t*) obj, c); + obj->super.msg = "Could not allocate memory."; + add_method(obj, OutOfMemoryException, destruct); } OutOfMemoryException_t* method(OutOfMemoryException, construct)() { - OutOfMemoryException_t* obj = malloc(sizeof(Exception_t)); + OutOfMemoryException_t* obj = malloc(sizeof(OutOfMemoryException_t)); + if (obj == NULL) { + fprintf(stderr, "\nCritical error! Can not allocate memory for OutOfMemoryException: %s\n", strerror(errno)); + _print_backtrace(stderr, 1); + } + populate(OutOfMemoryException)(obj, OutOfMemoryException_class); return obj; diff --git a/try/exceptions/exceptions.h b/exceptions/stdex.h similarity index 94% rename from try/exceptions/exceptions.h rename to exceptions/stdex.h index 21af941..99bc158 100644 --- a/try/exceptions/exceptions.h +++ b/exceptions/stdex.h @@ -1,7 +1,7 @@ -#ifndef EXCEPTIONS_H -#define EXCEPTIONS_H +#ifndef STDEX_H +#define STDEX_H -#include "../../oop/oop.h" +#include "../oop.h" #define Exception construct(Exception) extern class(Exception, Object_class, NO_INTERFACES, true) { diff --git a/memory/memory.c b/memory.c similarity index 80% rename from memory/memory.c rename to memory.c index 8b98128..1f00183 100644 --- a/memory/memory.c +++ b/memory.c @@ -1,6 +1,6 @@ #include "memory.h" -#include "../try/try.h" -#include "../try/exceptions/exceptions.h" +#include "try.h" +#include "exceptions/stdex.h" #include #include @@ -14,7 +14,7 @@ void allocate_sub(size_t size, void** ptr) { throws(Exception_t); void* allocate(size_t size) { throws(Exception_t); void* ptr; - s_(allocate_sub(size, &ptr)); + sr_(allocate_sub(size, &ptr), NULL); return ptr; } @@ -27,13 +27,13 @@ void reallocate_sub(size_t size, void** ptr, void* old) { throws(Exception_t); void* reallocate(void* old, size_t size) { throws(Exception_t); void* ptr; - s_(reallocate_sub(size, &ptr, old)); + sr_(reallocate_sub(size, &ptr, old), NULL); return ptr; } void* clone(const void* org, size_t size) { throws(Exception_t); void* ptr; - s_(allocate_sub(size, &ptr)); + sr_(allocate_sub(size, &ptr), NULL); memcpy(ptr, org, size); return ptr; } diff --git a/memory/memory.h b/memory.h similarity index 100% rename from memory/memory.h rename to memory.h diff --git a/memory/Makefile b/memory/Makefile deleted file mode 100644 index 20ab839..0000000 --- a/memory/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -# -# Makefile for crap-libs/memory -# -# Author: overflowerror -# - -CC = gcc -DEFS = -D_XOPEN_SOURCE=500 -D_BSD_SOURCE -B "../try/" -B "../oop/" -CFLAGS = -Wall -Wextra -g -std=c99 -pedantic -DENDEBUG $(DEFS) -fPIC -LDFLAGS = $(DEFS) -LIBFLAGS = -shared $(DEFS) - -.PHONY: all clean - -all: example libmemory.so - -%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - -example: example.o memory.o ../try/libtry.so ../oop/liboop.so - $(CC) $(LDFLAGS) -o $@ $^ - -libmemory.so: memory.o ../try/libtry.so ../oop/liboop.so - $(CC) $(LIBFLAGS) -o $@ $^ - -memory.o: memory.c memory.h -example.o: example.c memory.h - -../try/libtry.so: - cd ../try/ && $(MAKE) libtry.so - -../oop/liboop.so: - cd ../oop/ && $(MAKE) liboop.so - -clean: - rm -f *.o example libmemory.so - cd ../try/ && $(MAKE) clean - cd ../oop/ && $(MAKE) clean diff --git a/memory/example.c b/memory/example.c deleted file mode 100644 index 69ec6c5..0000000 --- a/memory/example.c +++ /dev/null @@ -1,20 +0,0 @@ -#include "memory.h" - -#include - -int main(void) { - - const char* str = "Hallo Welt"; - - char* tmp = clone_string(str); - - printf("%s\n", tmp); - - free(tmp); - - tmp = allocate((size_t) 1 << 63); - - free(tmp); - - return 0; -} diff --git a/misc/unused.h b/misc.h similarity index 51% rename from misc/unused.h rename to misc.h index a3f7865..75f017e 100644 --- a/misc/unused.h +++ b/misc.h @@ -1,5 +1,5 @@ -#ifndef UNUSED_H -#define UNUSED_H +#ifndef MISC_H +#define MISC_H #define UNUSED(v) (v = v); diff --git a/oop/oop.c b/oop.c similarity index 92% rename from oop/oop.c rename to oop.c index fd9144b..dfdfbd6 100644 --- a/oop/oop.c +++ b/oop.c @@ -1,4 +1,7 @@ #include "oop.h" +#include "memory.h" +#include "try.h" +#include "exceptions/stdex.h" #include #include @@ -99,8 +102,8 @@ void method(Object, destruct)(Object_t* obj) { free(obj); } -Object_t* method(Object, construct)() { - Object_t* obj = malloc(sizeof(Object_t)); +Object_t* method(Object, construct)() { throws(OutOfMemoryException_t); + sr_(Object_t* obj = allocate_object(Object_t), NULL); populate(Object)(obj, Object_class); return obj; } diff --git a/oop/oop.h b/oop.h similarity index 100% rename from oop/oop.h rename to oop.h diff --git a/oop/Makefile b/oop/Makefile deleted file mode 100644 index 1c99336..0000000 --- a/oop/Makefile +++ /dev/null @@ -1,30 +0,0 @@ -# -# Makefile for crap-libs/oop -# -# Author: overflowerror -# - -CC = gcc -DEFS = -D_XOPEN_SOURCE=500 -D_BSD_SOURCE -CFLAGS = -Wall -Wextra -g -std=c99 -pedantic -DENDEBUG $(DEFS) -fPIC -LDFLAGS = $(DEFS) -LIBFLAGS = -shared $(DEFS) - -.PHONY: all clean - -all: example liboop.so - -%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - -example: example.o oop.o - $(CC) $(LDFLAGS) -o $@ $^ - -liboop.so: oop.o - $(CC) $(LIBFLAGS) -o $@ $^ - -oop.o: oop.c oop.h -example.o: example.c oop.h - -clean: - rm -f *.o example liboop.so diff --git a/oop/example.c b/oop/example.c deleted file mode 100644 index 612d027..0000000 --- a/oop/example.c +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include - -#include "oop.h" - -#define Test construct(Test) - -class(Test, Object_class, (interfaces){1 namely {Cloneable_class}}, true) { - extends(Object_t); - implements(Cloneable); - - const char* string; - - void (*print)(defclass Test*); - void (*destruct)(defclass Test*); -} Test_t; - -void method(Test, print)(Test_t*); -void method(Test, destruct)(Test_t*); -void* method(Test, clone)(void*); -void method(Test, populate)(Test_t*, class_t); -Test_t* method(Test, construct)(const char*); - - -void method(Test, print)(Test_t* this) { - printf("%s\n", this->string); -} -void method(Test, destruct)(Test_t* this) { - this->super.destruct((Object_t*) this); -} - -void* method(Test, clone)(void* obj) { // parameter void* because of interface - Test_t* test = new Test(""); - test->string = ((Test_t*) obj)->string; - return (void*) test; -} - -void method(Test, populate)(Test_t* obj, class_t c) { - populate(Object)((Object_t*) obj, c); - - add_method(obj, Test, destruct); - add_method(obj, Test, print); - add_method(obj, Test, clone); -} - -Test_t* method(Test, construct)(const char* string) { - Test_t* obj = malloc(sizeof(Test_t)); - populate(Test)(obj, Test_class); - - obj->string = string; - - return obj; -} - -int main(void) { - Test_t* obj = new Test("Hallo Welt"); - - class_t c = oop_get_class_from_obj((Object_t*) obj); - printf("class: %s\n", oop_get_class_name(c)); - printf("superclass: %s\n", oop_get_class_name(oop_get_super_class(c))); - printf("instanceof Test: %i\n", instanceof(obj, Test_class)); - printf("instanceof Object: %i\n", instanceof(obj, Object_class)); - printf("instanceof Cloneable: %i\n", instanceof(obj, Cloneable_class)); - - obj->print(obj); - obj->destruct(obj); - return 0; -} diff --git a/strbuilder/Makefile b/strbuilder/Makefile deleted file mode 100644 index 64e3d82..0000000 --- a/strbuilder/Makefile +++ /dev/null @@ -1,34 +0,0 @@ -# -# Makefile for crap-libs/strbuilder -# -# Author: overflowerror -# - -CC = gcc -DEFS = -D_XOPEN_SOURCE=500 -D_BSD_SOURCE -B "../oop/" -CFLAGS = -Wall -Wextra -g -std=c99 -pedantic -DENDEBUG $(DEFS) -fPIC -LDFLAGS = $(DEFS) -LIBFLAGS = -shared $(DEFS) - -.PHONY: all clean - -all: example libstrbuilder.so - -%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - -example: example.o strbuilder.o ../oop/liboop.so - $(CC) $(LDFLAGS) -o $@ $^ - -../oop/liboop.so: - cd ../oop/ && $(MAKE) liboop.so - -libstrbuilder.so: strbuilder.o - $(CC) $(LIBFLAGS) -o $@ $^ - -strbuilder.o: strbuilder.c strbuilder.h -example.o: example.c strbuilder.h - -clean: - rm -f *.o example libstrbuilder.so - cd ../oop/ && $(MAKE) clean diff --git a/strbuilder/example.c b/strbuilder/example.c deleted file mode 100644 index f588617..0000000 --- a/strbuilder/example.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include - -#include "../oop/oop.h" -#include "strbuilder.h" - -int main(void) { - Strbuilder_t* builder = new Strbuilder("Hallo"); - - builder->add(builder, " Welt"); - builder->add(builder, "!"); - builder->build(builder); - - const char* string = builder->get(builder); - printf("%s\n", string); - - builder->destruct(builder); - - return 0; -} diff --git a/strbuilder/strbuilder.c b/strbuilder/strbuilder.c index c5b37cb..5e5c8a7 100644 --- a/strbuilder/strbuilder.c +++ b/strbuilder/strbuilder.c @@ -1,6 +1,7 @@ #include "strbuilder.h" +#include "../memory.h" + #include -#include #include class_t Strbuilder_class; @@ -20,8 +21,10 @@ void method(Strbuilder, destruct)(Strbuilder_t* this) { } void method(Strbuilder, add)(Strbuilder_t* this, const char* string) { - this->strings = realloc(this->strings, ++this->nrstrings * sizeof(char*)); - this->strings[this->nrstrings - 1] = malloc(strlen(string) + 1); + throws(OutOfMemoryException_t); + + s_(this->strings = reallocate(this->strings, ++this->nrstrings * sizeof(char*))); + s_(this->strings[this->nrstrings - 1] = allocate(strlen(string) + 1)); strcpy(this->strings[this->nrstrings - 1], string); } @@ -36,9 +39,11 @@ size_t method(Strbuilder, length)(Strbuilder_t* this) { } void method(Strbuilder, build)(Strbuilder_t* this) { + throws(OutOfMemoryException_t); + size_t length = this->length(this); bool empty = this->string == NULL; - this->string = realloc(this->string, length + 1); + s_(this->string = reallocate(this->string, length + 1)); if (empty) this->string[0] = '\0'; for (int i = 0; i < this->nrstrings; i++) { @@ -52,12 +57,14 @@ const char* method(Strbuilder, get)(Strbuilder_t* this) { return this->string; } -Strbuilder_t* method(Strbuilder, construct)(const char* string) { - Strbuilder_t* obj = malloc(sizeof(Strbuilder_t)); +Strbuilder_t* method(Strbuilder, construct)(const char* string) { + throws(OutOfMemoryException_t); + + sr_(Strbuilder_t* obj = allocate_object(Strbuilder_t), NULL); populate(Strbuilder)(obj, Strbuilder_class); - obj->string = malloc(strlen(string) + 1); + sr_(obj->string = allocate(strlen(string) + 1), NULL); strcpy(obj->string, string); return obj; diff --git a/strbuilder/strbuilder.h b/strbuilder/strbuilder.h index 6d05e88..d3a8d74 100644 --- a/strbuilder/strbuilder.h +++ b/strbuilder/strbuilder.h @@ -2,7 +2,7 @@ #define STRBUILDER_H #include -#include "../oop/oop.h" +#include "../oop.h" #define Strbuilder construct(Strbuilder) extern class(Strbuilder, Object_class, NO_INTERFACES, true) { diff --git a/try/try.c b/try.c similarity index 93% rename from try/try.c rename to try.c index 0638005..6ee89d3 100644 --- a/try/try.c +++ b/try.c @@ -1,5 +1,5 @@ #include "try.h" -#include "exceptions/exceptions.h" +#include "exceptions/stdex.h" #include #include @@ -69,7 +69,7 @@ void try_push(try_t id) { #define BACKTRACE_BUFFER_SIZE 16 #define EXIT_BACKTRACE_FAILED 3 #define EXIT_UNCAUGHT 4 -void print_backtrace(FILE* file) { +void _print_backtrace(FILE* file, int ignore) { void* buffer[BACKTRACE_BUFFER_SIZE]; char** strings; int entries = backtrace(buffer, BACKTRACE_BUFFER_SIZE); @@ -83,7 +83,7 @@ void print_backtrace(FILE* file) { int tmp = (entries < BACKTRACE_BUFFER_SIZE) ? entries : entries - 1; // from 2, because of print_backtrace and try_throw - for (int i = 2; i < tmp; i++) { + for (int i = ignore; i < tmp; i++) { fprintf(file, " at %s\n", strings[i]); } if (tmp < entries) { @@ -109,7 +109,7 @@ void try_throw(try_t id, void* exception) { e->msg); #endif - print_backtrace(stderr); + _print_backtrace(stderr, 2); } else { trys[id].exception = exception; } diff --git a/try/try.h b/try.h similarity index 79% rename from try/try.h rename to try.h index a3695c7..f66be6e 100644 --- a/try/try.h +++ b/try.h @@ -3,8 +3,8 @@ #include -#include "../oop/oop.h" -#include "../misc/unused.h" +#include "oop.h" +#include "misc.h" typedef int try_t; @@ -17,14 +17,16 @@ typedef int try_t; #define endtry while(false); } if (_try_catch) { UNUSED(_try_body); UNUSED(_try_data); }}; #define throw(e) { try_throw(_try_id, (void*) e); return; } +#define throwr(e, r) { try_throw(_try_id, (void*) e); return r; } #define until(condition) ; do { try_reset(_try_id); _try_f(); } while(try_has_catch(_try_id) == condition); void* _try_result = try_catch(_try_id); try_remove(_try_id); if(true) { do #define get_thrown() (_try_result) #define failed false #define succeeded true -#define subtry() try_t _try_id = try_pop(); -#define s_(f) try_push(_try_id); f; if(_try_has_catch(_try_id)) return; +#define subtry() try_t _try_id = try_pop(); UNUSED(_try_id); +#define s_(f) try_push(_try_id); f; if(try_has_catch(_try_id)) return; +#define sr_(f, r) try_push(_try_id); f; if(try_has_catch(_try_id)) return r; #define tpush() try_push(_try_id) #define cthrow(e) _try_id = try_pop(); throw(e); @@ -40,4 +42,6 @@ try_t try_pop(void); void try_push(try_t); void try_reset(try_t); +void _print_backtrace(FILE*, int); + #endif diff --git a/try/Makefile b/try/Makefile deleted file mode 100644 index 63fc375..0000000 --- a/try/Makefile +++ /dev/null @@ -1,35 +0,0 @@ -# -# Makefile for crap-libs/try -# -# Author: overflowerror -# - -CC = gcc -DEFS = -D_XOPEN_SOURCE=500 -D_BSD_SOURCE -B "../oop/" -CFLAGS = -Wall -Wextra -g -std=c99 -DENDEBUG $(DEFS) -fPIC -LDFLAGS = $(DEFS) -rdynamic -LIBFLAGS = -shared $(DEFS) - -.PHONY: all clean - -all: example libtry.so - -%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - -example: example.o try.o exceptions/exceptions.o ../oop/liboop.so - $(CC) $(LDFLAGS) -o $@ $^ - -libtry.so: try.o exceptions/exceptions.o ../oop/liboop.so - $(CC) $(LIBFLAGS) -o $@ $^ - -try.o: try.c try.h -example.o: example.c try.h -exceptions/exceptions.o: exceptions/exceptions.c exceptions/exceptions.h - -../oop/liboop.so: - cd ../oop/ && $(MAKE) liboop.so - -clean: - rm -f *.o example libtry.so - cd ../oop/ && $(MAKE) clean diff --git a/try/example.c b/try/example.c deleted file mode 100644 index fa7b157..0000000 --- a/try/example.c +++ /dev/null @@ -1,24 +0,0 @@ -#include "try.h" - -#include "../oop/oop.h" -#include "exceptions/exceptions.h" - -#include - -void function(void) { throws(Exception_class) - - throw(new Exception("This is an uncaught exception.")); -} - -int main(void) { - try { - throw(new Exception("This is a caught exception.")); - } catch(Exception_class, Exception_t* e) { - printf("Caught: %s\n", e->msg); - e->destruct(e); - } endtry; - - function(); - - return 0; -}