File: gnatcoll-memory.adb

package info (click to toggle)
libgnatcoll 18-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,068 kB
  • sloc: ada: 40,393; python: 354; ansic: 310; makefile: 245; sh: 31
file content (323 lines) | stat: -rw-r--r-- 10,766 bytes parent folder | download | duplicates (2)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
------------------------------------------------------------------------------
--                             G N A T C O L L                              --
--                                                                          --
--                     Copyright (C) 2005-2017, AdaCore                     --
--                                                                          --
-- This library is free software;  you can redistribute it and/or modify it --
-- under terms of the  GNU General Public License  as published by the Free --
-- Software  Foundation;  either version 3,  or (at your  option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE.                            --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Exceptions;
with System.Storage_Elements;  use System.Storage_Elements;

pragma Warnings (Off);
with System.CRTL;

--  Force a user's s-memory.adb to be part of the link,
--  otherwise s-memory.o will simply be ignored by gprbuild
with System.Memory;

pragma Warnings (On);

package body GNATCOLL.Memory is

   Memory_Pool : GNAT.Debug_Pools.Debug_Pool;

   use Ada.Exceptions;

   Memory_Monitor : Boolean := False;
   Memory_Check   : Boolean := False;
   Debug_Pool_Initialization_Needed : Boolean := True;

   procedure Initialize_System_Memory_Debug_Pool
     (Has_Unhandled_Memory : Boolean := False);
   --  If not already done, let Debug_Pools know that System.Memory will use
   --  Debug_Pools, and have to handle memory allocated by System.CRTL package.

   -----------------------------------------
   -- Initialize_System_Memory_Debug_Pool --
   -----------------------------------------

   procedure Initialize_System_Memory_Debug_Pool
     (Has_Unhandled_Memory : Boolean := False) is
   begin
      if Debug_Pool_Initialization_Needed then
         Debug_Pool_Initialization_Needed := False;
         GNAT.Debug_Pools.System_Memory_Debug_Pool (Has_Unhandled_Memory);
      end if;
   end Initialize_System_Memory_Debug_Pool;

   -----------
   -- Alloc --
   -----------

   function Alloc (Size : size_t) return System.Address is
      Result      : System.Address;
      Actual_Size : size_t := Size;
   begin

      if Size = size_t'Last then
         Raise_Exception (Storage_Error'Identity, "object too large");
      end if;

      --  Change size from zero to non-zero. We still want a proper pointer
      --  for the zero case because pointers to zero length objects have to
      --  be distinct, but we can't just go ahead and allocate zero bytes,
      --  since some malloc's return zero for a zero argument.

      if Size = 0 then
         Actual_Size := 1;
      end if;

      if Memory_Monitor then

         Initialize_System_Memory_Debug_Pool;

         Memory_Pool.Allocate
           (Storage_Address          => Result,
            Size_In_Storage_Elements => Storage_Count (Actual_Size),
            Alignment                => Standard'Maximum_Alignment);

      else
         Result := System.CRTL.malloc (System.CRTL.size_t (Actual_Size));
      end if;

      if Result = System.Null_Address then
         Raise_Exception (Storage_Error'Identity, "heap exhausted");
      end if;

      return Result;
   end Alloc;

   ----------
   -- Free --
   ----------

   procedure Free (Ptr : System.Address) is
   begin

      if Ptr /= System.Null_Address and not Memory_Check then
         if Memory_Monitor then

            Initialize_System_Memory_Debug_Pool;

            Memory_Pool.Deallocate
              (Storage_Address          => Ptr,
               Size_In_Storage_Elements => Storage_Count'Last,
               Alignment                => Standard'Maximum_Alignment);
         else
            System.CRTL.free (Ptr);
         end if;

      end if;
   end Free;

   -------------
   -- Realloc --
   -------------

   function Realloc
     (Ptr  : System.Address;
      Size : size_t)
      return System.Address
   is
      Result      : System.Address;
      Actual_Size : size_t := Size;
   begin

      if Size = size_t'Last then
         Raise_Exception (Storage_Error'Identity, "object too large");
      end if;

      if not Memory_Monitor then
         Result := System.CRTL.realloc (Ptr, System.CRTL.size_t (Actual_Size));
      else
         declare
            Size_Was    : Storage_Count;
            Valid       : Boolean;

            procedure Memmove
              (Dest : System.Address;
               Src  : System.Address;
               N    : size_t);
            pragma Import (C, Memmove, "memmove");

         begin

            Initialize_System_Memory_Debug_Pool;

            Get_Size
              (Storage_Address          => Ptr,
               Size_In_Storage_Elements => Size_Was,
               Valid                    => Valid);
            if not Valid then
               declare
                  Reallocated : System.Address;
               begin
                  Reallocated := System.CRTL.realloc
                    (Ptr, System.CRTL.size_t (Actual_Size));
                  Memory_Pool.Allocate
                    (Storage_Address          => Result,
                     Size_In_Storage_Elements => Storage_Count (Actual_Size),
                     Alignment                => Standard'Maximum_Alignment);
                  Memmove
                   (Dest => Result,
                    Src  => Reallocated,
                    N    => Actual_Size);
                  System.CRTL.free (Reallocated);
               end;
            else
               Memory_Pool.Allocate
                 (Storage_Address          => Result,
                  Size_In_Storage_Elements => Storage_Count (Size),
                  Alignment                => Standard'Maximum_Alignment);
               if size_t (Size_Was) < Actual_Size then
                  Actual_Size := size_t (Size_Was);
               end if;
               Memmove
                 (Dest => Result,
                  Src  => Ptr,
                  N    => Actual_Size);
               Free (Ptr);
            end if;

         end;

      end if;

      if Result = System.Null_Address then
         Raise_Exception (Storage_Error'Identity, "heap exhausted");
      end if;

      return Result;
   end Realloc;

   -----------------------
   -- Redirectable_Dump --
   -----------------------

   procedure Redirectable_Dump
     (Size   : Positive;
      Report : Report_Type := All_Reports) is
      procedure Redirected_Dump is new GNAT.Debug_Pools.Dump
        (Put_Line => Put_Line,
         Put      => Put);
   begin
      Redirected_Dump
        (Memory_Pool,
         Size   => Size,
         Report => GNAT.Debug_Pools.Report_Type (Report));
   end Redirectable_Dump;

   procedure Dump (Size : Positive; Report : Report_Type := All_Reports) is
   begin

      Memory_Pool.Dump_Stdout
        (Size   => Size,
         Report => GNAT.Debug_Pools.Report_Type (Report));
   end Dump;

   -------------------------
   -- Get_Ada_Allocations --
   -------------------------

   function Get_Ada_Allocations return Watermark_Info is
   begin
      return
        (High    => GNATCOLL.Memory.Byte_Count (Memory_Pool.High_Water_Mark),
         Current => GNATCOLL.Memory.Byte_Count (Memory_Pool.Current_Water_Mark)
        );
   end Get_Ada_Allocations;

   ---------------------
   -- Get_Allocations --
   ---------------------

   function Get_Allocations return Watermark_Info is
      function Get_Peak_RSS return size_t;
      pragma Import (C, Get_Peak_RSS, "gnatcoll_getPeakRSS");

      function Get_Current_RSS return size_t;
      pragma Import (C, Get_Current_RSS, "gnatcoll_getCurrentRSS");

   begin
      return (High    => Byte_Count (Get_Peak_RSS),
              Current => Byte_Count (Get_Current_RSS));
   end Get_Allocations;

   -----------
   -- Reset --
   -----------

   procedure Reset is
   begin
      GNAT.Debug_Pools.Reset;
   end Reset;

   ---------------
   -- Configure --
   ---------------

   procedure Configure
     (Activate_Monitor               : Boolean           := False;
      Disable_Free                   : Boolean           := False;
      Stack_Trace_Depth              : Natural           := 30;
      Maximum_Logically_Freed_Memory : Long_Long_Integer := 50_000_000;
      Minimum_To_Free                : Long_Long_Integer  := 0;
      Reset_Content_On_Free          : Boolean           := True;
      Raise_Exceptions               : Boolean           := False;
      Advanced_Scanning              : Boolean           := False;
      Errors_To_Stdout               : Boolean           := True;
      Low_Level_Traces               : Boolean           := False) is
   begin

      Memory_Check := Disable_Free;

      if Activate_Monitor and not Memory_Monitor then
         Initialize_System_Memory_Debug_Pool
           (Has_Unhandled_Memory => True);
         Memory_Monitor := True;
      end if;

      if Memory_Monitor then

         Memory_Pool.Configure
           (Stack_Trace_Depth              => Stack_Trace_Depth,
            Maximum_Logically_Freed_Memory =>
              SSC (Maximum_Logically_Freed_Memory),
            Minimum_To_Free                => SSC (Minimum_To_Free + 1),
            Reset_Content_On_Free          => Reset_Content_On_Free,
            Raise_Exceptions               => Raise_Exceptions,
            Advanced_Scanning              => Advanced_Scanning,
            Errors_To_Stdout               => Errors_To_Stdout,
            Low_Level_Traces               => Low_Level_Traces);
      end if;

   end Configure;

   --------------------
   -- Mark_Traceback --
   --------------------

   procedure Mark_Traceback is
      Size_Was : Byte_Count;
      pragma Unreferenced (Size_Was);
   begin
      null;
   end Mark_Traceback;

end GNATCOLL.Memory;