mirror of
https://github.com/sigmasternchen/crap-libs
synced 2025-03-15 07:38:56 +00:00
memory management stuff
This commit is contained in:
parent
c4ef8a726f
commit
2052814783
4 changed files with 111 additions and 0 deletions
38
memory/Makefile
Normal file
38
memory/Makefile
Normal file
|
@ -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
|
20
memory/example.c
Normal file
20
memory/example.c
Normal file
|
@ -0,0 +1,20 @@
|
|||
#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;
|
||||
}
|
39
memory/memory.c
Normal file
39
memory/memory.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include "memory.h"
|
||||
#include "../try/try.h"
|
||||
#include "../try/exceptions/exceptions.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
14
memory/memory.h
Normal file
14
memory/memory.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef MEMORY_H
|
||||
#define MEMORY_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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
|
Loading…
Reference in a new issue