Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2008-03

AllocMem(0)nil

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

Hi.

I discovered some Delphi incompatibility. Call AllocMem(0) do not return nil. Fpc from svn.

In SysGetMem are some comments about it:

  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;

Is this behaviour essential, or is possible make it compatible with Delphi?

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

Reply by email

Peter Vreman
Show quoted text 14 lines
Hi. I discovered some Delphi incompatibility. Call AllocMem(0) do not return nil. Fpc from svn. In SysGetMem are some comments about it: 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; Is this behaviour essential, or is possible make it compatible with Delphi?

Current behaviour is compatible with TP7 that gave a valid pointer back pointing to heapend. See also the comment.

IMHO doing an AllocMem(0) is an error in your program.

Peter

Reply by email

Jonas Maebe
to Peter Vreman
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).

Jonas

Reply by email

Vincent Snijders
to Jonas Maebe
Jonas Maebe wrote 9 lines
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)?

Vincent

Reply by email

Jonas Maebe
to Vincent Snijders
Vincent Snijders wrote 9 lines
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).

Jonas

Reply by email

Unknown author
to Jonas Maebe
Jonas Maebe wrote 16 lines
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

Reply by email

Jonas Maebe
to Unknown author
Show quoted text 21 lines
> > 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.

Well, at the same time it breaks backwards compatibility with FPC. I really don't remember anymore why we did things this way, but I'm pretty sure there was a reason for it. Does anyone else remember?

Jonas

Reply by email