浏览代码

* always clear memory in getmem and freemem

peter 24 年之前
父节点
当前提交
25d6ec3c83
共有 1 个文件被更改,包括 56 次插入51 次删除
  1. 56 51
      rtl/inc/heaptrc.pp

+ 56 - 51
rtl/inc/heaptrc.pp

@@ -338,7 +338,7 @@ end;
 
 
 Function TraceGetMem(size:longint):pointer;
 Function TraceGetMem(size:longint):pointer;
 var
 var
-  i,bp : longint;
+  allocsize,i,bp : longint;
   pl : pdword;
   pl : pdword;
   p : pointer;
   p : pointer;
   pp : pheap_mem_info;
   pp : pheap_mem_info;
@@ -346,11 +346,12 @@ begin
   inc(getmem_size,size);
   inc(getmem_size,size);
   inc(getmem8_size,((size+7) div 8)*8);
   inc(getmem8_size,((size+7) div 8)*8);
 { Do the real GetMem, but alloc also for the info block }
 { Do the real GetMem, but alloc also for the info block }
-  bp:=size+sizeof(theap_mem_info)+extra_info_size;
+  allocsize:=size+sizeof(theap_mem_info)+extra_info_size;
   if add_tail then
   if add_tail then
-    inc(bp,sizeof(longint));
-  p:=SysGetMem(bp);
+    inc(allocsize,sizeof(longint));
+  p:=SysGetMem(allocsize);
   pp:=pheap_mem_info(p);
   pp:=pheap_mem_info(p);
+  inc(p,sizeof(theap_mem_info));
 { Create the info block }
 { Create the info block }
   pp^.sig:=$DEADBEEF;
   pp^.sig:=$DEADBEEF;
   pp^.size:=size;
   pp^.size:=size;
@@ -363,7 +364,7 @@ begin
   }
   }
   if extra_info_size>0 then
   if extra_info_size>0 then
    begin
    begin
-     pp^.extra_info:=pointer(p)+bp-extra_info_size;
+     pp^.extra_info:=pointer(pp)+allocsize-extra_info_size;
      fillchar(pp^.extra_info^,extra_info_size,0);
      fillchar(pp^.extra_info^,extra_info_size,0);
      pp^.extra_info^.check:=$12345678;
      pp^.extra_info^.check:=$12345678;
      pp^.extra_info^.fillproc:=fill_extra_info_proc;
      pp^.extra_info^.fillproc:=fill_extra_info_proc;
@@ -379,36 +380,37 @@ begin
    pp^.extra_info:=nil;
    pp^.extra_info:=nil;
   if add_tail then
   if add_tail then
     begin
     begin
-      pl:=pointer(p)+bp-extra_info_size-sizeof(longint);
+      pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(longint);
       pl^:=$DEADBEEF;
       pl^:=$DEADBEEF;
     end;
     end;
