A bug or a feature? SysAllocMem calls MemoryManager.GetMem instead of SysGetMem lazarus / 62-General ---------------------------------------------------------------------- chebmaster · 2017-02-28 17:03 I am confused. I tried creating a wrapper memory manager for debugging purposes (i.e. a memory manager that expands on the system one by increasing allocated block sizes, thus adding some extra data to the allocated blocks) It failed craptacularly because, for example, SysAllocMem() calls not on SysGetMem as one would expect but on MemoryManager.GetMem  - which I already redefined and which adds that record and performs pointer arithmetic before calling SysGetMem - i.e. the block gets added twice, then FreeMem comes trying to untangle tham mess and the everything goes to hell.  Sadly that wasn't t the only place the MemoryManager.xxx methods get called. So, my question is: is this done for some compatibility reason, or is this an oversight? Because as it is now, the only way to get a modded memory manager is to write one from scratch ---------------------------------------------------------------------- marcov · 2017-02-28 17:34 The Sys variants are afaik the default  implementations for the memorymanager records. They are afaik meant to be replaced en-bloc by another memory manager implementation, and their internals are not documented? Did you maybe replace getmem in the memory manager, but not allocmem in the memory manager? ---------------------------------------------------------------------- chebmaster · 2017-02-28 18:00 I surely replaced them *all*. The problem is that I replaced them with wrappers that called upon them (OldMM.AllocMem or SysAllocMem - it makes no difference). But they, in turn, were calling upon my wrappers instead of calling on each other. Which is fundamentally wrong. So I am trying to understand: why? Is there an obscure but legitimate reason, or is this a genuine bug? I tried hacking around that (see SysGetMem below), but SysReallocMem is too complex for it to work Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   function mml_AllocMem(Size : ptruint) : Pointer;  begin    EnterCriticalSection(CS);    Result:= MyAllocMem(Size);    LeaveCriticalSection(CS);  end;   function MyAllocMem  (Size : ptruint) : Pointer;  var    b: PMemBlockRec;    newallocsize: ptruint;  begin      if Size = 0 then begin        Exit(nil);      end;       newallocsize:= Size + sizeof(TMemBlockRec);      b:= SysGetMem(newallocsize);      FillChar(b^, newallocsize, 0);      b^.sig:= defsig;      b^.size:= Size;      b^.alloc_size:= newallocsize;      b^.ThreadId:= GetCurrentThreadID;      if not deactivated then begin        b^.prev:= @StubBlock;        b^.next:= StubBlock.next;        StubBlock.next:= b;        if Assigned(b^.next) then b^.next^.prev:= b;        GetBacktrace(b);      end;      Result:= pointer(ptruint(b) + sizeof(TMemBlockRec));  end; ---------------------------------------------------------------------- chebmaster · 2017-02-28 18:12 P.S. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Function mml_FreeMem (P : pointer) : ptruint;  begin    EnterCriticalSection(CS);    Result:= MyFreeMem(p);    LeaveCriticalSection(CS);  end;   Function mml_FreeMemSize(p:pointer;Size:ptruint):ptruint;  begin    Result:= mml_FreeMem(p);  end;    Function myFreeMem (P : pointer) : ptruint;  var    b: PMemBlockRec;    alien: boolean = true;    newallocsize: ptruint;  begin      if not Assigned(p) then begin        Exit(0);    end;      b:= PMemBlockRec(pointer(ptruint(p) - sizeof(TMemBlockRec)));      alien:= b^.sig <> defsig;      if alien then begin        //alien block allocated prior to installation of my memory manager        myFreeMem:= SysFreeMem (p);        Exit      end;      if Assigned(b^.next) then b^.next^.prev:= b^.prev;      if Assigned(b^.prev) then b^.prev^.next:= b^.next;      b^.prev:= nil;    b^.next:= nil;      myFreeMem:= b^.size;      SysFreeMemSize(b, b^.alloc_size);   end; ---------------------------------------------------------------------- Cyrax · 2017-03-01 00:41 I would not recommend to use directly Sys* variants of memory allocation functions. Rather make a backup of previous memory manager (GetMemoryManager) and use it extensively in your own manager by redirecting calls to old manager with necessary modifications. ---------------------------------------------------------------------- chebmaster · 2017-03-01 07:16 Quotemake a backup of previous memory manager (GetMemoryManager) and use it extensively in your own manager by redirecting calls to old manager with necessary modificationsHave you read my posts at all? I DID EXACTLY THAT. IT DOESN'T WORK. Because the Sys*** functions call MemoryManager.*** functions, not their fellow Sys*** functions. Please stop giving me noob-targeted answers. I have more than 20 years of experience using Pascal, since Turbo 5.0 / Delphi 2.0 / FPC 1.X So. To anyone who can understand the topic: Is that a bug or is that an obscure feature? P.S. Sister topic in Russian: http://freepascal.ru/forum/viewtopic.php?f=1&t=18231 ---------------------------------------------------------------------- Cyrax · 2017-03-01 10:30 Take chill pill, dude. With that kind of attitude, you will not find help here in the future. I take that this code example won't work because of reasons? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  Classes  { you can add units after this }; Var  AOldMemoryManager : TMemoryManager; Function MyGetmem(Size:ptruint):Pointer;Var  AMemory : PByte;begin  AMemory := AOldMemoryManager.Getmem(Size + 100);  Inc(AMemory, 100);  Result := AMemory;end; Function MyFreeMem(p:pointer):ptruint;Var  AMemory : PByte;Begin  AMemory := P;  Dec(AMemory, 100);  Result := AOldMemoryManager.FreeMem(AMemory);end; Var  ANewMemoryManager : TMemoryManager;  AMemory : Pointer;begin  System.GetMemoryManager(AOldMemoryManager);  ANewMemoryManager := AOldMemoryManager;   ANewMemoryManager.Getmem := @MyGetmem;  ANewMemoryManager.FreeMem := @MyFreeMem;   System.SetMemoryManager(ANewMemoryManager);  GetMem(AMemory, 10);  FreeMem(AMemory);   System.SetMemoryManager(AOldMemoryManager);end. ---------------------------------------------------------------------- chebmaster · 2017-03-01 10:58 QuoteI take that this code example won't work because of reasons?Yeah  It would have worked IF SysReallocMem only used MemoryManager.GetMem/FreeMem/MemSize. Alas, it also uses  SysTryResizeMem. In short, it mixes calls to my wrapper functions that work in the transformed space with calls to routines that should work with original space. Nothing but mess can result. A wrapper would only work if all it did was non-intrusive stuff like counting number of deallocations during certain phase of the Moon QuoteTake chill pill, dude.I try, but "intellectual" replies like "Try adding 2 and 2! It's 4, see?" make ANGRY ---------------------------------------------------------------------- molly · 2017-03-01 11:09 In that case, something seems a bit 'off' in the documentation: QuoteTo implement your own memory manager, it is sufficient to construct such a record and to issue a call to SetMemoryManager. To avoid conflicts with the system memory manager, setting the memory manager should happen as soon as possible in the initialization of your program, i.e. before any call to getmem is processed. This means in practice that the unit implementing the memory manager should be the first in the uses clause of your program or library, since it will then be initialized before all other units - except the system unit itself, of course. ---------------------------------------------------------------------- marcov · 2017-03-01 11:09 I assume you did look at heaptrc implementation ? ---------------------------------------------------------------------- Thaddy · 2017-03-01 12:12 If somebody doesn't want to understand the difference between RAW memory allocation (getmem) and CLEAN memory allocation (allocmem), why bother to answer. Sometimes it pays to be rude....< grumpy mode > ---------------------------------------------------------------------- Thaddy · 2017-03-01 12:14 Quote from: molly on March 01, 2017, 12:09:31 pmIn that case, something seems a bit 'off' in the documentation: QuoteTo implement your own memory manager, it is sufficient to construct such a record and to issue a call to SetMemoryManager. To avoid conflicts with the system memory manager, setting the memory manager should happen as soon as possible in the initialization of your program, i.e. before any call to getmem is processed. This means in practice that the unit implementing the memory manager should be the first in the uses clause of your program or library, since it will then be initialized before all other units - except the system unit itself, of course. That's not off, only for heaptrc. The compiler makes that decision. You can..however... write your own heaptrc unit as long as it's called heaptrc.... and with -gh.... Hmm, maybe I should edit the wiki some more... If you want a memorymanager to be able to do what is asked.... What logically follows is you can't debug properly unless your own memory manager provides for  that. You aren't silly idiots, are you??? ---------------------------------------------------------------------- molly · 2017-03-01 12:39 @Thaddy: Remind me to put up a smiley next time i put up documentation with the intention to let TS decide whether the documentation is at fault or his code. That part of the documentation has been there ... how many years ? And nobody noticed there was something wrong with it.... hmz... maybe ... perhaps .... possibly .... there is something wrong with my code or assumptions ? There is a perfect example accompanied with that link, using c memory manager. Note that i'm not excluding any errors that do exists in the documentation. ---------------------------------------------------------------------- marcov · 2017-03-01 12:42 Quote from: Thaddy on March 01, 2017, 01:14:32 pm That's not off, only for heaptrc. The compiler makes that decision. You can..however... write your own heaptrc unit as long as it's called heaptrc.... and with -gh.... .. You aren't silly idiots, are you??? some can't even find -Fa ! ---------------------------------------------------------------------- chebmaster · 2017-03-01 13:44 QuoteIf somebody doesn't want to understand the difference between RAW memory allocation (getmem) and CLEAN memory allocation (allocmem),Wut? I do call FillChar(b^, newallocsize, 0); there. I was hoping to create a custom MM quickly, in a couple of hours, for a specific debugging purpose. So writing optimal code wasn't my goal. Writing working code was. QuoteThis means in practice that the unit implementing the memory manager should be the first in the uses clause of your program or library, since it will then be initialized before all other units - except the system unit itself, of course.I though I had a workaround for that: my MM marked each block with a signature (at negative offset relative to the memory block returned by it) in and passed the call to OldMM if there was no such signature. It would have worked, too (with negligible chance of crash), if not the topic of this thread. QuoteheaptrcIt includes lineinfo but lineinfo either causes my custom lineinfo to fail (-gl, no -gw2) or it fails itself (-gw2, no -gl) Either way my game engine reports all exception addresses in useless hexadecimals. Anyway, since the simple solution failed I'm going to create an extensible memory manager using mmap, mremap, VirtualAlloc, MapViewOfFile and the like. There is no need to hold back  Should be way easier than taming heaptrc, right?