[fpc-devel] AllocMem(0)<>nil

petr.kristan at epos.cz petr.kristan at epos.cz
Wed Mar 26 09:55:52 CET 2008


On Tue, Mar 25, 2008 at 06:58:28PM +0100, Jonas Maebe wrote:
> 
> On 25 Mar 2008, at 18:54, Vincent Snijders wrote:
> 
> >Jonas Maebe schreef:
> >>On 25 Mar 2008, at 18:30, Peter Vreman wrote:
> >>>Current behaviour is compatible with TP7 that gave a valid pointer  
> >>>back pointing to heapend. See
> >>>also the comment.
> >>I thought so too, but I tested and TP7 stores nil in p when doing  
> >>getmem(p,0) (under DosBox).
> >
> >But AllocMem(0)?
> 
> AllocMem does not exist in TP7. And the code posted by the original  
> poster comes from SysGetMem, not from AllocMem (which is just a  
> wrapper for getmem+fillchar).
Yes. I tested GetMem and AllocMem in TP7.0, Delphi1, Delphi7, Kylix and
all compillers returns nil when size is zero.

This patch solves it.

Index: inc/heap.inc
===================================================================
--- inc/heap.inc        (revision 10368)
+++ inc/heap.inc        (working copy)
@@ -991,10 +991,11 @@
 function SysGetMem(size : ptruint):pointer;
 begin
 { Something to allocate ? }
-  if size=0 then
-    { we always need to allocate something, using heapend is not
possible,
-      because heappend can be changed by growheap (PFV) }
-    size := 1;
+  if size<=0 then
+    begin
+      result := nil;
+      exit;
+    end;
 { calc to multiple of 16 after adding the needed bytes for memchunk
header }
   if size <= (maxblocksize - sizeof(tmemchunk_fixed_hdr)) then
     begin


But heaptrc will show unfreeed blocks with zero size. And this helps.

Index: inc/heaptrc.pp
===================================================================
--- inc/heaptrc.pp      (revision 10368)
+++ inc/heaptrc.pp      (working copy)
@@ -406,6 +406,11 @@
   pp : pheap_mem_info;
   loc_info: pheap_info;
 begin
+  if size<=0 then
+    begin
+      TraceGetMem:=nil;
+      exit;
+    end;
   loc_info := @heap_info;
   try_finish_heap_free_todo_list(loc_info);
   inc(loc_info^.getmem_size,size);


Are these patches acceptable?

Petr

-- 
Ing. Petr Kristan
.
EPOS PRO s.r.o., Bozeny Nemcove 2625, 530 02 Pardubice
tel: +420 466335223    Czech Republic (Eastern Europe) 
fax: +420 466510709



More information about the fpc-devel mailing list