comp.lang.ada
 help / color / mirror / Atom feed
From: Frank Buss <fb@frank-buss.de>
Subject: Re: FIFO
Date: Sat, 16 Sep 2017 20:32:34 +0200
Date: 2017-09-16T20:32:34+02:00	[thread overview]
Message-ID: <opjqo2$jo2$1@newsreader4.netcologne.de> (raw)
In-Reply-To: <opjmkt$fp6$1@gioia.aioe.org>

On 09/16/2017 07:22 PM, Dmitry A. Kazakov wrote:
> On 2017-09-16 19:12, Frank Buss wrote:
>
>> Oh well, I'll write my own package for it then. On the plus side it
>> wouldn't have all these interface stuff overhead and might be smaller
>> and faster, just a simple generic with the type and the size.
>
> You can borrow code from here:
>
>    http://www.dmitry-kazakov.de/ada/components.htm#Generic_FIFO

Thanks, looks similar. I tried my own version, without looking at your 
version :-) Would this work for interrupts with the volatile? Good idea 
to use the size generic argument inside the package.

And everyone feel free to criticize my code, as I'm very new to Ada. For 
example, I saw that when you want to package something neatly, but it 
consists mainly of one type, then the package is named plural of the 
type, like I did: "Ringbuffers" and "Ringbuffer" for the type. Is this 
common practice?

For testing I wrote a small program. How do Ada programmers usually test 
their libraries and programs? For Java I often use JUnit.

My version:

ringbuffers.ads:

generic
    Size : Positive;
    type Item is private;
package Ringbuffers is

    type Ringbuffer is tagged private;

    procedure Write (Self : in out Ringbuffer; e : Item);
    function Read (Self : in out Ringbuffer) return Item;
    function Is_Empty (Self : Ringbuffer) return Boolean;

private

    type Item_Array is array (1 .. Size) of Item;
    pragma Volatile (Item_Array);
    type Ringbuffer is tagged record
       Read_Index  : Positive := 1;
       Write_Index : Positive := 1;
       Items       : Item_Array;
    end record;
    pragma Volatile (Ringbuffer);

end Ringbuffers;


ringbuffers.adb:

package body Ringbuffers is

    procedure Write (Self : in out Ringbuffer; e : Item) is
    begin
       Self.Items (Self.Write_Index) := e;
       Self.Write_Index := Self.Write_Index + 1;
       if (Self.Write_Index = Size) then
          Self.Write_Index := 1;
       end if;
    end Write;

    function Read (Self : in out Ringbuffer) return Item is
       Result : Item;
    begin
       Result := Self.Items (Self.Read_Index);
       Self.Read_Index := Self.Read_Index + 1;
       if (Self.Read_Index = Size) then
          Self.Read_Index := 1;
       end if;
       return Result;
    end Read;

    function Is_Empty (Self : Ringbuffer) return Boolean is
    begin
       return Self.Write_Index = Self.Read_Index;
    end Is_Empty;

end Ringbuffers;


test_ringbuffer.adb:

with Interfaces;  use Interfaces;
with Ada.Text_IO; use Ada.Text_IO;
with Ringbuffers;

procedure Test_Ringbuffer is
    package FIFO_Package is new Ringbuffers (256, Unsigned_8);
    subtype FIFO is FIFO_Package.Ringbuffer;
    Test : FIFO;

begin
    Test.Write (1);
    Test.Write (7);
    while not Test.Is_Empty loop
       Put_Line (Unsigned_8'Image (Test.Read));
    end loop;
end Test_Ringbuffer;


-- 
Frank Buss, http://www.frank-buss.de
electronics and more: http://www.youtube.com/user/frankbuss


  reply	other threads:[~2017-09-16 18:32 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-16 15:24 FIFO Frank Buss
2017-09-16 16:19 ` FIFO Jacob Sparre Andersen
2017-09-16 17:07   ` FIFO Dmitry A. Kazakov
2017-09-16 17:12   ` FIFO Frank Buss
2017-09-16 17:22     ` FIFO Dmitry A. Kazakov
2017-09-16 18:32       ` Frank Buss [this message]
2017-09-17  7:39         ` FIFO Jacob Sparre Andersen
2017-09-17  8:38           ` FIFO Jacob Sparre Andersen
2017-09-17  8:57           ` FIFO Niklas Holsti
2017-09-17 11:30         ` FIFO AdaMagica
     [not found]           ` <8b99f47a-63bf-4f07-9077-6dab3cf32a7f@googlegroups.com>
     [not found]             ` <oppbcq$7jj$1@newsreader4.netcologne.de>
     [not found]               ` <d37417dd-3a94-4a43-bc9c-071f2da6181d@googlegroups.com>
     [not found]                 ` <f2bs2lFq4j3U1@mid.individual.net>
     [not found]                   ` <opqq32$3bd$1@newsreader4.netcologne.de>
2017-09-25 20:57                     ` FIFO Niklas Holsti
2017-09-16 17:52 ` FIFO Robert A Duff
2017-09-16 18:11   ` FIFO Frank Buss
2017-09-17  9:46   ` FIFO Simon Wright
2017-09-16 22:04 ` FIFO Jeffrey R. Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox