Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // note that you probably shouldn't slice the resulting array or assign new objects to it
- T[] myNewArr(T, Args...) (size_t len, Args args) {
- // get class size of class object in bytes
- size_t clSize = __traits(classInstanceSize, T);
- // allocate memory for the objects - maybe even with GC malloc
- void* tmp = malloc( clSize * len );
- if(!tmp) throw new Exception("no memory");
- T[] ret; // remember - this just contains references
- for(size_t i=0;i<len;++i) {
- size_t pos = i*clSize;
- void[] objMem = tmp[pos..pos+clSize];
- // add reference to new object to array
- ret ~= emplace!(T, Args)(objMem, args);
- }
- return ret;
- }
- void myDeleteArr(T)(ref T[] arr) {
- void *adr = cast(void*)arr[0];
- foreach(elem; arr)
- clear(elem);
- // core.stdc.stdlib.free(adr);
- arr=null; // so the data will be freed
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement