9-Mar-00 03:01 you wrote:
Khimenko Victor wrote:
This is NOT what you usually want. GC ALWAYS has penatly over "normal" malloc/free way. Yes, there are benefits as well but when you are using third-party library where malloc/free already used then with GC's replacement you'll get slowdown and nothing more. In case of C it's all simple: just use malloc/free and gc_alloc as desired. In case of C++ overloaded new can be used for this purpose. In case of GPC you can have GC-enabled new (with GC's malloc) or non GC-enabled new bot not both :-/ You can not just use gc_alloc(sizeof(record)) since if record has embedded strings or files you need to initialize it first (and if record has virtual part then new can allocate less then gc_alloc(sizeof(record)) -- or is this Pascal's feature not implemented yet?). So the question is: is it possible to use another allocator (and deallocator) in GPC's new ? Something akin to field:=new(malloc,OldNormalNonGcClass); ptr:=new(gc_alloc,TestClass); dispose(free,field);
Something like this was discussed when shred memory allocation problem was discussed but I can not find it :-/
The relevant hooks are GetMemPtr etc., declared in the GPC unit. You need them if you want to switch between different allocators.
Hmm. It'll require locks in multithreaded program and will be not very convenient. Perhaps we can hide it over clever #define ? Let's try: -- cut -- uses gpc; #define gc_alloc(x) (begin var res:^x; save_ptr:GetMemType; save_ptr:=GetMemPtr; GetMemPtr:=GC_alloc; res:=new(x); GetMemPtr:=save_ptr; end) var i:^integer; begin i:=gc_alloc(integer); end. -- cut -- $ gpc test.p test.p:1: warning: missing program header test.p: In function `program_noname': test.p:6: parse error before `Begin' test.p:6: undeclared label `Save_ptr' test.p:6: warning: function call as a statement - value is ignored test.p:6: undeclared identifier `Save_ptr' (first use this function) test.p:6: (Each undeclared identifier is reported only once test.p:6: for each function it appears in.) test.p:6: undeclared identifier `Gc_alloc' (first use this function) test.p:6: pointer type required for `New' test.p: At top level: test.p:6: parse error before `)' test.p:6: warning: missing `.' at the end of program test.p:6: warning: missing program header test.p:6: only one program declaration allowed test.p:6: syntax error before `)' -- cut -- Oops. Is it possible to use compound statement as expression in GPC somehow ? And BTW how I can continue #define on next line (C's \ in the end of line does not work).
By default, AIUI, Boehm's GC, when linked in, will be used for all allocations without further action on the Pascal side.
Oh, no. Boehm's GC is non-intrusive by default. You need to recompile with -DREDIRECT_MALLOC=GC_malloc or -DREDIRECT_MALLOC=GC_malloc_uncollectable to replace standard malloc/free (and in latter case, of course, no GC will occur).