From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.194.216.10 with SMTP id om10mr896906wjc.3.1466304632990; Sat, 18 Jun 2016 19:50:32 -0700 (PDT) X-Received: by 10.157.37.168 with SMTP id q37mr295024ota.13.1466304632879; Sat, 18 Jun 2016 19:50:32 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!goblin1!goblin.stu.neva.ru!oe3no4548350lbb.1!news-out.google.com!f5ni7414lbb.0!nntp.google.com!w10no2747927lbo.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sat, 18 Jun 2016 19:50:32 -0700 (PDT) In-Reply-To: <431af616-7df3-4e4d-9262-26ed68cb74c7@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=216.121.226.25; posting-account=ENgozAkAAACH-stq5yXctoDQeZQP2E6J NNTP-Posting-Host: 216.121.226.25 References: <66c14298-c62d-4f4b-b0c0-e969454f9334@googlegroups.com> <2e39857a-7121-476b-807a-d2bff1e598f4@googlegroups.com> <431af616-7df3-4e4d-9262-26ed68cb74c7@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <037df2b8-b9c4-4447-87ee-cc89d7072b30@googlegroups.com> Subject: Re: Generic Embedded List Nodes From: Warren Injection-Date: Sun, 19 Jun 2016 02:50:32 +0000 Content-Type: text/plain; charset=UTF-8 Xref: news.eternal-september.org comp.lang.ada:30804 Date: 2016-06-18T19:50:32-07:00 List-Id: On Saturday, 18 June 2016 22:21:18 UTC-4, Warren wrote: > On Saturday, 18 June 2016 22:14:22 UTC-4, Jeremiah wrote: > > On Saturday, June 18, 2016 at 6:52:06 PM UTC-4, Warren wrote: > > > Getting back to Ada after a hiatus, I currently have a need to build a > > > generic package to implement "embedded list node" lists. The advantage > > > is high performance in a server setting to avoid underlying malloc/free > > > calls. > > > > > > The idea is that the list node resides within the structure/tagged type, > > > and acts as a doubly linked list node when in a list. The Emb_Node can > > > be used as a list head when itself (or as part of another structure). This > > > kind of thing is done in the Linux kernel, for example. > > > > > > The Emb_Node and its operations are trivial. The problem occurs when > > > you traverse a linked list of Emb_Nodes (or its derived type). With > > > a given node, I need to then access the object that _contains_ it. In > > > C/C++ you do some offset calculation from the node address back to > > > the owning struct/class. > > > > > > My idea (in Ada) was to save some kind of access value in a type > > > derived from Emb_List. But that is where the trouble starts. > > > > > > package Emb_List is > > > > > > type Emb_Node is tagged private; > > > > > > procedure Insert_Head(Head: access Emb_Node; Node: access Emb_Node); > > > procedure Unlink(Node: access Emb_Node); > > > > > > private > > > > > > type Emb_Node is tagged > > > record > > > Next: access Emb_Node; -- Ptr to next node in list (if any) > > > Prev: access Emb_Node; -- Ptr to prev node in list (if any) > > > end record; > > > > > > end Emb_List; > > > > > > The generic extension (below) was intended to hold the reference to the > > > containing object, which is where the trouble starts. I was hoping this > > > would work, but running into "missing full declaration for private > > > extension". > > > > > > generic > > > type Object_Type is tagged private; > > > package Emb_List.Nodes is > > > > > > type Node_Type(Obj: access Object_Type) is new Emb_Node; > > > > > > function Object(Node: Node_Type) return Object_Type; > > > > > > end Emb_List.Nodes; > > > > Change Node_Type to: > > type Node_Type(Obj: access Object_Type) is new Emb_Node with null record; > > > > You need an extension, even if it is a null record. > > I had clearly forgotten about "with null record". I will try that. > > Warren generic type Object_Type is tagged private; package Emb_List.Nodes is type Node_Type(Obj: access Object_Type := Obj'Access) is new Emb_Node with null record; function Object(Node: Node_Type) return Object_Type; end Emb_List.Nodes; The problem with this now is "discriminant "Obj" cannot be used before end of discriminant part". This is intended to allow extension of My_Recd with a Node that knows My_Recd'Access. declare type My_Recd is tagged record Id: Natural := 99; end record; package EMB is new Emb_List.Nodes(Object_Type => My_Recd); Is there another way that I could do this: i.e. determine My_Recd'Access from some list node object, embedded in My_Recd? Warren