[fpc-pascal]two questions plz help

Thomas Schatzl tom_at_work at yline.com
Thu Jan 10 15:42:28 CET 2002


Subject: Re: [fpc-pascal]two questions plz help


> > Does that mean the RTL is already MT safe ?
>
> No, it's not. So fork() is currently only useful if you do

I assumed because a developer answered to this question without mentioning
this....

> a) no memory allocation (or if you use a thread-safe heap manager, such
> as the one in the cmem unit of the packages)

This includes implicit memory allocations like ansistring handling, object
creation, etc. I guess ?

> b) no IO
> c) no errno error checking
>
> I think the above are the only parts that are not thread safe...

So if I used a unit like this for Win32 (I remember cmem being Linux
only...) everything should be ok ? [Assuming that the Win32 system memory
management is threadsafe]

---------------- start of unit
unit win32heap;

interface
{$mode delphi}

implementation

uses
 windows;

function win32getmem(size : longint) : pointer;
begin
 result := HeapAlloc(GetProcessHeap, 0, size);
end;

function win32freemem(var p : pointer) : longint;
begin
 result := HeapSize(GetProcessHeap, 0, p);
 HeapFree(GetProcessHeap, 0, p);
 p := nil;
end;

function win32freememsize(var p : pointer; size : longint) : longint;
begin
 result := win32freemem(p);
end;

function win32allocmem(size : longint) : pointer;
begin
 result := HeapAlloc(GetProcessHeap, HEAP_ZERO_MEMORY, size);
end;

function win32reallocmem(var p : pointer; size : longint) : pointer;
begin
 result := HeapReAlloc(GetProcessHeap, 0, p, size);
 if (result <> p) then
  win32freemem(p);
end;

function win32memsize(p : pointer) : longint;
begin
 result := HeapSize(GetProcessHeap, 0, p);
end;

function win32memavail : longint;
begin
 result := 0;
end;

function win32maxavail : longint;
begin
 result := 0;
end;

function win32heapsize : longint;
begin
 result := 0;
end;

const
 Win32MemoryManager : TMemoryManager = (
  getmem : @win32Getmem;
  freemem : @win32Freemem;
  freememsize : @win32FreememSize;
  allocmem : @win32Allocmem;
  reAllocmem : @win32ReAllocmem;
  memsize : @win32MemSize;
  memAvail : @win32MemAvail;
  maxAvail : @win32MaxAvail;
  heapSize : @win32Heapsize;
 );

var
 defaultMemoryManager : TMemoryManager;

initialization
begin
 GetMemoryManager(defaultMemoryManager);
 SetMemoryManager(Win32MemoryManager);
end;

finalization
begin
 Writeln('exiting memory manager');
 SetMemoryManager(defaultMemoryManager);
end;

end.
--------- end of unit

Regards,
  Thomas





More information about the fpc-pascal mailing list