Ada: named access types and memory pools

222 Views Asked by At

I have read the following from wikibooks:

A pool access type handles accesses to objects which were created on some specific heap (or storage pool as it is called in Ada). A pointer of these types cannot point to a stack or library level (static) object or an object in a different storage pool. Therefore, conversion between pool access types is illegal. (Unchecked_Conversion may be used, but note that deallocation via an access object with a storage pool different from the one it was allocated with is erroneous.)

According the bold text, if the named access types belongs to the same memory pool then the conversion is legal?

I'm implementing a composite pattern, and I think I could improve the design if the composites return references to its concrete leaves and composites, avoiding the use of keyword "all" in the named access definition. I think I need memory pools to accomplish with it, but I think that it is an advanced feature and I haven't found enough documentation to be sure I can implement one by myself correctly.

I have been following the links shared in this post. Does anybody know other resources after ten years?

1

There are 1 best solutions below

0
Shark8 On

Hm, given the description, it seems like subpools would work well for this: have a subpooled-pool "for the composite type", where each component has a subpool.

But first, if there is any confusion as to what storage-pools are, or how to use them in Ada, it is an excellent exercise to implement one, and in the case of subpools its interface is given in System.Storage_Pools.Subpools.

package Example is
  Type Kpool is new System.Storage_Pools.Subpools.Root_Storage_Pool_With_Subpools
  with null record;


  overriding procedure Allocate
    (Pool                     : in out Kpool;
     Storage_Address          : out System.Address;
     Size_In_Storage_Elements : System.Storage_Elements.Storage_Count;
     Alignment                : System.Storage_Elements.Storage_Count) is null;

  overriding procedure Allocate_From_Subpool
    (Pool                     : in out Kpool;
     Storage_Address          : out System.Address;
     Size_In_Storage_Elements : System.Storage_Elements.Storage_Count;
     Alignment                : System.Storage_Elements.Storage_Count;
     Subpool                  : not null System.Storage_Pools.Subpools.Subpool_Handle) is null;

  overriding function Create_Subpool
    (Pool : in out Kpool)
     return not null System.Storage_Pools.Subpools.Subpool_Handle is
    (raise Program_Error);

  overriding procedure Deallocate
    (Pool                     : in out Kpool;
     Storage_Address          : System.Address;
     Size_In_Storage_Elements : System.Storage_Elements.Storage_Count;
     Alignment                : System.Storage_Elements.Storage_Count)
  is null;

  overriding procedure Deallocate_Subpool
    (Pool    : in out Kpool;
     Subpool : in out System.Storage_Pools.Subpools.Subpool_Handle)
  is null;
End Example;

----------
-- BODY --
----------
Package Body Example is
  -- Put implementations here.
End Example;

You can use Ada.Text_IO.Put_Line to "see" the data being passed, and from there figure out what you need to do with your data. (A good exercise is to make the pool hold an array of Storage_Element via discriminated record, and from there do the memory-management using those bytes.)