Files
kraken/stdlib/mem.krak

195 lines
4.5 KiB
Plaintext
Raw Normal View History

import set
import map
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) */
fun null<T>(): *T {
__if_comp__ __C__ {
simple_passthrough(::) """
return (void*)0;
"""
}
}
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__ {
2015-04-04 01:32:40 -04:00
simple_passthrough(memPtr = memPtr ::) """
free(memPtr);
"""
}
}
fun sizeof<T>(): int {
var testObj: *T;
var result: int;
__if_comp__ __C__ {
2015-04-04 01:32:40 -04:00
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 Object 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(Object)>(toDelete: *T, itemCount: int): void {
// start at one because the actual delete will call the destructor of the first one as it
// finishes the pointer
for (var i: int = 0; i < itemCount; i++;)
toDelete[i].destruct();
free<T>(toDelete);
//delete<T>(toDelete);
}
/* We specilize on the trait Object to decide on whether or not the destructor should be called */
fun delete<T>(toDelete: *T): void {
2015-04-04 01:32:40 -04:00
free<T>(toDelete);
}
fun delete<T(Object)>(toDelete: *T): void {
toDelete->destruct();
2015-04-04 01:32:40 -04:00
free<T>(toDelete);
}
// a wrapper for construct if it has the Object trait
fun maybe_construct<T>(it:*T):*T {
return it
}
fun maybe_construct<T(Object)>(it:*T):*T {
return it->construct()
}
// a wrapper for copy constructing if it has the Object trait
fun maybe_copy_construct<T>(to:*T, from:*T):void {
*to = *from
}
fun maybe_copy_construct<T(Object)>(to:*T, from:*T):void {
to->copy_construct(from)
}
// a wrapper for destruct if it has the Object trait
fun maybe_destruct<T>(it:*T):void {}
fun maybe_destruct<T(Object)>(it:*T):void {
it->destruct()
}
// a function that allows the safe deletion of recursive and complicated data structures
fun safe_recursive_delete<T>(first: *T, addingFunc: fun(*T): set::set<*T>) {
var toDelete = set::set<*T>()
var next = set::set(first)
while (toDelete != next) {
toDelete = next
toDelete.for_each( fun(it: *T) next.add(addingFunc(it)); )
}
toDelete.for_each( fun(it: *T) delete(it); )
}
// a function that allows the safe cloning of recursive and complicated data structures
// cloneing func is the func that does the cloning, it takes in a recursive clone function and
// a register clone function
fun safe_recursive_clone<T>(first: *T, cloningFunc: fun(*T, fun(*T):*T, fun(*T):void): void): *T {
var rec_map = map::map<*T,*T>()
// can't do type infrence if you need the type inside the expression...
var rec_it: fun(*T):*T = fun(it: *T): *T {
if (!rec_map.contains_key(it))
cloningFunc(it, rec_it, fun(cloned: *T) { rec_map[it] = cloned; })
return rec_map[it]
}
return rec_it(first)
}
2016-01-11 19:25:09 -05:00
obj shared_ptr<T> (Object){
var data: *T;
var refCount: int;
fun construct(): *shared_ptr<T> {
data = 0;
refCount = 1;
return this;
}
fun construct(newPtr: *T): *shared_ptr<T> {
data = newPtr;
refCount = 1;
return this;
}
fun construct(newPtr: ref shared_ptr<T>): *shared_ptr<T> {
data = newPtr.data;
refCount = newPtr.refCount;
refCount++;
return this;
}
fun destruct(): void {
if(refCount == 1){
delete(data,1);
refCount--;
}
}
fun operator*(): ref T {
return *data;
}
fun operator->(): *T {
return data;
}
fun operator=(newPtr: ref shared_ptr<T>): ref shared_ptr<T> {
if(this != &newPtr){
if(refCount == 1){
delete(data,1);
refCount--;
}
//use copy constructor here???
data = newPtr.data;
refCount = newPtr.refCount;
refCount++;
}//end self-assignment check
return *this;
}
2016-01-11 19:52:11 -05:00
fun operator=(newPtr: ref *T): ref shared_ptr<T> {
data = newPtr;
refCount = 1;
delete(newPtr,1);
return *this;
}
2016-01-11 19:25:09 -05:00
}; //end shared_ptr class