+  { clear the memory }
+  fillchar(p^,size,#255);
   { retrieve backtrace info }
   { retrieve backtrace info }
   bp:=get_caller_frame(get_frame);
   bp:=get_caller_frame(get_frame);
   for i:=1 to tracesize do
   for i:=1 to tracesize do
    begin
    begin
-     pheap_mem_info(p)^.calls[i]:=get_caller_addr(bp);
+     pp^.calls[i]:=get_caller_addr(bp);
      bp:=get_caller_frame(bp);
      bp:=get_caller_frame(bp);
    end;
    end;
   { insert in the linked list }
   { insert in the linked list }
   if heap_mem_root<>nil then
   if heap_mem_root<>nil then
-   heap_mem_root^.next:=pheap_mem_info(p);
-  pheap_mem_info(p)^.previous:=heap_mem_root;
-  pheap_mem_info(p)^.next:=nil;
+   heap_mem_root^.next:=pp;
+  pp^.previous:=heap_mem_root;
+  pp^.next:=nil;
 {$ifdef EXTRA}
 {$ifdef EXTRA}
-  pheap_mem_info(p)^.prev_valid:=heap_valid_last;
-  heap_valid_last:=pheap_mem_info(p);
+  pp^.prev_valid:=heap_valid_last;
+  heap_valid_last:=pp;
   if not assigned(heap_valid_first) then
   if not assigned(heap_valid_first) then
-    heap_valid_first:=pheap_mem_info(p);
+    heap_valid_first:=pp;
 {$endif EXTRA}
 {$endif EXTRA}
-  heap_mem_root:=p;
+  heap_mem_root:=pp;
   { must be changed before fill_extra_info is called
   { must be changed before fill_extra_info is called
     because checkpointer can be called from within
     because checkpointer can be called from within
     fill_extra_info PM }
     fill_extra_info PM }
   inc(getmem_cnt);
   inc(getmem_cnt);
-{ update the pointer }
+  { update the signature }
   if usecrc then
   if usecrc then
-    pheap_mem_info(p)^.sig:=calculate_sig(pheap_mem_info(p));
-  inc(p,sizeof(theap_mem_info));
+    pp^.sig:=calculate_sig(pp);
   TraceGetmem:=p;
   TraceGetmem:=p;
 end;
 end;
 
 
@@ -428,14 +430,15 @@ var
 begin
 begin
   inc(freemem_size,size);
   inc(freemem_size,size);
   inc(freemem8_size,((size+7) div 8)*8);
   inc(freemem8_size,((size+7) div 8)*8);
-  dec(p,sizeof(theap_mem_info));
-  pp:=pheap_mem_info(p);
-  extra_size:=pp^.extra_info_size;
+  pp:=pheap_mem_info(p-sizeof(theap_mem_info));
   ppsize:= size + sizeof(theap_mem_info)+pp^.extra_info_size;
   ppsize:= size + sizeof(theap_mem_info)+pp^.extra_info_size;
   if add_tail then
   if add_tail then
     inc(ppsize,sizeof(longint));
     inc(ppsize,sizeof(longint));
-  if not quicktrace and not(is_in_getmem_list(pp)) then
-    RunError(204);
+  if not quicktrace then
+    begin
+      if not(is_in_getmem_list(pp)) then
+       RunError(204);
+    end;
   if (pp^.sig=$AAAAAAAA) and not usecrc then
   if (pp^.sig=$AAAAAAAA) and not usecrc then
     begin
     begin
        error_in_heap:=true;
        error_in_heap:=true;
@@ -465,6 +468,8 @@ begin
        { don't release anything in this case !! }
        { don't release anything in this case !! }
        exit;
        exit;
     end;
     end;
+  { save old values }
+  extra_size:=pp^.extra_info_size;
   { now it is released !! }
   { now it is released !! }
   pp^.sig:=$AAAAAAAA;
   pp^.sig:=$AAAAAAAA;
   if not keepreleased then
   if not keepreleased then
@@ -486,13 +491,11 @@ begin
         end;
         end;
     end;
     end;
   inc(freemem_cnt);
   inc(freemem_cnt);
-  { release the normal memory at least !! }
+  { clear the memory }
+  fillchar(p^,size,#240); { $F0 will lead to GFP if used as pointer ! }
   { this way we keep all info about all released memory !! }
   { this way we keep all info about all released memory !! }
   if keepreleased then
   if keepreleased then
     begin
     begin
-       i:=ppsize;
-       inc(p,sizeof(theap_mem_info));
-       fillchar(p^,size,#240); { $F0 will lead to GFP if used as pointer ! }
 {$ifdef EXTRA}
 {$ifdef EXTRA}
        { We want to check if the memory was changed after release !! }
        { We want to check if the memory was changed after release !! }
        pp^.release_sig:=calculate_release_sig(pp);
        pp^.release_sig:=calculate_release_sig(pp);
@@ -501,6 +504,8 @@ begin
             heap_valid_last:=pp^.prev_valid;
             heap_valid_last:=pp^.prev_valid;
             if pp=heap_valid_first then
             if pp=heap_valid_first then
               heap_valid_first:=nil;
               heap_valid_first:=nil;
+            TraceFreememsize:=size;
+            p:=nil;
             exit;
             exit;
          end;
          end;
        pp2:=heap_valid_last;
        pp2:=heap_valid_last;
@@ -511,20 +516,25 @@ begin
                  pp2^.prev_valid:=pp^.prev_valid;
                  pp2^.prev_valid:=pp^.prev_valid;
                  if pp=heap_valid_first then
                  if pp=heap_valid_first then
                    heap_valid_first:=pp2;
                    heap_valid_first:=pp2;
+                 TraceFreememsize:=size;
+                 p:=nil;
                  exit;
                  exit;
               end
               end
             else
             else
               pp2:=pp2^.prev_valid;
               pp2:=pp2^.prev_valid;
          end;
          end;
 {$endif EXTRA}
 {$endif EXTRA}
+       TraceFreememsize:=size;
+       p:=nil;
        exit;
        exit;
-    end
-  else
-    i:=SysFreeMemSize(p,ppsize);
-  dec(i,sizeof(theap_mem_info)+extra_size);
-  if add_tail then
-   dec(i,sizeof(longint));
-  TraceFreeMemSize:=i;
+    end;
+   { release the normal memory at least }
+   i:=SysFreeMemSize(pp,ppsize);
+   { return the correct size }
+   dec(i,sizeof(theap_mem_info)+extra_size);
+   if add_tail then
+     dec(i,sizeof(longint));
+   TraceFreeMemSize:=i;
 end;
 end;
 
 
 
 
@@ -533,8 +543,7 @@ var
   l : longint;
   l : longint;
   pp : pheap_mem_info;
   pp : pheap_mem_info;
 begin
 begin
-  dec(p,sizeof(theap_mem_info));
-  pp:=pheap_mem_info(p);
+  pp:=pheap_mem_info(p-sizeof(theap_mem_info));
   l:=SysMemSize(pp);
   l:=SysMemSize(pp);
   dec(l,sizeof(theap_mem_info)+pp^.extra_info_size);
   dec(l,sizeof(theap_mem_info)+pp^.extra_info_size);
   if add_tail then
   if add_tail then
@@ -550,7 +559,6 @@ var
 begin
 begin
   pp:=pheap_mem_info(p-sizeof(theap_mem_info));
   pp:=pheap_mem_info(p-sizeof(theap_mem_info));
   size:=TraceMemSize(p);
   size:=TraceMemSize(p);
-  size:=TraceMemSize(p);
   { this can never happend normaly }
   { this can never happend normaly }
   if pp^.size>size then
   if pp^.size>size then
    begin
    begin
@@ -596,8 +604,7 @@ begin
      exit;
      exit;
    end;
    end;
 { Resize block }
 { Resize block }
-  dec(p,sizeof(theap_mem_info));
-  pp:=pheap_mem_info(p);
+  pp:=pheap_mem_info(p-sizeof(theap_mem_info));
   { test block }
   { test block }
   if ((pp^.sig<>$DEADBEEF) or usecrc) and
   if ((pp^.sig<>$DEADBEEF) or usecrc) and
      ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
      ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
@@ -625,10 +632,8 @@ begin
    inc(allocsize,sizeof(longint));
    inc(allocsize,sizeof(longint));
   { Try to resize the block, if not possible we need to do a
   { Try to resize the block, if not possible we need to do a
     getmem, move data, freemem }
     getmem, move data, freemem }
-  if not SysTryResizeMem(p,allocsize) then
+  if not SysTryResizeMem(pp,allocsize) then
    begin
    begin
-     { restore p }
-     inc(p,sizeof(theap_mem_info));
      { get a new block }
      { get a new block }
      oldsize:=TraceMemSize(p);
      oldsize:=TraceMemSize(p);
      newP := TraceGetMem(size);
      newP := TraceGetMem(size);
@@ -637,18 +642,18 @@ begin
        move(p^,newP^,oldsize);
        move(p^,newP^,oldsize);
      { release p }
      { release p }
      traceFreeMem(p);
      traceFreeMem(p);
-     p := newP;
-     traceReAllocMem := p;
+     { return the new pointer }
+     p:=newp;
+     traceReAllocMem := newp;
      exit;
      exit;
    end;
    end;
-  pp:=pheap_mem_info(p);
 { adjust like a freemem and then a getmem, so you get correct
 { adjust like a freemem and then a getmem, so you get correct
   results in the summary display }
   results in the summary display }
   inc(freemem_size,pp^.size);
   inc(freemem_size,pp^.size);
   inc(freemem8_size,((pp^.size+7) div 8)*8);
   inc(freemem8_size,((pp^.size+7) div 8)*8);
   inc(getmem_size,size);
   inc(getmem_size,size);
   inc(getmem8_size,((size+7) div 8)*8);
   inc(getmem8_size,((size+7) div 8)*8);
-{ Create the info block }
+{ Recreate the info block }
   pp^.sig:=$DEADBEEF;
   pp^.sig:=$DEADBEEF;
   pp^.size:=size;
   pp^.size:=size;
   pp^.extra_info_size:=oldextrasize;
   pp^.extra_info_size:=oldextrasize;
@@ -656,7 +661,7 @@ begin
   { add the new extra_info and tail }
   { add the new extra_info and tail }
   if pp^.extra_info_size>0 then
   if pp^.extra_info_size>0 then
    begin
    begin
-     pp^.extra_info:=p+allocsize-pp^.extra_info_size;
+     pp^.extra_info:=pointer(pp)+allocsize-pp^.extra_info_size;
      fillchar(pp^.extra_info^,extra_info_size,0);
      fillchar(pp^.extra_info^,extra_info_size,0);
      pp^.extra_info^.check:=$12345678;
      pp^.extra_info^.check:=$12345678;
      pp^.extra_info^.fillproc:=old_fill_extra_info_proc;
      pp^.extra_info^.fillproc:=old_fill_extra_info_proc;
@@ -668,7 +673,7 @@ begin
    pp^.extra_info:=nil;
    pp^.extra_info:=nil;
   if add_tail then
   if add_tail then
     begin
     begin
-      pl:=pointer(p)+allocsize-pp^.extra_info_size-sizeof(longint);
+      pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(longint);
       pl^:=$DEADBEEF;
       pl^:=$DEADBEEF;
     end;
     end;
   { generate new backtrace }
   { generate new backtrace }
@@ -681,8 +686,8 @@ begin
   { regenerate signature }
   { regenerate signature }
   if usecrc then
   if usecrc then
     pp^.sig:=calculate_sig(pp);
     pp^.sig:=calculate_sig(pp);
-{ update the pointer }
-  inc(p,sizeof(theap_mem_info));
+  { return the pointer }
+  p:=pointer(pp)+sizeof(theap_mem_info);
   TraceReAllocmem:=p;
   TraceReAllocmem:=p;
 end;
 end;
 
 
@@ -1118,8 +1123,8 @@ finalization
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.9  2001-04-12 18:00:14  peter
-    * allow runtime setting using the environment HEAPTRC
+  Revision 1.10  2001-04-13 01:18:08  peter
+    * always clear memory in getmem and freemem
 
 
   Revision 1.8  2001/04/11 14:08:31  peter
   Revision 1.8  2001/04/11 14:08:31  peter
     * some small fixes to my previous commit
     * some small fixes to my previous commit