1 /*******************************************************************************
2 
3     Base class for a fixed size memory-based ring queue.
4 
5     Copyright:
6         Copyright (c) 2009-2016 dunnhumby Germany GmbH.
7         All rights reserved.
8 
9     License:
10         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
11         Alternatively, this file may be distributed under the terms of the Tango
12         3-Clause BSD License (see LICENSE_BSD.txt for details).
13 
14 *******************************************************************************/
15 
16 module ocean.util.container.queue.model.IRingQueue;
17 
18 
19 
20 
21 import ocean.util.container.queue.model.IQueueInfo;
22 
23 import ocean.util.container.mem.MemManager;
24 
25 
26 
27 /*******************************************************************************
28 
29     Base class for a fixed size memory-based ring queue.
30 
31 *******************************************************************************/
32 
33 public abstract class IRingQueue ( IBaseQueue ) : IBaseQueue
34 {
35     /***************************************************************************
36 
37         Data array -- the actual queue where the items are stored.
38 
39     ***************************************************************************/
40 
41     protected ubyte[] data;
42 
43     version (unittest)
44     public ubyte[] get_data ()
45     {
46         return this.data;
47     }
48 
49     /***************************************************************************
50 
51         Read & write positions (indices into the data array).
52 
53     ***************************************************************************/
54 
55     protected size_t write_to = 0;
56 
57     version (unittest)
58     public size_t get_write_to ()
59     {
60         return this.write_to;
61     }
62 
63     protected size_t read_from = 0;
64 
65     version (unittest)
66     public size_t get_read_from ()
67     {
68         return this.read_from;
69     }
70 
71 
72     /***************************************************************************
73 
74         Number of items in the queue.
75 
76     ***************************************************************************/
77 
78     protected uint items = 0;
79 
80     version (unittest)
81     public size_t get_items ()
82     {
83         return this.items;
84     }
85 
86     /***************************************************************************
87 
88         Memory manager used to allocated / deallocate the queue's buffer.
89 
90     ***************************************************************************/
91 
92     private IMemManager mem_manager;
93 
94 
95     /***************************************************************************
96 
97         Constructor. The queue's memory buffer is allocated by the GC.
98 
99         Params:
100             dimension = size of queue in bytes
101 
102     ***************************************************************************/
103 
104     protected this ( size_t dimension )
105     {
106         auto manager = gcMemManager;
107         this(manager, dimension);
108     }
109 
110 
111     /***************************************************************************
112 
113         Constructor. Allocates the queue's memory buffer with the provided
114         memory manager.
115 
116         Params:
117             mem_manager = memory manager to use to allocate queue's buffer
118             dimension = size of queue in bytes
119 
120     ***************************************************************************/
121 
122     protected this ( IMemManager mem_manager, size_t dimension )
123     in
124     {
125         assert(mem_manager !is null, typeof(this).stringof ~ ": memory manager is null");
126         assert(dimension > 0, typeof(this).stringof ~ ": cannot construct a 0-length queue");
127     }
128     do
129     {
130         this.mem_manager = mem_manager;
131 
132         this.data = this.mem_manager.create(dimension);
133     }
134 
135     /***************************************************************************
136 
137         Called for explicit deletes and on collection
138 
139     ***************************************************************************/
140 
141     ~this ( )
142     {
143         this.mem_manager.dtor(this.data);
144     }
145 
146 
147     /***************************************************************************
148 
149         Returns:
150             the number of items in the queue
151 
152     ***************************************************************************/
153 
154     public size_t length ( )
155     {
156         return this.items;
157     }
158 
159 
160     /***************************************************************************
161 
162         Tells whether the queue is empty.
163 
164         Returns:
165             true if the queue is empty
166 
167     ***************************************************************************/
168 
169     public bool is_empty ( )
170     {
171         return this.items == 0;
172     }
173 
174 
175     /***************************************************************************
176 
177         Returns:
178             number of bytes free in queue
179 
180     ***************************************************************************/
181 
182     public ulong free_space ( )
183     {
184         return this.data.length - this.used_space;
185     }
186 
187 
188     /***************************************************************************
189 
190         Returns:
191             number of bytes stored in queue
192 
193     ***************************************************************************/
194 
195     abstract ulong used_space ( );
196 
197 
198     /***************************************************************************
199 
200         Returns:
201             total number of bytes used by queue (used space + free space)
202 
203     ***************************************************************************/
204 
205     public ulong total_space ( )
206     {
207         return this.data.length;
208     }
209 
210 
211     /***************************************************************************
212 
213         Removes all items from the queue.
214 
215     ***************************************************************************/
216 
217     public void clear ( )
218     {
219         this.write_to   = 0;
220         this.read_from  = 0;
221         this.items      = 0;
222     }
223 }