Bu aralar derleme zamanı olanaklarına kafayı takmış gibi gözüksem de gerçekten ihtiyaç var :)
module main;
import std.stdio;
import core.memory;
int allocate_size = 16;
auto sizeOf(T)() @property{
static if(T.sizeof < 16) return 16;
else{
auto x = T.sizeof;
x--;
x |= x >> 1; // handle 2 bit numbers
x |= x >> 2; // handle 4 bit numbers
x |= x >> 4; // handle 8 bit numbers
x |= x >> 8; // handle 16 bit numbers
x |= x >> 16; // handle 32 bit numbers
x++;
if(x>allocate_size)
allocate_size = x;
return x;
}
}
struct str{
enum size = sizeOf!(typeof(this));
int y;
int z;
final destroy(){
GC.free(&this);
}
}
struct str2{
enum size = sizeOf!(typeof(this));
string z, a,b,c,d,w,e,r,t,y,o,k,l,m;
final destroy(){
GC.free(&this);
}
}
T* smalloc(T, D...)(D datas){
auto v = cast(T*) GC.malloc(str.sizeof);
*v = T(datas);
return v;
}
import std.datetime;
int main(string[] argv){
//GC.disable();
auto p1 = Clock.currTime();
// for (int i; i < 1_000_000; i++){
auto obj = smalloc!str;
auto obj2 = smalloc!str2;
writeln("val: ", GC.sizeOf(obj), "-", obj.size);
obj.destroy();
// }
while(1){}
//writeln(*cast(str*)a);
return 0;
}
Kodlar karışık durabilir ama burada olay şu smalloc! ile uluşturulan structların en büyük boyutu en büyük olanın boyutu bir değişken içerisinde tutulmak isteniyor :)
Zekeriya
--
[ Bu gönderi, http://ddili.org/forum'dan dönüştürülmüştür. ]