From 2052814783c6f8cacfa997ef5b29fa50b10ef068 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Fri, 10 Mar 2017 23:17:53 +0100 Subject: [PATCH] memory management stuff --- memory/Makefile | 38 ++++++++++++++++++++++++++++++++++++++ memory/example.c | 20 ++++++++++++++++++++ memory/memory.c | 39 +++++++++++++++++++++++++++++++++++++++ memory/memory.h | 14 ++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 memory/Makefile create mode 100644 memory/example.c create mode 100644 memory/memory.c create mode 100644 memory/memory.h diff --git a/memory/Makefile b/memory/Makefile new file mode 100644 index 0000000..445d1e7 --- /dev/null +++ b/memory/Makefile @@ -0,0 +1,38 @@ +# +# 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 memory.so + cd ../try/ && $(MAKE) clean + cd ../oop/ && $(MAKE) clean diff --git a/memory/example.c b/memory/example.c new file mode 100644 index 0000000..69ec6c5 --- /dev/null +++ b/memory/example.c @@ -0,0 +1,20 @@ +#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/memory/memory.c b/memory/memory.c new file mode 100644 index 0000000..0e3107e --- /dev/null +++ b/memory/memory.c @@ -0,0 +1,39 @@ +#include "memory.h" +#include "../try/try.h" +#include "../try/exceptions/exceptions.h" + +#include +#include + +void allocate_sub(size_t size, void** ptr) { throws(exception_t); + *ptr = malloc(size); + if (*ptr == NULL) { + throw(new exception(strerror(errno))); + } +} + +void* allocate(size_t size) { throws(exception_t); + void* ptr; + s_(allocate_sub(size, &ptr)); + return ptr; +} + +void reallocate_sub(size_t size, void** ptr, void* old) { throws(exception_t); + *ptr = realloc(old, size); + if (*ptr == NULL) { + throw(new exception(strerror(errno))); + } +} + +void* reallocate(void* old, size_t size) { throws(exception_t); + void* ptr; + s_(reallocate_sub(size, &ptr, old)); + return ptr; +} + +void* clone(const void* org, size_t size) { throws(exception_t); + void* ptr; + trycall allocate_sub(size, &ptr); + memcpy(ptr, org, size); + return ptr; +} diff --git a/memory/memory.h b/memory/memory.h new file mode 100644 index 0000000..fd65769 --- /dev/null +++ b/memory/memory.h @@ -0,0 +1,14 @@ +#ifndef MEMORY_H +#define MEMORY_H + +#include +#include + +#define clone_string(s) clone((void*) s, strlen(s) + 1) +#define clone_obj(obj, class) clone((void*) obj, sizeof(class)) + +void* allocate(size_t); +void* reallocate(void*, size_t); +void* clone(const void*, size_t); + +#endif