2
0
Rika Ichinose 1 сар өмнө
parent
commit
4e69e46b8e

+ 51 - 54
rtl/inc/heap.inc

@@ -1794,32 +1794,6 @@ begin
     result := 0;
 end;
 
-function SysTryResizeMem(var p: pointer; size: ptruint): boolean;
-var
-  ts: HeapInc.pThreadState;
-  h: uint32;
-  newp: pointer;
-begin
-  h := HeapInc.pCommonHeader(p - HeapInc.CommonHeaderSize)^.h;
-  if h and HeapInc.FixedFlag <> 0 then
-    result := (size <= HeapInc.MaxFixedHeaderAndPayload - HeapInc.CommonHeaderSize) and (h and HeapInc.SizeIndexMask = HeapInc.SizeMinus1ToIndex(size + (HeapInc.CommonHeaderSize - 1)))
-  else
-  begin
-    ts := @HeapInc.thisTs;
-  {$ifdef FPC_HAS_FEATURE_THREADING}
-    if Assigned(ts^.toFree) then
-      ts^.FlushToFree;
-  {$endif FPC_HAS_FEATURE_THREADING}
-    if h <> HeapInc.HugeHeader then
-      newp := ts^.TryResizeVar(p, size)
-    else
-      newp := ts^.TryResizeHuge(p, size);
-    result := Assigned(newp);
-    if result then
-      p := newp;
-  end;
-end;
-
 function SysMemSize(p: pointer): ptruint;
 var
   h: uint32;
@@ -1837,40 +1811,63 @@ end;
 
 function SysReAllocMem(var p: pointer; size: ptruint):pointer;
 var
-  oldsize, newsize, tocopy: SizeUint;
+  ts: HeapInc.pThreadState;
+  h: uint32;
+  oldOrCopySize: SizeUint;
+  newp: pointer;
 begin
-  if size = 0 then
+  result := p; { Use as old p value until freed etc. }
+  if (size = 0) or not Assigned(result) then { Special cases; check at once. }
+  begin
+    if size = 0 then
     begin
-      SysFreeMem(p);
+      SysFreeMem(result);
       result := nil;
-      p := nil;
-    end
-  else if not Assigned(p) then
-    begin
+    end else
       result := SysGetMem(size);
-      p := result;
-    end
-  else if SysTryResizeMem(p, size) then
-    result := p
-  else
+    p := result;
+    exit;
+  end;
+  h := HeapInc.pCommonHeader(result - HeapInc.CommonHeaderSize)^.h;
+  if h and HeapInc.FixedFlag <> 0 then
+  begin
+    if (size <= HeapInc.MaxFixedHeaderAndPayload - HeapInc.CommonHeaderSize)
+      and (h and HeapInc.SizeIndexMask = HeapInc.SizeMinus1ToIndex(size + (HeapInc.CommonHeaderSize - 1))) then
+      exit;
+  end else
+  begin
+    ts := @HeapInc.thisTs;
+  {$ifdef FPC_HAS_FEATURE_THREADING}
+    if Assigned(ts^.toFree) then
+      ts^.FlushToFree;
+  {$endif FPC_HAS_FEATURE_THREADING}
+    if h <> HeapInc.HugeHeader then
+      newp := ts^.TryResizeVar(result, size)
+    else
+      newp := ts^.TryResizeHuge(result, size);
+    if Assigned(newp) then
     begin
-      oldsize := SysMemSize(p);
-      newsize := size;
-      result := SysGetMem(newsize);
-      if not Assigned(result) then
-        begin
-          if size <= oldsize then
-            { Don’t fail if shrinking. }
-            result := p;
-          exit; { If growing failed, return nil, but keep the old p. }
-        end;
-      tocopy := oldsize;
-      if tocopy > newsize then
-        tocopy := newsize;
-      Move(p^, result^, tocopy);
-      SysFreeMem(p);
-      p := result;
+      p := newp;
+      exit(newp);
     end;
+  end;
+
+  { Generic fallback: GetMem + Move + FreeMem. }
+  oldOrCopySize := SysMemSize(result);
+  newp := SysGetMem(size);
+  if not Assigned(newp) then
+  begin
+    { Don’t fail if shrinking. If growing failed, return nil, but keep the old p. }
+    if size > oldOrCopySize then
+      result := nil;
+    exit;
+  end;
+  p := newp;
+  if oldOrCopySize > size then
+    oldOrCopySize := size;
+  Move(result^, newp^, oldOrCopySize);
+  SysFreeMem(result);
+  result := newp;
 end;
 
 Function SysFreeMemSize(p: pointer; size: ptruint):ptruint;

+ 0 - 1
rtl/inc/heaph.inc

@@ -74,7 +74,6 @@ Function  SysFreemem(p:pointer):ptruint;
 Function  SysFreememSize(p:pointer;Size:ptruint):ptruint;
 Function  SysMemSize(p:pointer):ptruint;
 Function  SysAllocMem(size:ptruint):Pointer;
-function  SysTryResizeMem(var p:pointer;size:ptruint):boolean;
 Function  SysReAllocMem(var p:pointer;size:ptruint):Pointer;
 function  SysGetHeapStatus:THeapStatus;
 function  SysGetFPCHeapStatus:TFPCHeapStatus;

+ 35 - 61
rtl/inc/heaptrc.pp

@@ -824,9 +824,7 @@ end;
 
 function TraceReAllocMem(var p:pointer;size:ptruint):Pointer;
 var
-  newP: pointer;
-  i, allocsize,
-  movesize  : ptruint;
+  i, allocsize : ptruint;
   pl : pdword;
   pp,prevpp{$ifdef EXTRA},ppv{$endif} : pheap_mem_info;
   oldsize,
