Robert AH Prins wrote:
>> Actually, the sensible way is to use REFER; that's what it's for, and it avoids
>> nasty problems like, "How many bytes in a pointer?"
>
> Maybe now that more access to REFER based structures is being in-lined, yes, but REFER used to be an absolute killer as
> far as performance was considered.
>
> One of the changes I made at a client in 1996 (which in combination saved them around eur 120,000 per year) was to pass
> "BASED REFER" structures to a procedure where they were declared with '*' extents. Peter Elderon almost verbatim copied
> my suggestion to do so to save CPU into chapter 13 of the PL/I Programming Guide.
>
I think you are missing the point on the use of refer. The extra overhead you are objecting to only occurs if you access
the allocated storage using the declaration used to allocate it. The following function is equivalent for all intents
and purposes to what you want:
alloc: proc(a,n) returns(ptr);
dcl
a area, n bin fixed(31) byvalue, p ptr,
1 space based(p),
2 amt bin fixed(31)
2 bytes(n refer(amt)) char(1);
n-=4; allocate space in(a); return(p);
end alloc;
After executing p=alloc(a,n); you can use the n bytes pointed at by p using whatever based declaration you wish just as
you would after having allocated n bytes using the allocate built in function.
Emptying the area will free everything in one fell swoop as you want. If you should want to free some of the storage
piecemeal, you would have to restore the correct value of p->amt before executing free p->space in(a) or all hell would
break loose.
In Personal PL/I for OS/2 and, I suspect, in the other workstation implementations as well, storage of whatever class
allocated by the allocate statement as well as storage allocated by the allocate built in function is taken from the
heap. Heap allocation has an allocation granularity of 16 bytes and a hidden overhead of 16 bytes and is always
paragraph aligned. Controlled storage has additional hidden overhead. allocate(n) consumes 16+16*ceil(n/16) bytes. On
the other hand, based variable allocation within an area using the allocate statement has an allocation granularity of 8
and no hidden overhead. Thus allocation in an area of an item that requires n bytes consumes 8*ceil(n/8) bytes of the
area. It is the 16 hidden overhead bytes on the heap that enables plifree(p) to work. There is no way your suggestion
could be implemented without a major redesign that would affect the behavior of existing programs.