libgpr2_24.0.0_eda3c693/src/lib/gpr2-project-attribute_cache.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
--
--  Copyright (C) 2019-2023, AdaCore
--
--  SPDX-License-Identifier: Apache-2.0 WITH LLVM-Exception
--

with Ada.Unchecked_Deallocation;
with GNAT.Task_Lock;

package body GPR2.Project.Attribute_Cache is

   use type Project.Attribute_Index.Object;

   function Cache_Key
      (Name   : Q_Attribute_Id;
       Index  : Project.Attribute_Index.Object := Attribute_Index.Undefined;
       At_Pos : Unit_Index                     := No_Index)
      return String;
   --  Given the Get_Attribute parameters return a unique key.

   procedure Free is new Ada.Unchecked_Deallocation
      (Attribute_Cache_Maps.Map, Map_Access);

   procedure Free is new Ada.Unchecked_Deallocation
      (Inner_Object, Inner_Object_Access);

   ------------
   -- Adjust --
   ------------

   overriding procedure Adjust (Cache : in out Object) is
   begin
      Cache.Inner := new Inner_Object'
        (Enabled               => Cache.Inner.Enabled,
         Table                 => new Attribute_Cache_Maps.Map'
                                    (Cache.Inner.Table.Copy),
         --  No need to keep the former cache table on copy
         Former_Table          => null,
         --  Extra capacity should be set to 0 as no new element is scheduled
         --  for addition in the cache.
         Needed_Extra_Capacity => 0);

      --  Ensure cache capacity is at least Min_Cache_Size
      if Cache.Inner.Table.Capacity < Min_Cache_Size then
         Cache.Inner.Table.Reserve_Capacity (Min_Cache_Size);
      end if;
   end Adjust;

   ---------------
   -- Cache_Key --
   ---------------

   function Cache_Key
      (Name   : Q_Attribute_Id;
       Index  : Project.Attribute_Index.Object := Attribute_Index.Undefined;
       At_Pos : Unit_Index                     := No_Index)
      return String
   is
   begin
      if Index /= Attribute_Index.Undefined then
         return Name.Attr'Img & ":" & Name.Pack'Img & ":" &
            Index.Value (Preserve_Case => Index.Is_Case_Sensitive) & ":" &
            At_Pos'Img;
      else
         return Name.Attr'Img & ":" & Name.Pack'Img;
      end if;
   end Cache_Key;

   -----------------
   -- Check_Cache --
   -----------------

   function Check_Cache
      (Self   : Object;
       Name   : Q_Attribute_Id;
       Index  : Project.Attribute_Index.Object := Attribute_Index.Undefined;
       At_Pos : Unit_Index                     := No_Index)
      return Cursor
   is
      Key : constant String := Cache_Key (Name, Index, At_Pos);
   begin
      return Attribute_Cache.Cursor
         (Attribute_Cache_Maps.Find (Self.Inner.Table.all, Key));
   end Check_Cache;

   -----------------
   -- Clear_Cache --
   -----------------

   procedure Clear_Cache (Self : Object) is
   begin
      Attribute_Cache_Maps.Clear (Self.Inner.Table.all);
   end Clear_Cache;

   -------------------
   -- Disable_Cache --
   -------------------

   procedure Disable_Cache (Self : Object) is
   begin
      Self.Inner.Enabled := False;
      Self.Clear_Cache;
   end Disable_Cache;

   -------------
   -- Element --
   -------------

   overriding function Element
      (C : Cursor) return GPR2.Project.Attribute.Object
   is
   begin
      return Attribute_Cache_Maps.Element (Attribute_Cache_Maps.Cursor (C));
   end Element;

   ------------------
   -- Enable_Cache --
   ------------------

   procedure Enable_Cache (Self : Object) is
   begin
      Self.Inner.Enabled := True;
   end Enable_Cache;

   --------------
   -- Finalize --
   --------------

   overriding procedure Finalize (Cache : in out Object) is
      procedure Free is new Ada.Unchecked_Deallocation
         (Attribute_Cache_Maps.Map, Map_Access);

   begin
      if Cache.Inner /= null then
         if Cache.Inner.Table /= null then
            Free (Cache.Inner.Table);
         end if;

         if Cache.Inner.Former_Table /= null then
            Free (Cache.Inner.Former_Table);
         end if;

         Free (Cache.Inner);
      end if;

   end Finalize;

   -----------------
   -- Has_Element --
   -----------------

   overriding function Has_Element (C : Cursor) return Boolean is
   begin
      return Attribute_Cache_Maps.Has_Element
         (Attribute_Cache_Maps.Cursor (C));
   end Has_Element;

   ----------------
   -- Initialize --
   ----------------

   overriding procedure Initialize (Cache : in out Object)
   is
   begin
      Cache.Inner := new Inner_Object'
        (Enabled               => True,
         Table                 => new Attribute_Cache_Maps.Map'
                                    (Attribute_Cache_Maps.Empty
                                       (Capacity => Min_Cache_Size)),
         Former_Table          => null,
         Needed_Extra_Capacity => 0);
   end Initialize;

   ---------------------------
   -- Schedule_Update_Cache --
   ---------------------------

   procedure Schedule_Update_Cache (Self : Object) is
   begin
      GNAT.Task_Lock.Lock;

      --  In the worst case we will need 1 more element in the cache
      Self.Inner.Needed_Extra_Capacity :=
         Self.Inner.Needed_Extra_Capacity + 1;

      if Self.Inner.Table.Length + Self.Inner.Needed_Extra_Capacity >
         Self.Inner.Table.Capacity
      then
         --  We have reached the maximum capacity. Increase the table size
         declare
            New_Table : constant Map_Access :=
               new Attribute_Cache_Maps.Map'(Self.Inner.Table.all.Copy);
            New_Capacity : Ada.Containers.Count_Type :=
               Self.Inner.Table.Capacity * 2;
         begin
            while New_Capacity <
               Self.Inner.Table.Length + Self.Inner.Needed_Extra_Capacity
            loop
               New_Capacity := New_Capacity * 2;
            end loop;

            New_Table.Reserve_Capacity (New_Capacity);

            --  Free the former table. We assume that sufficiently time has
            --  passed so that any concurrent query on the former table had
            --  time to finish. Making that assumption allows GPR2 to avoid
            --  need for lock on cache read.
            if Self.Inner.Former_Table /= null then
               Free (Self.Inner.Former_Table);
            end if;

            --  Switch access to tables
            Self.Inner.Former_Table := Self.Inner.Table;
            Self.Inner.Table := New_Table;
         end;
      end if;

      GNAT.Task_Lock.Unlock;
   end Schedule_Update_Cache;

   ------------------
   -- Update_Cache --
   ------------------

   procedure Update_Cache
      (Self   : Object;
       Name   : Q_Attribute_Id;
       Index  : Project.Attribute_Index.Object := Attribute_Index.Undefined;
       At_Pos : Unit_Index                     := No_Index;
       Attr   : GPR2.Project.Attribute.Object)
   is
      Key : constant String := Cache_Key (Name, Index, At_Pos);
   begin
      GNAT.Task_Lock.Lock;

      if Self.Inner.Enabled then
         Attribute_Cache_Maps.Include (Self.Inner.Table.all, Key, Attr);

         if Self.Inner.Needed_Extra_Capacity > 0 then
            Self.Inner.Needed_Extra_Capacity :=
              Self.Inner.Needed_Extra_Capacity - 1;
         end if;
      end if;

      GNAT.Task_Lock.Unlock;
   end Update_Cache;

end GPR2.Project.Attribute_Cache;