@@ -888,66 +886,42 @@ begin
   { Try to resize the block, if not possible we need to do a
     getmem, move data, freemem }
   prevpp:=pp;
-  if not SysTryResizeMem(pp,allocsize) then
-   begin
-     { get a new block }
-     newP := TraceGetMem(size);
-     { move the data }
-     if newP <> nil then
-      begin
-        movesize:=TraceMemSize(p);
-        {if the old size is larger than the new size,
-         move only the new size}
-        if movesize>size then
-          movesize:=size;
-        move(p^,newP^,movesize);
-      end;
-     { release p }
-     traceFreeMem(p);
-     { return the new pointer }
-     p:=newp;
-     traceReAllocMem := newp;
-     exit;
-   end
-  else
-   begin
-     if (pp<>prevpp) then
-       begin
-         { We need to update the previous/next chains }
-         if assigned(pp^.previous) then
-           pp^.previous^.next:=pp;
-         if assigned(pp^.next) then
-           pp^.next^.previous:=pp;
-         if prevpp=loc_info^.heap_mem_root then
-           loc_info^.heap_mem_root:=pp;
+  if (SysReAllocMem(pp,allocsize)<>prevpp) then
+    begin
+      { We need to update the previous/next chains }
+      if assigned(pp^.previous) then
+        pp^.previous^.next:=pp;
+      if assigned(pp^.next) then
+        pp^.next^.previous:=pp;
+      if prevpp=loc_info^.heap_mem_root then
+        loc_info^.heap_mem_root:=pp;
 {$ifdef EXTRA}
-         { remove prevpp from prev_valid chain }
-         ppv:=loc_info^.heap_valid_last;
-         if (ppv=prevpp) then
-           loc_info^.heap_valid_last:=pp^.prev_valid
-         else
-           begin
-             while assigned(ppv) do
-               begin
-                 if (ppv^.prev_valid=prevpp) then
-                   begin
-                     ppv^.prev_valid:=pp^.prev_valid;
-                     if prevpp=loc_info^.heap_valid_first then
-                       loc_info^.heap_valid_first:=ppv;
-                     ppv:=nil;
-                   end
-                 else
-                   ppv:=ppv^.prev_valid;
-               end;
-           end;
-         { Reinsert new value in last position }
-         pp^.prev_valid:=loc_info^.heap_valid_last;
-         loc_info^.heap_valid_last:=pp;
-         if not assigned(loc_info^.heap_valid_first) then
-           loc_info^.heap_valid_first:=pp;
+      { remove prevpp from prev_valid chain }
+      ppv:=loc_info^.heap_valid_last;
+      if (ppv=prevpp) then
+        loc_info^.heap_valid_last:=pp^.prev_valid
+      else
+        begin
+          while assigned(ppv) do
+            begin
+              if (ppv^.prev_valid=prevpp) then
+                begin
+                  ppv^.prev_valid:=pp^.prev_valid;
+                  if prevpp=loc_info^.heap_valid_first then
+                    loc_info^.heap_valid_first:=ppv;
+                  ppv:=nil;
+                end
+              else
+                ppv:=ppv^.prev_valid;
+            end;
+        end;
+      { Reinsert new value in last position }
+      pp^.prev_valid:=loc_info^.heap_valid_last;
+      loc_info^.heap_valid_last:=pp;
+      if not assigned(loc_info^.heap_valid_first) then
+        loc_info^.heap_valid_first:=pp;
 {$endif EXTRA}
-       end;
-   end;
+    end;
 { Recreate the info block }
   pp^.sig:=longword(AllocateSig);
   pp^.size:=size;

+ 0 - 5
rtl/inc/osheap.inc

@@ -71,11 +71,6 @@
         result:=pptruint(p)[0]-sizeof(ptruint);
       end;
 
-    function SysTryResizeMem(var p: pointer; size: ptruint) : boolean;
-      begin
-        result := false;
-      end;
-
     function SysAllocMem(size: ptruint): pointer;
       begin
         result := SysGetMem(size);

+ 0 - 5
rtl/inc/tinyheap.inc

@@ -285,11 +285,6 @@
         result := findsize(p);
       end;
 
-    function SysTryResizeMem(var p: pointer; size: ptruint) : boolean;
-      begin
-        result := false;
-      end;
-
     function SysAllocMem(size: ptruint): pointer;
       begin
         result := SysGetMem(size);

+ 0 - 10
rtl/nativent/sysheap.inc

@@ -132,16 +132,6 @@ begin
   p := SysReAllocMem;
 end;
 
-function SysTryResizeMem(var p: Pointer; size: PtrUInt): Boolean;
-var
-  res: pointer;
-begin
-  res := SysGetMem(Size);
-  SysTryResizeMem := (res <> Nil) or (Size = 0);
-  if SysTryResizeMem then
-    p := res;
-end;
-
 function SysMemSize(P : pointer): PtrUInt;
 begin
   SysMemSize := PPtrUInt(P - SizeOf(PtrUInt) - SizeOf(LongWord))^;

+ 0 - 10
rtl/wince/system.pp

@@ -1599,16 +1599,6 @@ begin
   p:=Result;
 end;
 
-function SysTryResizeMem(var p:pointer;size : ptruint):boolean;
-var
-  res: pointer;
-begin
-  res:=realloc(p, Size);
-  Result:=(res <> nil) or (Size = 0);
-  if Result then
-    p:=res;
-end;
-
 function SysMemSize(P : pointer): ptruint;
 begin
   Result:=_msize(P);