Files
kraken/stdlib/mem.krak

65 lines
1.6 KiB
Plaintext
Raw Normal View History

2015-04-04 01:32:40 -04:00
__if_comp__ __C__ simple_passthrough """
#include <stdlib.h>
"""
/* we have a template versions so we don't have to cast (because we don't have that yet) */
template <T> |T*| malloc(|int| size) {
|T*| memPtr = 0;
__if_comp__ __C__ {
2015-04-04 01:32:40 -04:00
simple_passthrough( size = size : memPtr = memPtr :) """
memPtr = malloc(size);
"""
}
return memPtr;
}
template <T> |void| free(|T*| memPtr) {
__if_comp__ __C__ {
2015-04-04 01:32:40 -04:00
simple_passthrough(memPtr = memPtr ::) """
free(memPtr);
"""
}
}
template <T> |int| sizeof() {
|int| result = 0;
|T| testObj;
__if_comp__ __C__ {
2015-04-04 01:32:40 -04:00
simple_passthrough(testObj = testObj : result = result:) """
result = sizeof(testObj);
"""
}
return result;
}
template <T> |T*| new(|int| count) {
return malloc<T>( sizeof<T>() * count );
}
template <T> |T*| new() {
return new<T>(1);
}
/* We specilize on the trait Destructable to decide on whether or not the destructor should be called */
template <T> |void| delete(|T*| toDelete, |int| itemCount) {
delete<T>(toDelete);
}
/* Calling this with itemCount = 0 allows you to delete destructable objects without calling their destructors. */
template <T(Destructable)> |void| delete(|T*| toDelete, |int| itemCount) {
for (|int| i = 0; i < itemCount; i++;)
toDelete[i].destruct();
delete<T>(toDelete);
}
/* We specilize on the trait Destructable to decide on whether or not the destructor should be called */
template <T> |void| delete(|T*| toDelete) {
2015-04-04 01:32:40 -04:00
free<T>(toDelete);
}
template <T(Destructable)> |void| delete(|T*| toDelete) {
toDelete->destruct();
2015-04-04 01:32:40 -04:00
free<T>(toDelete);
}