From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED.2uCIJahv+a4XEBqttj5Vkw.user.gioia.aioe.org!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: General circular buffer example not tied to any specific type Date: Sun, 5 Jul 2020 20:46:56 +0200 Organization: Aioe.org NNTP Server Message-ID: References: <6ca9747e-698f-4d60-8e23-b883da5d9f38o@googlegroups.com> NNTP-Posting-Host: 2uCIJahv+a4XEBqttj5Vkw.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 X-Notice: Filtered by postfilter v. 0.9.2 Content-Language: en-US Xref: reader01.eternal-september.org comp.lang.ada:59349 List-Id: On 05/07/2020 17:36, Simon Wright wrote: > "Jeffrey R. Carter" writes: > >> On 7/5/20 11:52 AM, Simon Wright wrote: >>> Simon Wright writes: >>> >>>> For the Size, see ARM 13.13.2(2). >>> >>> 13.13.2(1.2), sorry >> >> That only works for elementary types. For any type, a better approach >> is generic package Ada.Storage_IO, ARM A.9 >> (http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-9.html). > > As you say, this has the advantage of actually working. I see no difference. In both cases some kind of type tag need to be written and the object's size with it if not derived from the tag. Whether stream Read/Write or storage elements copy is then used changes little. P.S. I never used Storage_IO, I did storage pools instead. With a ring buffer you go as follows. Make a LIFO of Storage_Elements. It could be a descendant of Root_Storage_Pool or else if true parent type is already spent (Ada has no full multiple inheritance) use an access discriminant in a mix-in: type LIFO_Pool (Buffer : not null access LIFO'Class) with null record; The implementation of Allocate with reserve place in Buffer. Allocate already has parameter Size_In_Storage_Elements. You should only pay attention to Alignment and to atomic kicking off old elements when new allocation overlaps old elements in the buffer. So the element sizes must be stored in the LIFO. Writing into the LIFO will become: procedure Push (Buffer : in out LIFO'Class S : String) is Pool : LIFO_Pool (Buffer'Unchecked_Access); type String_Ptr is access String; for String_Ptr'Storage_Pool use Pool; Ptr : String_Ptr; begin Ptr := new String'("Some text"); end Push; P.P.S. Beware Ada finalization design bug ARM 7.6.1 (11.1/3): no controlled types, also. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de