2015-05-05 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / gcc / ada / a-cusyqu.adb
blob3a87306af150fbd364295cd13e1507412e38efb3
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- ADA.CONTAINERS.UNBOUNDED_SYNCHRONIZED_QUEUES --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2011-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 with Ada.Unchecked_Deallocation;
32 package body Ada.Containers.Unbounded_Synchronized_Queues is
34 pragma Annotate (CodePeer, Skip_Analysis);
36 package body Implementation is
38 -----------------------
39 -- Local Subprograms --
40 -----------------------
42 procedure Free is
43 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
45 -------------
46 -- Dequeue --
47 -------------
49 procedure Dequeue
50 (List : in out List_Type;
51 Element : out Queue_Interfaces.Element_Type)
53 X : Node_Access;
55 begin
56 Element := List.First.Element;
58 X := List.First;
59 List.First := List.First.Next;
61 if List.First = null then
62 List.Last := null;
63 end if;
65 List.Length := List.Length - 1;
67 Free (X);
68 end Dequeue;
70 -------------
71 -- Enqueue --
72 -------------
74 procedure Enqueue
75 (List : in out List_Type;
76 New_Item : Queue_Interfaces.Element_Type)
78 Node : Node_Access;
80 begin
81 Node := new Node_Type'(New_Item, null);
83 if List.First = null then
84 List.First := Node;
85 List.Last := List.First;
87 else
88 List.Last.Next := Node;
89 List.Last := Node;
90 end if;
92 List.Length := List.Length + 1;
94 if List.Length > List.Max_Length then
95 List.Max_Length := List.Length;
96 end if;
97 end Enqueue;
99 --------------
100 -- Finalize --
101 --------------
103 procedure Finalize (List : in out List_Type) is
104 X : Node_Access;
106 begin
107 while List.First /= null loop
108 X := List.First;
109 List.First := List.First.Next;
110 Free (X);
111 end loop;
112 end Finalize;
114 ------------
115 -- Length --
116 ------------
118 function Length (List : List_Type) return Count_Type is
119 begin
120 return List.Length;
121 end Length;
123 ----------------
124 -- Max_Length --
125 ----------------
127 function Max_Length (List : List_Type) return Count_Type is
128 begin
129 return List.Max_Length;
130 end Max_Length;
132 end Implementation;
134 protected body Queue is
136 -----------------
137 -- Current_Use --
138 -----------------
140 function Current_Use return Count_Type is
141 begin
142 return List.Length;
143 end Current_Use;
145 -------------
146 -- Dequeue --
147 -------------
149 entry Dequeue (Element : out Queue_Interfaces.Element_Type)
150 when List.Length > 0
152 begin
153 List.Dequeue (Element);
154 end Dequeue;
156 -------------
157 -- Enqueue --
158 -------------
160 entry Enqueue (New_Item : Queue_Interfaces.Element_Type) when True is
161 begin
162 List.Enqueue (New_Item);
163 end Enqueue;
165 --------------
166 -- Peak_Use --
167 --------------
169 function Peak_Use return Count_Type is
170 begin
171 return List.Max_Length;
172 end Peak_Use;
174 end Queue;
176 end Ada.Containers.Unbounded_Synchronized_Queues;