From 4030d67a73d4b5254912c586b6638ca3851855d6 Mon Sep 17 00:00:00 2001 From: Tristan Gingold Date: Mon, 25 Jul 2022 05:12:24 +0200 Subject: dyn_tables,tables: add Reserve. For #2139 --- src/dyn_tables.adb | 22 +++++++++++++++++----- src/dyn_tables.ads | 2 ++ src/tables.adb | 5 +++++ src/tables.ads | 4 ++++ 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/dyn_tables.adb b/src/dyn_tables.adb index 972ffd492..cf292d4fb 100644 --- a/src/dyn_tables.adb +++ b/src/dyn_tables.adb @@ -23,8 +23,9 @@ package body Dyn_Tables is size_t (Table_Type'Component_Size / System.Storage_Unit); -- Expand the table by doubling its size. The table must have been - -- initialized. - procedure Expand (T : in out Instance; Num : Unsigned) + -- initialized. Memory space for the extra elements are allocated but the + -- length of the table is not increased. + procedure Reserve (T : in out Instance; Num : Unsigned) is -- For efficiency, directly call realloc. function Crealloc (Ptr : Table_Thin_Ptr; Size : size_t) @@ -42,10 +43,9 @@ package body Dyn_Tables is if New_Last < T.Priv.Last_Pos then raise Constraint_Error; end if; - T.Priv.Last_Pos := New_Last; -- Check if need to reallocate. - if T.Priv.Last_Pos < T.Priv.Length then + if New_Last < T.Priv.Length then return; end if; @@ -59,7 +59,7 @@ package body Dyn_Tables is end if; T.Priv.Length := New_Len; - exit when New_Len > T.Priv.Last_Pos; + exit when New_Len > New_Last; end loop; -- Realloc and check result. @@ -70,8 +70,20 @@ package body Dyn_Tables is if T.Table = null then raise Storage_Error; end if; + end Reserve; + + -- Expand the table (allocate and increase the length). + procedure Expand (T : in out Instance; Num : Unsigned) is + begin + Reserve (T, Num); + T.Priv.Last_Pos := T.Priv.Last_Pos + Num; end Expand; + procedure Reserve (T : in out Instance; Num : Natural) is + begin + Reserve (T, Unsigned (Num)); + end Reserve; + procedure Allocate (T : in out Instance; Num : Natural := 1) is begin Expand (T, Unsigned (Num)); diff --git a/src/dyn_tables.ads b/src/dyn_tables.ads index 4f8096f81..d6577de60 100644 --- a/src/dyn_tables.ads +++ b/src/dyn_tables.ads @@ -91,6 +91,8 @@ package Dyn_Tables is -- Increase by NUM the length of the array. procedure Allocate (T : in out Instance; Num : Natural := 1); + -- Reserve memory for NUM extra entries. + procedure Reserve (T : in out Instance; Num : Natural); private type Unsigned is mod 2**32; diff --git a/src/tables.adb b/src/tables.adb index 3ed04af87..ffd071a9b 100644 --- a/src/tables.adb +++ b/src/tables.adb @@ -24,6 +24,11 @@ package body Tables is return Res; end Allocate; + procedure Reserve (Num : Natural := 1) is + begin + Dyn_Table.Reserve (T, Num); + end Reserve; + procedure Increment_Last is begin -- Increase by 1. diff --git a/src/tables.ads b/src/tables.ads index 9a506d871..a76398d2f 100644 --- a/src/tables.ads +++ b/src/tables.ads @@ -79,4 +79,8 @@ package Tables is -- of Last + 1. function Allocate (Num : Natural := 1) return Table_Index_Type; pragma Inline (Allocate); + + -- Reserve memory for NUM extra elements. + procedure Reserve (Num : Natural := 1); + pragma Inline (Reserve); end Tables; -- cgit v1.2.3