File : s-memory-zfp.adb
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-2011, 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 -- --
19 -- --
20 -- --
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 -- Simple implementation for use with ZFP
33
34 with System.Storage_Elements;
35 with Unchecked_Conversion;
36
37 package body System.Memory is
38
39 package SSE renames System.Storage_Elements;
40
41 Default_Size : constant := 20 * 1_024;
42
43 type Mark_Id is new SSE.Integer_Address;
44
45 type Memory is array (Mark_Id range <>) of SSE.Storage_Element;
46 for Memory'Alignment use Standard'Maximum_Alignment;
47
48 Mem : Memory (1 .. Default_Size);
49
50 Top : Mark_Id := Mem'First;
51
52 function To_Mark_Id is new Unchecked_Conversion
53 (size_t, Mark_Id);
54
55 ----------------
56 -- For C code --
57 ----------------
58
59 function Malloc (Size : size_t) return System.Address;
60 pragma Export (C, Malloc, "malloc");
61
62 function Calloc (N_Elem : size_t; Elem_Size : size_t) return System.Address;
63 pragma Export (C, Calloc, "calloc");
64
65 procedure Free (Ptr : System.Address);
66 pragma Export (C, Free, "free");
67
68 -----------
69 -- Alloc --
70 -----------
71
72 function Alloc (Size : size_t) return System.Address is
73 Max_Align : constant Mark_Id := Mark_Id (Standard'Maximum_Alignment);
74 Max_Size : Mark_Id :=
75 ((To_Mark_Id (Size) + Max_Align - 1) / Max_Align) *
76 Max_Align;
77 Location : constant Mark_Id := Top;
78
79 begin
80 if Max_Size = 0 then
81 Max_Size := Max_Align;
82 end if;
83
84 if Size = size_t'Last then
85 raise Storage_Error;
86 end if;
87
88 Top := Top + Max_Size;
89
90 if Top > Default_Size then
91 raise Storage_Error;
92 end if;
93
94 return Mem (Location)'Address;
95 end Alloc;
96
97 ------------
98 -- Malloc --
99 ------------
100
101 function Malloc (Size : size_t) return System.Address is
102 begin
103 return Alloc (Size);
104 end Malloc;
105
106 ------------
107 -- Calloc --
108 ------------
109
110 function Calloc
111 (N_Elem : size_t; Elem_Size : size_t) return System.Address
112 is
113 begin
114 return Malloc (N_Elem * Elem_Size);
115 end Calloc;
116
117 ----------
118 -- Free --
119 ----------
120
121 procedure Free (Ptr : System.Address) is
122 pragma Unreferenced (Ptr);
123 begin
124 null;
125 end Free;
126
127 end System.Memory;