| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . M E M O R Y -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2001-2025, Free Software Foundation, Inc. -- |
| 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- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 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 -- |
| 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/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- This is the default implementation of this package |
| 33 |
|
| 34 |
-- This implementation assumes that the underlying malloc/free/realloc |
| 35 |
-- implementation is thread safe, and thus, no additional lock is required. |
| 36 |
-- Note that when using sjlj exception handling, we still need to defer abort |
| 37 |
-- because an asynchronous signal (as used for implementing asynchronous abort |
| 38 |
-- of task on sjlj runtimes) cannot safely be handled while malloc is |
| 39 |
-- executing. |
| 40 |
|
| 41 |
with System.CRTL; |
| 42 |
with System.Parameters; |
| 43 |
with System.Soft_Links; |
| 44 |
|
| 45 |
package body System.Memory is |
| 46 |
|
| 47 |
use System.Soft_Links; |
| 48 |
|
| 49 |
function c_malloc (Size : System.CRTL.size_t) return System.Address |
| 50 |
renames System.CRTL.malloc; |
| 51 |
|
| 52 |
procedure c_free (Ptr : System.Address) |
| 53 |
renames System.CRTL.free; |
| 54 |
|
| 55 |
function c_realloc |
| 56 |
(Ptr : System.Address; Size : System.CRTL.size_t) return System.Address |
| 57 |
renames System.CRTL.realloc; |
| 58 |
|
| 59 |
----------- |
| 60 |
-- Alloc -- |
| 61 |
----------- |
| 62 |
|
| 63 |
function Alloc (Size : size_t) return System.Address is |
| 64 |
Result : System.Address; |
| 65 |
begin |
| 66 |
-- A previous version moved the check for size_t'Last below, into the |
| 67 |
-- "if Result = System.Null_Address...". So malloc(size_t'Last) should |
| 68 |
-- return Null_Address, and then we can check for that special value. |
| 69 |
-- However, that doesn't work on VxWorks, because malloc(size_t'Last) |
| 70 |
-- prints an unwanted warning message before returning Null_Address. |
| 71 |
-- Note that the branch is correctly predicted on modern hardware, so |
| 72 |
-- there is negligible overhead. |
| 73 |
|
| 74 |
if Size = size_t'Last then |
| 75 |
raise Storage_Error with "object too large"; |
| 76 |
end if; |
| 77 |
|
| 78 |
if ZCX_By_Default or else Parameters.No_Abort then |
| 79 |
Result := c_malloc (System.CRTL.size_t (Size)); |
| 80 |
else |
| 81 |
Abort_Defer.all; |
| 82 |
Result := c_malloc (System.CRTL.size_t (Size)); |
| 83 |
Abort_Undefer.all; |
| 84 |
end if; |
| 85 |
|
| 86 |
if Result = System.Null_Address then |
| 87 |
|
| 88 |
-- If Size = 0, we can't allocate 0 bytes, because then two different |
| 89 |
-- allocators, one of which has Size = 0, could return pointers that |
| 90 |
-- compare equal, which is wrong. (Nonnull pointers compare equal if |
| 91 |
-- and only if they designate the same object, and two different |
| 92 |
-- allocators allocate two different objects). |
| 93 |
|
| 94 |
-- malloc(0) is defined to allocate a non-zero-sized object (in which |
| 95 |
-- case we won't get here, and all is well) or NULL, in which case we |
| 96 |
-- get here. We also get here in case of error. So check for the |
| 97 |
-- zero-size case, and allocate 1 byte. Otherwise, raise |
| 98 |
-- Storage_Error. |
| 99 |
|
| 100 |
-- We check for zero size here, rather than at the start, for |
| 101 |
-- efficiency. |
| 102 |
|
| 103 |
if Size = 0 then |
| 104 |
return Alloc (1); |
| 105 |
end if; |
| 106 |
|
| 107 |
raise Storage_Error with "heap exhausted"; |
| 108 |
end if; |
| 109 |
|
| 110 |
return Result; |
| 111 |
end Alloc; |
| 112 |
|
| 113 |
---------- |
| 114 |
-- Free -- |
| 115 |
---------- |
| 116 |
|
| 117 |
procedure Free (Ptr : System.Address) is |
| 118 |
begin |
| 119 |
if ZCX_By_Default or else Parameters.No_Abort then |
| 120 |
c_free (Ptr); |
| 121 |
else |
| 122 |
Abort_Defer.all; |
| 123 |
c_free (Ptr); |
| 124 |
Abort_Undefer.all; |
| 125 |
end if; |
| 126 |
end Free; |
| 127 |
|
| 128 |
------------- |
| 129 |
-- Realloc -- |
| 130 |
------------- |
| 131 |
|
| 132 |
function Realloc |
| 133 |
(Ptr : System.Address; |
| 134 |
Size : size_t) |
| 135 |
return System.Address |
| 136 |
is |
| 137 |
Result : System.Address; |
| 138 |
begin |
| 139 |
if Size = size_t'Last then |
| 140 |
raise Storage_Error with "object too large"; |
| 141 |
end if; |
| 142 |
|
| 143 |
if ZCX_By_Default or else Parameters.No_Abort then |
| 144 |
Result := c_realloc (Ptr, System.CRTL.size_t (Size)); |
| 145 |
else |
| 146 |
Abort_Defer.all; |
| 147 |
Result := c_realloc (Ptr, System.CRTL.size_t (Size)); |
| 148 |
Abort_Undefer.all; |
| 149 |
end if; |
| 150 |
|
| 151 |
if Result = System.Null_Address then |
| 152 |
raise Storage_Error with "heap exhausted"; |
| 153 |
end if; |
| 154 |
|
| 155 |
return Result; |
| 156 |
end Realloc; |
| 157 |
|
| 158 |
end System.Memory; |