Contents   Index   Previous   Next


4.8 Allocators

1
   [The evaluation of an allocator creates an object and yields an access value that designates the object. {new: See allocator} {malloc: See allocator} {heap management: See also allocator} ]

Syntax

2
allocator ::=
   new subtype_indication | new qualified_expression

Name Resolution Rules

3/1
     {8652/0010} {expected type (allocator) [partial]} The expected type for an allocator shall be a single access-to-object type withwhose designated type D such that either D covers the type determined by the subtype_mark of the subtype_indication or qualified_expression, or the expected type is anonymous and the determined type is D'Class.
3.a
Discussion: See 8.6, ``The Context of Overload Resolution'' for the meaning of ``shall be a single ... type whose ...''
3.a.1/1
Ramification: {8652/0010} An allocator is allowed as a controlling parameter of a dispatching call (see 3.9.2).

Legality Rules

4
   {initialized allocator} An initialized allocator is an allocator with a qualified_expression. {uninitialized allocator} An uninitialized allocator is one with a subtype_indication. In the subtype_indication of an uninitialized allocator, a constraint is permitted only if the subtype_mark denotes an [unconstrained] composite subtype; if there is no constraint, then the subtype_mark shall denote a definite subtype. {constructor: See initialized allocator}
4.a
Ramification: For example, ... new S'Class ... (with no initialization expression) is illegal, but ... new S'Class'(X) ... is legal, and takes its tag and constraints from the initial value X. (Note that the former case cannot have a constraint.)
5
   If the type of the allocator is an access-to-constant type, the allocator shall be an initialized allocator. If the designated type is limited, the allocator shall be an uninitialized allocator.
5.a
Ramification: For an access-to-constant type whose designated type is limited, allocators are illegal. The Access attribute is legal for such a type, however.

Static Semantics

6
   If the designated type of the type of the allocator is elementary, then the subtype of the created object is the designated subtype. If the designated type is composite, then the created object is always constrained; if the designated subtype is constrained, then it provides the constraint of the created object; otherwise, the object is constrained by its initial value [(even if the designated subtype is unconstrained with defaults)]. {constrained by its initial value [partial]}
6.a
Discussion: See AI83-00331.
6.b
Reason: All objects created by an allocator are aliased, and all aliased composite objects need to be constrained so that access subtypes work reasonably.

Dynamic Semantics

7
   {evaluation (allocator) [partial]} For the evaluation of an allocator, the elaboration of the subtype_indication or the evaluation of the qualified_expression is performed first. {evaluation (initialized allocator) [partial]} {assignment operation (during evaluation of an initialized allocator)} For the evaluation of an initialized allocator, an object of the designated type is created and the value of the qualified_expression is converted to the designated subtype and assigned to the object. {implicit subtype conversion (initialization expression of allocator) [partial]}
7.a
Ramification: The conversion might raise Constraint_Error.
8
   {evaluation (uninitialized allocator) [partial]} For the evaluation of an uninitialized allocator:
9
10/1
10.a
Discussion: AI83-00150.
11
    [If the created object contains any tasks, they are activated (see 9.2).] Finally, an access value that designates the created object is returned.
NOTES
12
24  Allocators cannot create objects of an abstract type. See 3.9.3.
13
25  If any part of the created object is controlled, the initialization includes calls on corresponding Initialize or Adjust procedures. See 7.6.
14
26  As explained in 13.11, ``Storage Management'', the storage for an object allocated by an allocator comes from a storage pool (possibly user defined). {Storage_Error (raised by failure of run-time check)} The exception Storage_Error is raised by an allocator if there is not enough storage. Instances of Unchecked_Deallocation may be used to explicitly reclaim storage.
15
27  Implementations are permitted, but not required, to provide garbage collection (see 13.11.3).
15.a
Ramification: Note that in an allocator, the exception Constraint_Error can be raised by the evaluation of the qualified_expression, by the elaboration of the subtype_indication, or by the initialization.
15.b
Discussion: By default, the implementation provides the storage pool. The user may exercise more control over storage management by associating a user-defined pool with an access type.

Examples

16
    Examples of allocators:
17
new Cell'(0, nullnull)                          -- initialized explicitly, see 3.10.1
new Cell'(Value => 0, Succ => null, Pred => null-- initialized explicitly
new Cell                                          -- not initialized
18
new Matrix(1 .. 10, 1 .. 20)                      -- the bounds only are given
new Matrix'(1 .. 10 => (1 .. 20 => 0.0))          -- initialized explicitly
19
new Buffer(100)                                   -- the discriminant only is given
new Buffer'(Size => 80, Pos => 0, Value => (1 .. 80 => 'A')) -- initialized explicitly
20
Expr_Ptr'(new Literal)                  -- allocator for access-to-class-wide type, see 3.9.1
Expr_Ptr'(new Literal'(Expression with 3.5))      -- initialized explicitly

Incompatibilities With Ada 83

20.a/1
{incompatibilities with Ada 83} The subtype_indication of an uninitialized allocator may not have an explicit constraint if the designated type is an access type. In Ada 83, this was permitted even though the constraint had no eaffect on the subtype of the created object.

Extensions to Ada 83

20.b
{extensions to Ada 83} Allocators creating objects of type T are now overloaded on access types designating T'Class and all class-wide types that cover T.
20.c
Implicit array subtype conversion (sliding) is now performed as part of an initialized allocator.

Wording Changes from Ada 83

20.d
We have used a new organization, inspired by the ACID document, that makes it clearer what is the subtype of the created object, and what subtype conversions take place.
20.e
Discussion of storage management issues, such as garbage collection and the raising of Storage_Error, has been moved to 13.11, ``Storage Management''.

Contents   Index   Previous   Next   Legal