]>
Commit | Line | Data |
---|---|---|
cacbc350 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT RUN-TIME COMPONENTS -- | |
4 | -- -- | |
5 | -- S Y S T E M . M E M O R Y -- | |
6 | -- -- | |
fbf5a39b | 7 | -- B o d y -- |
cacbc350 | 8 | -- -- |
748086b7 | 9 | -- Copyright (C) 2001-2009, Free Software Foundation, Inc. -- |
cacbc350 RK |
10 | -- -- |
11 | -- GNAT is free software; you can redistribute it and/or modify it under -- | |
12 | -- terms of the GNU General Public License as published by the Free Soft- -- | |
748086b7 | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
cacbc350 RK |
14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
748086b7 JJ |
16 | -- or FITNESS FOR A PARTICULAR PURPOSE. -- |
17 | -- -- | |
18 | -- As a special exception under Section 7 of GPL version 3, you are granted -- | |
19 | -- additional permissions described in the GCC Runtime Library Exception, -- | |
20 | -- version 3.1, as published by the Free Software Foundation. -- | |
21 | -- -- | |
22 | -- You should have received a copy of the GNU General Public License and -- | |
23 | -- a copy of the GCC Runtime Library Exception along with this program; -- | |
24 | -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- | |
25 | -- <http://www.gnu.org/licenses/>. -- | |
cacbc350 RK |
26 | -- -- |
27 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
71ff80dc | 28 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
cacbc350 RK |
29 | -- -- |
30 | ------------------------------------------------------------------------------ | |
31 | ||
bfc8aa81 | 32 | -- This is the default implementation of this package |
cacbc350 RK |
33 | |
34 | -- This implementation assumes that the underlying malloc/free/realloc | |
35 | -- implementation is thread safe, and thus, no additional lock is required. | |
1a49cf99 AC |
36 | -- Note that we still need to defer abort because on most systems, an |
37 | -- asynchronous signal (as used for implementing asynchronous abort of | |
38 | -- task) cannot safely be handled while malloc is executing. | |
cacbc350 | 39 | |
1a49cf99 AC |
40 | -- If you are not using Ada constructs containing the "abort" keyword, then |
41 | -- you can remove the calls to Abort_Defer.all and Abort_Undefer.all from | |
42 | -- this unit. | |
cacbc350 | 43 | |
2d9ea47f RD |
44 | pragma Warnings (Off); |
45 | pragma Compiler_Unit; | |
46 | pragma Warnings (On); | |
47 | ||
cacbc350 RK |
48 | with Ada.Exceptions; |
49 | with System.Soft_Links; | |
07fc65c4 | 50 | with System.Parameters; |
209db2bf | 51 | with System.CRTL; |
cacbc350 RK |
52 | |
53 | package body System.Memory is | |
54 | ||
55 | use Ada.Exceptions; | |
56 | use System.Soft_Links; | |
57 | ||
209db2bf AC |
58 | function c_malloc (Size : System.CRTL.size_t) return System.Address |
59 | renames System.CRTL.malloc; | |
cacbc350 | 60 | |
209db2bf AC |
61 | procedure c_free (Ptr : System.Address) |
62 | renames System.CRTL.free; | |
cacbc350 RK |
63 | |
64 | function c_realloc | |
209db2bf AC |
65 | (Ptr : System.Address; Size : System.CRTL.size_t) return System.Address |
66 | renames System.CRTL.realloc; | |
cacbc350 RK |
67 | |
68 | ----------- | |
69 | -- Alloc -- | |
70 | ----------- | |
71 | ||
72 | function Alloc (Size : size_t) return System.Address is | |
73 | Result : System.Address; | |
74 | Actual_Size : size_t := Size; | |
75 | ||
76 | begin | |
77 | if Size = size_t'Last then | |
78 | Raise_Exception (Storage_Error'Identity, "object too large"); | |
79 | end if; | |
80 | ||
81 | -- Change size from zero to non-zero. We still want a proper pointer | |
82 | -- for the zero case because pointers to zero length objects have to | |
83 | -- be distinct, but we can't just go ahead and allocate zero bytes, | |
84 | -- since some malloc's return zero for a zero argument. | |
85 | ||
86 | if Size = 0 then | |
87 | Actual_Size := 1; | |
88 | end if; | |
89 | ||
07fc65c4 | 90 | if Parameters.No_Abort then |
209db2bf | 91 | Result := c_malloc (System.CRTL.size_t (Actual_Size)); |
07fc65c4 GB |
92 | else |
93 | Abort_Defer.all; | |
209db2bf | 94 | Result := c_malloc (System.CRTL.size_t (Actual_Size)); |
07fc65c4 GB |
95 | Abort_Undefer.all; |
96 | end if; | |
cacbc350 RK |
97 | |
98 | if Result = System.Null_Address then | |
99 | Raise_Exception (Storage_Error'Identity, "heap exhausted"); | |
100 | end if; | |
101 | ||
102 | return Result; | |
103 | end Alloc; | |
104 | ||
105 | ---------- | |
106 | -- Free -- | |
107 | ---------- | |
108 | ||
109 | procedure Free (Ptr : System.Address) is | |
110 | begin | |
07fc65c4 GB |
111 | if Parameters.No_Abort then |
112 | c_free (Ptr); | |
113 | else | |
114 | Abort_Defer.all; | |
115 | c_free (Ptr); | |
116 | Abort_Undefer.all; | |
117 | end if; | |
cacbc350 RK |
118 | end Free; |
119 | ||
120 | ------------- | |
121 | -- Realloc -- | |
122 | ------------- | |
123 | ||
124 | function Realloc | |
125 | (Ptr : System.Address; | |
126 | Size : size_t) | |
127 | return System.Address | |
128 | is | |
129 | Result : System.Address; | |
fbf5a39b | 130 | Actual_Size : constant size_t := Size; |
cacbc350 RK |
131 | |
132 | begin | |
133 | if Size = size_t'Last then | |
134 | Raise_Exception (Storage_Error'Identity, "object too large"); | |
135 | end if; | |
136 | ||
07fc65c4 | 137 | if Parameters.No_Abort then |
209db2bf | 138 | Result := c_realloc (Ptr, System.CRTL.size_t (Actual_Size)); |
07fc65c4 GB |
139 | else |
140 | Abort_Defer.all; | |
209db2bf | 141 | Result := c_realloc (Ptr, System.CRTL.size_t (Actual_Size)); |
07fc65c4 GB |
142 | Abort_Undefer.all; |
143 | end if; | |
cacbc350 RK |
144 | |
145 | if Result = System.Null_Address then | |
146 | Raise_Exception (Storage_Error'Identity, "heap exhausted"); | |
147 | end if; | |
148 | ||
149 | return Result; | |
150 | end Realloc; | |
151 | ||
152 | end System.Memory; |