2017-03-10 22:17:53 +00:00
|
|
|
#include "memory.h"
|
2017-03-11 17:00:23 +00:00
|
|
|
#include "try.h"
|
|
|
|
#include "exceptions/stdex.h"
|
2017-03-10 22:17:53 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-03-12 17:58:29 +00:00
|
|
|
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-03-12 17:58:29 +00:00
|
|
|
throw(new OutOfMemoryException());
|
2017-03-10 22:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 17:58:29 +00:00
|
|
|
void* allocate_o(class_t c, size_t size) { throws(OutOfMemoryException_t, ClassNotInstanceableException_t);
|
|
|
|
if (!oop_is_instanceable(c))
|
|
|
|
throwr(new ClassNotInstanceableException(oop_get_class_name(c)), NULL);
|
2017-03-10 22:17:53 +00:00
|
|
|
void* ptr;
|
2017-03-11 17:00:23 +00:00
|
|
|
sr_(allocate_sub(size, &ptr), NULL);
|
2017-03-10 22:17:53 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2017-03-12 17:58:29 +00:00
|
|
|
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-03-12 17:58:29 +00:00
|
|
|
throw(new OutOfMemoryException());
|
2017-03-10 22:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 17:58:29 +00:00
|
|
|
void* reallocate(void* old, size_t size) { throws(OutOfMemoryException_t);
|
2017-03-10 22:17:53 +00:00
|
|
|
void* ptr;
|
2017-03-11 17:00:23 +00:00
|
|
|
sr_(reallocate_sub(size, &ptr, old), NULL);
|
2017-03-10 22:17:53 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2017-03-12 17:58:29 +00:00
|
|
|
void* clone(const void* org, size_t size) { throws(OutOfMemoryException_t);
|
2017-03-10 22:17:53 +00:00
|
|
|
void* ptr;
|
2017-03-11 17:00:23 +00:00
|
|
|
sr_(allocate_sub(size, &ptr), NULL);
|
2017-03-10 22:17:53 +00:00
|
|
|
memcpy(ptr, org, size);
|
|
|
|
return ptr;
|
|
|
|
}
|