1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . M E M O R Y --
9 -- Copyright (C) 2001-2003 Free Software Foundation, Inc. --
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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- This package provides the low level memory allocation/deallocation
35 -- mechanisms used by GNAT.
37 -- To provide an alternate implementation, simply recompile the modified
38 -- body of this package with gnatmake -u -a -g s-memory.adb and make sure
39 -- that the ali and object files for this unit are found in the object
42 -- This unit may be used directly from an application program by providing
43 -- an appropriate WITH, and the interface can be expected to remain stable.
45 package System
.Memory
is
46 pragma Elaborate_Body
;
48 type size_t
is mod 2 ** Standard
'Address_Size;
49 -- Note: the reason we redefine this here instead of using the
50 -- definition in Interfaces.C is that we do not want to drag in
51 -- all of Interfaces.C just because System.Memory is used.
53 function Alloc
(Size
: size_t
) return System
.Address
;
54 -- This is the low level allocation routine. Given a size in storage
55 -- units, it returns the address of a maximally aligned block of
56 -- memory. The implementation of this routine is guaranteed to be
57 -- task safe, and also aborts are deferred if necessary.
59 -- If size_t is set to size_t'Last on entry, then a Storage_Error
60 -- exception is raised with a message "object too large".
62 -- If size_t is set to zero on entry, then a minimal (but non-zero)
63 -- size block is allocated.
65 -- Note: this is roughly equivalent to the standard C malloc call
66 -- with the additional semantics as described above.
68 procedure Free
(Ptr
: System
.Address
);
69 -- This is the low level free routine. It frees a block previously
70 -- allocated with a call to Alloc. As in the case of Alloc, this
71 -- call is guaranteed task safe, and aborts are deferred.
73 -- Note: this is roughly equivalent to the standard C free call
74 -- with the additional semantics as described above.
77 (Ptr
: System
.Address
;
79 return System
.Address
;
80 -- This is the low level reallocation routine. It takes an existing
81 -- block address returned by a previous call to Alloc or Realloc,
82 -- and reallocates the block. The size can either be increased or
83 -- decreased. If possible the reallocation is done in place, so that
84 -- the returned result is the same as the value of Ptr on entry.
85 -- However, it may be necessary to relocate the block to another
86 -- address, in which case the information is copied to the new
87 -- block, and the old block is freed. The implementation of this
88 -- routine is guaranteed to be task safe, and also aborts are
89 -- deferred as necessary.
91 -- If size_t is set to size_t'Last on entry, then a Storage_Error
92 -- exception is raised with a message "object too large".
94 -- If size_t is set to zero on entry, then a minimal (but non-zero)
95 -- size block is allocated.
97 -- Note: this is roughly equivalent to the standard C realloc call
98 -- with the additional semantics as described above.
102 -- The following names are used from the generated compiler code
104 pragma Export
(C
, Alloc
, "__gnat_malloc");
105 pragma Export
(C
, Free
, "__gnat_free");
106 pragma Export
(C
, Realloc
, "__gnat_realloc");