crap-libs/memory.c

48 lines
1.2 KiB
C
Raw Permalink Normal View History

2017-03-10 22:17:53 +00:00
#include "memory.h"
#include "try.h"
#include "exceptions/stdex.h"
2017-03-10 22:17:53 +00:00
#include <errno.h>
#include <string.h>
void allocate_sub(size_t size, void** ptr) { throws(OutOfMemoryException_t);
2017-03-10 22:17:53 +00:00
*ptr = malloc(size);
if (*ptr == NULL) {
2017-04-14 22:12:54 +00:00
throw(new (OutOfMemoryException)());
2017-03-10 22:17:53 +00:00
}
}
void* allocate_o(class_t c, size_t size) { throws(OutOfMemoryException_t, ClassNotInstanceableException_t);
if (!oop_is_instanceable(c))
2017-04-14 22:12:54 +00:00
throwr(new (ClassNotInstanceableException)(oop_get_class_name(c)), NULL);
2017-03-10 22:17:53 +00:00
void* ptr;
sr_(allocate_sub(size, &ptr), NULL);
2017-03-10 22:17:53 +00:00
return ptr;
}
void* allocate(size_t size) { throws(OutOfMemoryException_t);
void* ptr;
sr_(allocate_sub(size, &ptr), NULL);
return ptr;
}
void reallocate_sub(size_t size, void** ptr, void* old) { throws(OutOfMemoryException_t);
2017-03-10 22:17:53 +00:00
*ptr = realloc(old, size);
if (*ptr == NULL) {
2017-04-14 22:12:54 +00:00
throw(new (OutOfMemoryException)());
2017-03-10 22:17:53 +00:00
}
}
void* reallocate(void* old, size_t size) { throws(OutOfMemoryException_t);
2017-03-10 22:17:53 +00:00
void* ptr;
sr_(reallocate_sub(size, &ptr, old), NULL);
2017-03-10 22:17:53 +00:00
return ptr;
}
void* clone(const void* org, size_t size) { throws(OutOfMemoryException_t);
2017-03-10 22:17:53 +00:00
void* ptr;
sr_(allocate_sub(size, &ptr), NULL);
2017-03-10 22:17:53 +00:00
memcpy(ptr, org, size);
return ptr;
}