new directory structure + new allocation method

This commit is contained in:
overflowerror 2017-03-11 18:00:23 +01:00
parent c09f20d06b
commit fe1db529a2
22 changed files with 107 additions and 301 deletions

33
Makefile Normal file
View file

@ -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

5
crap.h Normal file
View file

@ -0,0 +1,5 @@
#include "oop.h"
#include "try.h"
#include "memory.h"
#include "exceptions/stdex.h"
#include "misc.h"

6
example.c Normal file
View file

@ -0,0 +1,6 @@
#include "crap.h"
int main(void) {
return 0;
}

View file

@ -1,6 +1,10 @@
#include "exceptions.h" #include "stdex.h"
#include "../memory.h"
#include "../try.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <errno.h>
class_t Exception_class; class_t Exception_class;
void method(Exception, destruct)(Exception_t* this) { 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); add_method(obj, Exception, destruct);
} }
Exception_t* method(Exception, construct)(const char* msg) { 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); populate(Exception)(obj, Exception_class);
obj->msg = msg; obj->msg = msg;
@ -28,10 +34,14 @@ void method(NullPointerException, destruct)(NullPointerException_t* this) {
void method(NullPointerException, populate)(NullPointerException_t* obj, class_t c) { void method(NullPointerException, populate)(NullPointerException_t* obj, class_t c) {
populate(Exception)((Exception_t*) obj, c); populate(Exception)((Exception_t*) obj, c);
obj->super.msg = "Unexpected Null-pointer.";
add_method(obj, NullPointerException, destruct); add_method(obj, NullPointerException, destruct);
} }
NullPointerException_t* method(NullPointerException, construct)() { 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); populate(NullPointerException)(obj, NullPointerException_class);
return obj; return obj;
@ -45,10 +55,17 @@ void method(OutOfMemoryException, destruct)(OutOfMemoryException_t* this) {
void method(OutOfMemoryException, populate)(OutOfMemoryException_t* obj, class_t c) { void method(OutOfMemoryException, populate)(OutOfMemoryException_t* obj, class_t c) {
populate(Exception)((Exception_t*) obj, c); populate(Exception)((Exception_t*) obj, c);
obj->super.msg = "Could not allocate memory.";
add_method(obj, OutOfMemoryException, destruct); add_method(obj, OutOfMemoryException, destruct);
} }
OutOfMemoryException_t* method(OutOfMemoryException, construct)() { 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); populate(OutOfMemoryException)(obj, OutOfMemoryException_class);
return obj; return obj;

View file

@ -1,7 +1,7 @@
#ifndef EXCEPTIONS_H #ifndef STDEX_H
#define EXCEPTIONS_H #define STDEX_H
#include "../../oop/oop.h" #include "../oop.h"
#define Exception construct(Exception) #define Exception construct(Exception)
extern class(Exception, Object_class, NO_INTERFACES, true) { extern class(Exception, Object_class, NO_INTERFACES, true) {

View file

@ -1,6 +1,6 @@
#include "memory.h" #include "memory.h"
#include "../try/try.h" #include "try.h"
#include "../try/exceptions/exceptions.h" #include "exceptions/stdex.h"
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
@ -14,7 +14,7 @@ void allocate_sub(size_t size, void** ptr) { throws(Exception_t);
void* allocate(size_t size) { throws(Exception_t); void* allocate(size_t size) { throws(Exception_t);
void* ptr; void* ptr;
s_(allocate_sub(size, &ptr)); sr_(allocate_sub(size, &ptr), NULL);
return ptr; 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* reallocate(void* old, size_t size) { throws(Exception_t);
void* ptr; void* ptr;
s_(reallocate_sub(size, &ptr, old)); sr_(reallocate_sub(size, &ptr, old), NULL);
return ptr; return ptr;
} }
void* clone(const void* org, size_t size) { throws(Exception_t); void* clone(const void* org, size_t size) { throws(Exception_t);
void* ptr; void* ptr;
s_(allocate_sub(size, &ptr)); sr_(allocate_sub(size, &ptr), NULL);
memcpy(ptr, org, size); memcpy(ptr, org, size);
return ptr; return ptr;
} }

View file

@ -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

View file

@ -1,20 +0,0 @@
#include "memory.h"
#include <stdio.h>
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;
}

View file

@ -1,5 +1,5 @@
#ifndef UNUSED_H #ifndef MISC_H
#define UNUSED_H #define MISC_H
#define UNUSED(v) (v = v); #define UNUSED(v) (v = v);

View file

@ -1,4 +1,7 @@
#include "oop.h" #include "oop.h"
#include "memory.h"
#include "try.h"
#include "exceptions/stdex.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -99,8 +102,8 @@ void method(Object, destruct)(Object_t* obj) {
free(obj); free(obj);
} }
Object_t* method(Object, construct)() { Object_t* method(Object, construct)() { throws(OutOfMemoryException_t);
Object_t* obj = malloc(sizeof(Object_t)); sr_(Object_t* obj = allocate_object(Object_t), NULL);
populate(Object)(obj, Object_class); populate(Object)(obj, Object_class);
return obj; return obj;
} }

View file

View file

@ -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

View file

@ -1,68 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#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;
}

View file

@ -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

View file

@ -1,20 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#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;
}

View file

@ -1,6 +1,7 @@
#include "strbuilder.h" #include "strbuilder.h"
#include "../memory.h"
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
class_t Strbuilder_class; 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) { void method(Strbuilder, add)(Strbuilder_t* this, const char* string) {
this->strings = realloc(this->strings, ++this->nrstrings * sizeof(char*)); throws(OutOfMemoryException_t);
this->strings[this->nrstrings - 1] = malloc(strlen(string) + 1);
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); 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) { void method(Strbuilder, build)(Strbuilder_t* this) {
throws(OutOfMemoryException_t);
size_t length = this->length(this); size_t length = this->length(this);
bool empty = this->string == NULL; bool empty = this->string == NULL;
this->string = realloc(this->string, length + 1); s_(this->string = reallocate(this->string, length + 1));
if (empty) if (empty)
this->string[0] = '\0'; this->string[0] = '\0';
for (int i = 0; i < this->nrstrings; i++) { for (int i = 0; i < this->nrstrings; i++) {
@ -52,12 +57,14 @@ const char* method(Strbuilder, get)(Strbuilder_t* this) {
return this->string; return this->string;
} }
Strbuilder_t* method(Strbuilder, construct)(const char* string) { Strbuilder_t* method(Strbuilder, construct)(const char* string) {
Strbuilder_t* obj = malloc(sizeof(Strbuilder_t)); throws(OutOfMemoryException_t);
sr_(Strbuilder_t* obj = allocate_object(Strbuilder_t), NULL);
populate(Strbuilder)(obj, Strbuilder_class); populate(Strbuilder)(obj, Strbuilder_class);
obj->string = malloc(strlen(string) + 1); sr_(obj->string = allocate(strlen(string) + 1), NULL);
strcpy(obj->string, string); strcpy(obj->string, string);
return obj; return obj;

View file

@ -2,7 +2,7 @@
#define STRBUILDER_H #define STRBUILDER_H
#include <stdlib.h> #include <stdlib.h>
#include "../oop/oop.h" #include "../oop.h"
#define Strbuilder construct(Strbuilder) #define Strbuilder construct(Strbuilder)
extern class(Strbuilder, Object_class, NO_INTERFACES, true) { extern class(Strbuilder, Object_class, NO_INTERFACES, true) {

View file

@ -1,5 +1,5 @@
#include "try.h" #include "try.h"
#include "exceptions/exceptions.h" #include "exceptions/stdex.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -69,7 +69,7 @@ void try_push(try_t id) {
#define BACKTRACE_BUFFER_SIZE 16 #define BACKTRACE_BUFFER_SIZE 16
#define EXIT_BACKTRACE_FAILED 3 #define EXIT_BACKTRACE_FAILED 3
#define EXIT_UNCAUGHT 4 #define EXIT_UNCAUGHT 4
void print_backtrace(FILE* file) { void _print_backtrace(FILE* file, int ignore) {
void* buffer[BACKTRACE_BUFFER_SIZE]; void* buffer[BACKTRACE_BUFFER_SIZE];
char** strings; char** strings;
int entries = backtrace(buffer, BACKTRACE_BUFFER_SIZE); 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; int tmp = (entries < BACKTRACE_BUFFER_SIZE) ? entries : entries - 1;
// from 2, because of print_backtrace and try_throw // 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]); fprintf(file, " at %s\n", strings[i]);
} }
if (tmp < entries) { if (tmp < entries) {
@ -109,7 +109,7 @@ void try_throw(try_t id, void* exception) {
e->msg); e->msg);
#endif #endif
print_backtrace(stderr); _print_backtrace(stderr, 2);
} else { } else {
trys[id].exception = exception; trys[id].exception = exception;
} }

View file

@ -3,8 +3,8 @@
#include <stdbool.h> #include <stdbool.h>
#include "../oop/oop.h" #include "oop.h"
#include "../misc/unused.h" #include "misc.h"
typedef int try_t; 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 endtry while(false); } if (_try_catch) { UNUSED(_try_body); UNUSED(_try_data); }};
#define throw(e) { try_throw(_try_id, (void*) e); return; } #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 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 get_thrown() (_try_result)
#define failed false #define failed false
#define succeeded true #define succeeded true
#define subtry() try_t _try_id = try_pop(); #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 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 tpush() try_push(_try_id)
#define cthrow(e) _try_id = try_pop(); throw(e); #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_push(try_t);
void try_reset(try_t); void try_reset(try_t);
void _print_backtrace(FILE*, int);
#endif #endif

View file

@ -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

View file

@ -1,24 +0,0 @@
#include "try.h"
#include "../oop/oop.h"
#include "exceptions/exceptions.h"
#include <stdio.h>
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;
}