Skip to content

Commit 74c7bce

Browse files
committed
access: add adv chapter for idioms
- disabling deallocations - type safe idiom: access to subtype
1 parent 24ef98b commit 74c7bce

File tree

5 files changed

+99
-51
lines changed

5 files changed

+99
-51
lines changed

courses/fundamentals_of_ada/140_access_types-in_depth.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ Access Types In Depth
5757
.. include:: 140_access_types/08-memory_management.rst
5858
.. include:: 140_access_types/09-memory_debugging.rst
5959
.. include:: 140_access_types/10-memory_control.rst
60+
.. include:: 140_access_types/11-type_safe_idioms.rst
6061
.. include:: labs/140_access_types-in_depth.lab.rst
6162
.. include:: 140_access_types/99-summary_with_pools.rst

courses/fundamentals_of_ada/140_access_types/10-memory_control.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,3 @@ System.Storage_Pools Example (Partial)
123123
end if;
124124
end loop;
125125
end Deallocate;
126-

courses/fundamentals_of_ada/140_access_types/11-idiom_constant_pointer.rst

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
===========================
2+
Advanced Access Type Safety
3+
===========================
4+
5+
-----------------------------------
6+
Elaboration-Only Dynamic Allocation
7+
-----------------------------------
8+
9+
* Common in critical contexts
10+
* Rationale
11+
12+
1. We (might) need dynamically allocated date
13+
14+
- e.g. loading configuration data of unknown size
15+
16+
2. Deallocations can cause leaks, corruption
17+
18+
- |rightarrow| **Disallow** them entirely
19+
20+
3. A dynamically allocated object will needs deallocation
21+
22+
- |rightarrow| Unless it never goes out of **scope**
23+
24+
* |rightarrow| Allow only allocation onto globals
25+
26+
.. tip::
27+
28+
And restrict allocations to program elaboration
29+
30+
--------------------------
31+
Prevent Heap Deallocations
32+
--------------------------
33+
34+
* :ada:`Ada.Unchecked_Deallocation` cannot be used anymore
35+
* No heap deallocation is possible
36+
37+
- The total number of allocations should be bounded
38+
- e.g. elaboration-only allocations
39+
40+
.. code:: Ada
41+
42+
pragma Restrictions
43+
(No_Dependence => Unchecked_Deallocation);
44+
45+
--------------------------------
46+
Constant Access at Library Level
47+
--------------------------------
48+
49+
.. code:: Ada
50+
51+
type Acc is access T;
52+
procedure Free is new Ada.Unchecked_Deallocation (T, Acc);
53+
54+
A : constant Acc := new T;
55+
56+
* :ada:`A` is :ada:`constant`
57+
58+
* Cannot be deallocated
59+
60+
-------------------------------
61+
Constant Access as Discriminant
62+
-------------------------------
63+
64+
.. code:: Ada
65+
66+
type R (A : access T) is limited record
67+
68+
* :ada:`A` is :ada:`constant`
69+
70+
* Cannot be deallocated
71+
72+
* :ada:`R` is :ada:`limited`
73+
74+
* Cannot be copied
75+
76+
------------------------
77+
Idiom: Access to Subtype
78+
------------------------
79+
80+
.. tip::
81+
82+
:ada:`subtype` improves access-related code safety
83+
84+
* Subtype constraints still apply through the access type
85+
86+
.. code:: Ada
87+
88+
type Values_T is array (Positive range <>) of Integer;
89+
subtype Two_Values_T is Values_T (1 .. 2);
90+
type Two_Values_A is access all Two_Values_T;
91+
92+
function Get return Values_T is (1 => 10);
93+
94+
-- O : aliased Two_Values_T := Get;
95+
-- Runtime FAIL: Constraint check
96+
O : aliased Values_T := Get; -- Single value, bounds are 1 .. 1
97+
-- P : Two_Values_A := O'Access;
98+
-- Compile-time FAIL: Bounds must statically match

courses/fundamentals_of_ada/700_expert_resource_management.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ Expert Resource Management
3737
.. container:: PRELUDE END
3838

3939
.. include:: 110_private_types/07-limited_private.rst
40-
.. include:: 140_access_types/11-idiom_constant_pointer.rst
4140
.. include:: 260_controlled_types/10-idiom_refcounting.rst
4241
.. include:: 260_controlled_types/11-example_logger.rst
4342
.. include:: 230_interfacing_with_c/10-example_refcount_wrap.rst

0 commit comments

Comments
 (0)