65 lines
1.5 KiB
Plaintext
65 lines
1.5 KiB
Plaintext
__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) */
|
|
|
|
fun malloc<T>(size: int): T* {
|
|
var memPtr: T*;
|
|
__if_comp__ __C__ {
|
|
simple_passthrough( size = size, memPtr = memPtr : memPtr = memPtr :) """
|
|
memPtr = malloc(size);
|
|
"""
|
|
}
|
|
return memPtr;
|
|
}
|
|
|
|
fun free<T>(memPtr: T*): void {
|
|
__if_comp__ __C__ {
|
|
simple_passthrough(memPtr = memPtr ::) """
|
|
free(memPtr);
|
|
"""
|
|
}
|
|
}
|
|
|
|
fun sizeof<T>(): int {
|
|
var testObj: T*;
|
|
var result: int;
|
|
__if_comp__ __C__ {
|
|
simple_passthrough(testObj = testObj : result = result:) """
|
|
int result = sizeof(*testObj);
|
|
"""
|
|
}
|
|
return result;
|
|
}
|
|
|
|
fun new<T>(count: int): T* {
|
|
return malloc<T>( sizeof<T>() * count );
|
|
}
|
|
|
|
fun new<T>(): T* {
|
|
return new<T>(1);
|
|
}
|
|
|
|
/* We specilize on the trait Destructable to decide on whether or not the destructor should be called */
|
|
fun delete<T>(toDelete: T*, itemCount: int): void {
|
|
delete<T>(toDelete);
|
|
}
|
|
|
|
/* Calling this with itemCount = 0 allows you to delete destructable objects without calling their destructors. */
|
|
fun delete<T(Destructable)>(toDelete: T*, itemCount: int): void {
|
|
for (var i: int = 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 */
|
|
fun delete<T>(toDelete: T*): void {
|
|
free<T>(toDelete);
|
|
}
|
|
|
|
fun delete<T(Destructable)>(toDelete: T*): void {
|
|
toDelete->destruct();
|
|
free<T>(toDelete);
|
|
}
|