File: gnatcoll-storage_pools-headers.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 (144 lines) | stat: -rw-r--r-- 5,998 bytes parent folder | download
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
------------------------------------------------------------------------------
--                             G N A T C O L L                              --
--                                                                          --
--                     Copyright (C) 2015-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 System.Memory;           use System, System.Memory;

package body GNATCOLL.Storage_Pools.Headers is

   Default_Align : constant Storage_Count :=
      Standard'System_Allocator_Alignment;

   ------------------
   -- Header_Pools --
   ------------------

   package body Header_Pools is
      type Header is record
         Extra : Extra_Header;
      end record;
      type Extra_Header_Access is access all Extra_Header;

      Extra_Bytes  : constant Storage_Offset :=
         (Header'Max_Size_In_Storage_Elements
          - Header'Object_Size / Storage_Unit);
      --  If the header is a controlled type, we need to allocate extra size
      --  for its Previous and Next pointers. This constant computes how
      --  much such extra size is needed.

      Header_Size_Bytes : constant Storage_Count :=
         Header'Size / Storage_Unit;

      Extra_Allocation_Bytes : constant Storage_Count :=
         ((Header_Size_Bytes + Extra_Bytes + Default_Align - 1)
          / Default_Align) * Default_Align;
      --   Allocate a multiple of Default_Align bytes, so that the
      --   alignment of the Element_Type is suitable.

      function Convert is new Ada.Unchecked_Conversion
         (System.Address, Extra_Header_Access);

      function Address_Header_Of
        (Addr : System.Address) return System.Address
      is (Addr - Extra_Allocation_Bytes);
      --  Compute the address of the header.
      --  Do not call with a null pointer.

      --------------
      -- Allocate --
      --------------

      overriding procedure Allocate
         (Self      : in out Header_Pool;
          Addr      : out System.Address;
          Size      : System.Storage_Elements.Storage_Count;
          Alignment : System.Storage_Elements.Storage_Count)
      is
         --  The compiler requests a size that include the object size
         --  plus any extra header like bounds or next/previous for
         --  controlled types. This size also includes a padding to
         --  ensure that the element will be properly aligned.
         --  The computation is done in s-stposu.adb, in
         --  Header_Size_With_Padding.

         pragma Unreferenced (Self, Alignment);
         Aligned_Size : constant Storage_Count :=   --  bytes
            Size + Extra_Allocation_Bytes;
         Allocated : constant System.Address :=
            Alloc (size_t (Aligned_Size));
      begin
         Addr := Allocated + Extra_Allocation_Bytes;
      end Allocate;

      ----------------
      -- Deallocate --
      ----------------

      overriding procedure Deallocate
         (Self      : in out Header_Pool;
          Addr      : System.Address;
          Size      : System.Storage_Elements.Storage_Count;
          Alignment : System.Storage_Elements.Storage_Count)
      is
         pragma Unreferenced (Self, Alignment, Size);
         Header : constant System.Address := Address_Header_Of (Addr);
      begin
         System.Memory.Free (Header);
      end Deallocate;

      -----------
      -- Typed --
      -----------

      package body Typed is

         function Header_Of
            (Element : Element_Access) return access Extra_Header
         is
            F : constant Integer := Element.all'Finalization_Size;
            --  If the element_type is a controlled type, this constant will
            --  be the number of extra bytes requested by the compiler in
            --  calls to Allocate and Deallocate (see the memory layout
            --  description in the specs).
            --
            --  These extra bytes are automatically added and substracted by
            --  the compiler when calling Deallocate, but not when calling
            --  Header_Of so we need to take them into account when looking
            --  for the our own header.

            H : constant access Extra_Header :=
               (if Element = null
                then null
                else Convert
                   (Address_Header_Of
                      (Element.all'Address
                       - Storage_Offset (F)
                       - Element_Type'Descriptor_Size)));
         begin
            return H;
         end Header_Of;

      end Typed;

   end Header_Pools;

end GNATCOLL.Storage_Pools.Headers;