Skip to content

Commit 415f663

Browse files
QuentinOchemtonunaks
authored andcommitted
initial revision of importing array from addresses
1 parent 06b736e commit 415f663

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
- Feature Name: Import Array from Address
2+
- Start Date: 2025-05-06
3+
- RFC PR: (leave this empty)
4+
- RFC Issue: (leave this empty)
5+
6+
Summary
7+
=======
8+
9+
This proposal introduces the possibility of dynamically creating an access to
10+
and Ada unconstrained array from an address and its boundaries.
11+
12+
Motivation
13+
==========
14+
15+
It is difficult today to create access to arrays created from outside of Ada,
16+
e.g. when coming from C, as there's no way to dynamically create their
17+
boundaries. This proposal will make it possible and simplify interfacing
18+
between system data and other languages.
19+
20+
Guide-level explanation
21+
=======================
22+
23+
A new attribute is provided for arrays access types, `Address_To_Access`, which
24+
takes as parameter an address as well as the boundaries of the expected object.
25+
For example:
26+
27+
```ada
28+
type Arr is array (Integer range <>) of Integer;
29+
type Arr_Access is access all Arr;
30+
31+
function Create_C_Array_Of_Int (Size : Integer) return System.Address;
32+
pragma Import (C, Create_C_Array_Of_Int, "create_c_array_of_int);
33+
34+
V : Arr_Access := Arr_Access'Address_To_Access (Create_C_Array_Of_Int (10), 1, 10);
35+
```
36+
37+
This attribute is available for all arrays. Constrained array do not require
38+
boundaries to be provided. Multi-dimenstional arrays will need dimensions to
39+
be provided in order, and fixed lower bound only require one dimension. For
40+
example:
41+
42+
```ada
43+
type Constrained_Array is array (Integer range 1 .. 10) of Integer;
44+
type Constrained_Array_Access is access all Constrained_Array;
45+
46+
type 2D_Array is array (Integer range <>, Integer range <>) of Integer;
47+
type 2D_Array_Access is access all 2D_Array;
48+
49+
type FLB_Array is array (Integer range 1 .. <>) of Integer;
50+
type FLB_Array_Access is access all FLB_Array;
51+
52+
function Create_C_Array_Of_Int (Size : Integer) return System.Address;
53+
pragma Import (C, Create_C_Array, "create_c_array_of_int);
54+
55+
V1 : Constrained_Array_Access := Constrained_Array_Access'Address_To_Access
56+
(Create_C_Array_Of_Int (10));
57+
58+
V2 : 2D_Array_Access := 2D_Array_Access'Address_To_Access
59+
(Create_C_Array_Of_Int (100), 1, 10, 1, 10);
60+
61+
V3 : FLB_Array_Access := FLB_Array_Access'Address_To_Access
62+
(Create_C_Array_Of_Int (10), 10);
63+
```
64+
65+
Attribute parameters are named either First, Last (for the one dimension case)
66+
or First_n, Last_n (for the n dimensions case).
67+
68+
Reference-level explanation
69+
===========================
70+
71+
TBD
72+
73+
74+
Rationale and alternatives
75+
==========================
76+
77+
The attribute could have been provided on arrays, and return an anonymous
78+
access type instead. To some respect, not presuming the type of the object
79+
and not requiring the creation of an explicit access type might be better.
80+
However, this brings all accessibility issues that don't really make sense
81+
when addressing external memory, and it's most likely that these will need
82+
to be converted away anyway. Note that is accessibilty is an issue, it's still
83+
possible instead to create a local array mapped to an address:
84+
85+
```ada
86+
type Arr is array (Integer range <>) of Integer;
87+
type Arr_Access is access all Arr;
88+
89+
function Create_C_Array_Of_Int (Size : Integer) return System.Address;
90+
pragma Import (C, Create_C_Array_Of_Int, "create_c_array_of_int);
91+
92+
V : aliased Arr (1 .. 10) with Address => Create_C_Array_Of_Int (10);
93+
```
94+
95+
Drawbacks
96+
=========
97+
98+
This introduces a new "unsafe" construction in Ada, although well identify and
99+
easy to track / forbids.
100+
101+
This also adds more constraints on implementation. Some compilers implement
102+
access to arrays in a way that generates two pointers, one to data and one
103+
to bounds - which can then put at the same place when allocating for Ada. The
104+
issue then becomes the free operation - if all is allocated from Ada, it's
105+
possible to free both the data and the boundaries at the same time. However,
106+
in the example here, the address is externally provided and is not necessarily
107+
expected to be freed from the Ada side. Alternate implementation, such as
108+
putting the boundaries of the object in the pointer as opposed to indirectly
109+
refering to it, does fix this problem but requires in depth changes. Note that
110+
this is also necessary for other RFCs (such as access to array slice).
111+
112+
Prior art
113+
=========
114+
115+
Rust has a very similar problem (and solution) with the `from_raw_parts`
116+
function.
117+
118+
Unresolved questions
119+
====================
120+
121+
TBD
122+
123+
Future possibilities
124+
====================
125+
126+
TBD

0 commit comments

Comments
 (0)