crap-libs/memory.c

40 lines
848 B
C
Raw 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(Exception_t);
2017-03-10 22:17:53 +00:00
*ptr = malloc(size);
if (*ptr == NULL) {
throw(new Exception(strerror(errno)));
2017-03-10 22:17:53 +00:00
}
}
void* allocate(size_t size) { throws(Exception_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
return ptr;
}
void reallocate_sub(size_t size, void** ptr, void* old) { throws(Exception_t);
2017-03-10 22:17:53 +00:00
*ptr = realloc(old, size);
if (*ptr == NULL) {
throw(new Exception(strerror(errno)));
2017-03-10 22:17:53 +00:00
}
}
void* reallocate(void* old, size_t size) { throws(Exception_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(Exception_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;
}