www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is there any way to make this code work?

reply z <z gg.com> writes:
Hi,

I'm reading http://digitalmars.com/d/2.0/memory.html

and try to combine "Free Lists" and "Explicit Class Instance Allocation".

So instead of:

void test()
{
    Foo f = Foo.allocate();
    ...
    Foo.deallocate(f);
}

I can just write as normal:

void test()
{
    Foo f = new Foo();
    ...
    delete Foo;
}

But the simple code I wrote seg faults. Is this a compiler bug? or I'm doing
something wrong? or can this thing be done at all?

Thanks.
begin 644 custom.d
M:6UP;W)T('-T9"YS=&1I;SL*:6UP;W)T('-T9"YC+G-T9&QI8CL*:6UP;W)T
M('-T9"YO=71O9FUE;6]R>3L*:6UP;W)T('-T9"YG8SL*"F-L87-S($)A<B![
M"B` ("!P=6)L:6, =F]I9"!F=6XH*2!["B` ("` ('!R:6YT9B B0F%R7&XB
M*3L*("` ('T*?0H*8VQA<W, 1F]O(#H 0F%R('L*("` ('-T<FEN9R!N86UE
M.PH*("` ('1H:7,H*2!["B` ("` ("` <')I;G1F*")M>2!C=&]R7&XB*3L*
M("` ('T*"B` ("!^=&AI<R I('L*("` ("` ("!P<FEN=&8H(FUY(&1T;W)<
M;B(I.PH ("` ("` ("\O('-U<'!O<V4 22!D;VXG="!D;R!A;GET:&EN9R!H
M97)E"B` ("!]" H ("` <'5B;&EC(&]V97)R:61E('9O:60 9G5N*"D >PH 
M("` ("` ('=R:71E;&XH;F%M92D["B` ("!]" H ("` ;F5W*'-I>F5?="!S
M>BD*("` ('L*("` ("` ("!P<FEN=&8H(FUY(&YE=UQN(BD["B` ("` ("` 
M1F]O(&8[" EI9B`H9G)E96QI<W0I" E[("` 9B`](&9R965L:7-T.PH)("` 
M(&9R965L:7-T(#T 9BYN97AT.PH)("` (')E='5R;B!C87-T*'9O:60J*68[
M("` +R\ 22!K;F]W('1H870 8W1O<B!W:6QL(&)E(&-A;&QE9"!A9V%I;B!O

M*B!P.PH)<"`]('-T9"YC+G-T9&QI8BYM86QL;V,H<WHI.PH):68 *"%P*0H)
M("` ('1H<F]W(&YE=R!/=71/9DUE;6]R>45X8V5P=&EO;B I.PH)+R]S=&0N
M9V,N861D4F%N9V4H<"P <"`K('-Z*3L*"7)E='5R;B!P.PH ("` ?0H*("` 
M(&1E;&5T92AV;VED*B!P*0H ("` >PH ("` ("` ('!R:6YT9B B;7D 9&5L
M7&XB*3L*("` ("` ("`O+R!S=7!P;W-E($D :G5S="!S879E(&ET(&9O<B!R
M975S92P :6YS=&5A9"!O9B!F<F5E(&UE;6]R>0H ("` ("` ($9O;R!F(#T 
M8V%S="A&;V\I<#L*("` ("` ("!D96%L;&]C871E*&8I.PH ("` ("` ('!R
M:6YT9B B<V%V960A7&XB*3L*("` ("` ("`O+R!T<GD =&\ 8V%L;"!S;VUE
M(&UE=&AO9"P <V5E(&EF(&ET('-T:6QL('=O<FMS. H ("` ("` (&8N9G5N
M*"D[("`O+R!396=M96YT871I;VX 9F%U;'0 :&5R92X 27, =&AE<F4 86YY
M('=A>2!T;R!M86ME('1H:7, =V]R:S\*("` ("` ("`O+R!)(')U;B!T:')U
M('1H92!D96)U9V=E<BP 86YD(&9O=6YD(&8G<R!A='1R:71U8F4 :7, ;F]T
M(&-H86YG960N"B` ("` ("` +R\ <V\ =VAA="=S(&=O:6YG('=R;VYG/PH 
M("` ("` ("\O(&ES('1H:7, 8F5C875S92!E=F5N(&%F=&5R($D <')O=FED
M960 ;7D ;W=N("=D96QE=&4G(&%N9"`G?G1H:7,G+`H ("` ("` ("\O(&-O
M;7!I;&5R('-T:6QL(&EN<V5R=&5D('-O;64 97AT<F$ 8VQE86YU<"!C;V1E
M/PH ("` ?0H*("` ('-T871I8R!&;V\ 9G)E96QI<W0["0DO+R!S=&%R="!O
M9B!F<F5E(&QI<W0*"B` ("!S=&%T:6, =F]I9"!D96%L;&]C871E*$9O;R!F
M*0H ("` >PH)9BYN97AT(#T 9G)E96QI<W0[" EF<F5E;&ES="`](&8["B` 
M("!]" H ("` 1F]O(&YE>'0["0DO+R!F;W( =7-E(&)Y($9O;T9R965,:7-T
M"GT*"FEN="!M86EN*"D >PH ($9O;R!F;V\["B` 9F]O(#T ;F5W($9O;R I
M.PH (&9O;RYN86UE(#T (F]L9"UG=7DB.PH (&9O;RYF=6XH*3L*("!D96QE
M=&4 9F]O.PH*("!F;V\ /2!N97< 1F]O*"D["B` 9F]O+F9U;B I.PH*("!R
,971U<FX ,#L*?0H*
`
end
Aug 15 2008
parent reply z <z gg.com> writes:
== Quote from z (z gg.com)'s article
 Hi,
 I'm reading http://digitalmars.com/d/2.0/memory.html
 and try to combine "Free Lists" and "Explicit Class Instance Allocation".
 So instead of:
 void test()
 {
     Foo f = Foo.allocate();
     ...
     Foo.deallocate(f);
 }
 I can just write as normal:
 void test()
 {
     Foo f = new Foo();
     ...
     delete Foo;
 }
 But the simple code I wrote seg faults. Is this a compiler bug? or I'm doing
 something wrong? or can this thing be done at all?
 Thanks.
  << custom.d >>
the code: $ cat custom.d import std.stdio; import std.c.stdlib; import std.outofmemory; import std.gc; class Bar { public void fun() { printf("Bar\n"); } } class Foo : Bar { string name; this() { printf("my ctor\n"); } ~this() { printf("my dtor\n"); // suppose I don't do anything here } public override void fun() { writeln(name); } new(size_t sz) { printf("my new\n"); Foo f; if (freelist) { f = freelist; freelist = f.next; return cast(void*)f; // I know that ctor will be called again on f. Is there any way to skip :-) ? } void* p; p = std.c.stdlib.malloc(sz); if (!p) throw new OutOfMemoryException(); //std.gc.addRange(p, p + sz); return p; } delete(void* p) { printf("my del\n"); // suppose I just save it for reuse, instead of free memory Foo f = cast(Foo)p; deallocate(f); printf("saved!\n"); // try to call some method, see if it still works: f.fun(); // Segmentation fault here. Is there any way to make this work? // I run thru the debugger, and found f's attritube is not changed. // so what's going wrong? // is this because even after I provided my own 'delete' and '~this', // compiler still inserted some extra cleanup code? } static Foo freelist; // start of free list static void deallocate(Foo f) { f.next = freelist; freelist = f; } Foo next; // for use by FooFreeList } int main() { Foo foo; foo = new Foo(); foo.name = "old-guy"; foo.fun(); delete foo; foo = new Foo(); foo.fun(); return 0; }
Aug 15 2008
parent Sclytrack <sclytrack pi.be> writes:
== Extrait de l'article de « z (z gg.com) »
 == Quote from z (z gg.com)'s article
 Hi,
 I'm reading http://digitalmars.com/d/2.0/memory.html
 and try to combine "Free Lists" and "Explicit Class Instance Allocation".
;
 class Bar {
     public void fun() {
       printf("Bar\n");
     }
 }
 class Foo : Bar {
It would be easier of you split them up in two objects not inheriting from one another. class Bar {} //Use Explicit Class Instance Allocation here class BarFreeList {} //And free list here. Hmm maybe even a template.
Aug 16 2008