File: buffer.h

package info (click to toggle)
libcds 2.3.3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,632 kB
  • sloc: cpp: 135,002; ansic: 7,234; perl: 243; sh: 237; makefile: 6
file content (552 lines) | stat: -rw-r--r-- 19,810 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// Copyright (c) 2006-2018 Maxim Khizhinsky
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef CDSLIB_OPT_BUFFER_H
#define CDSLIB_OPT_BUFFER_H

#include <memory.h>
#include <cds/details/defs.h>
#include <cds/user_setup/allocator.h>
#include <cds/details/allocator.h>
#include <cds/algo/int_algo.h>
#include <memory>

namespace cds { namespace opt {

    /// [type-option] Option setter for user-provided plain buffer
    /**
        This option is used by some container as a random access array for storing
        container's item; for example, a bounded queue may use
        this option to define underlying buffer implementation.

        The template parameter \p Type should be rebindable.

        Implementations:
            - \p opt::v::initialized_static_buffer
            - \p opt::v::uninitialized_static_buffer
            - \p opt::v::initialized_dynamic_buffer
            - \p opt::v::uninitialized_dynamic_buffer

        Uninitialized buffer is just an array of uninitialized elements.
        Each element should be manually constructed, for example with a placement new operator.
        When the uninitialized buffer is destroyed the destructor of its element is not called.

        Initialized buffer contains default-constructed elements. Element destructor is called automatically
        when the buffer is destroyed.

        @note Usually, initialized and uninitialized buffers are not interchangeable.
    */
    template <typename Type>
    struct buffer {
        //@cond
        template <typename Base> struct pack: public Base
        {
            typedef Type buffer;
        };
        //@endcond
    };

    namespace v {

        /// Static uninitialized buffer
        /**
            One of available type for \p opt::buffer option.

            This buffer maintains static array of uninitialized elements.
            You should manually construct each element when needed.
            No dynamic memory allocation performed.

            \par Template parameters:
                - \p T - item type the buffer stores
                - \p Capacity - the capacity of buffer. The value must be power of two if \p Exp2 is \p true
                - \p Exp2 - a boolean flag. If it is \p true the buffer capacity must be power of two.
                    Otherwise it can be any positive number. Usually, it is required that the buffer has
                    size of a power of two.
        */
        template <typename T, size_t Capacity, bool Exp2 = true>
        class uninitialized_static_buffer
        {
        public:
            typedef T   value_type;   ///< value type
            static constexpr const size_t c_nCapacity = Capacity;    ///< Capacity
            static constexpr const bool c_bExp2 = Exp2; ///< \p Exp2 flag

            /// Rebind buffer for other template parameters
            template <typename Q, size_t Capacity2 = c_nCapacity, bool Exp22 = c_bExp2>
            struct rebind {
                typedef uninitialized_static_buffer<Q, Capacity2, Exp22> other;   ///< Rebind result type
            };

            // Capacity must be power of 2
            static_assert(!c_bExp2 || (c_nCapacity & (c_nCapacity - 1)) == 0, "Capacity must be power of two");

        private:
            //@cond
            union element {
                value_type v;
                char       c;

                element()
                {}
            };

            element  m_buffer[c_nCapacity];
            //@endcond
        public:
            /// Construct static buffer
            uninitialized_static_buffer() noexcept
            {}

            /// Construct buffer of given capacity
            /**
                This ctor ignores \p nCapacity argument. The capacity of static buffer
                is defined by template argument \p Capacity
            */
            uninitialized_static_buffer( size_t nCapacity ) noexcept
            {
                CDS_UNUSED( nCapacity );
            }

            uninitialized_static_buffer( const uninitialized_static_buffer& ) = delete;
            uninitialized_static_buffer& operator =( const uninitialized_static_buffer& ) = delete;

            /// Get item \p i
            value_type& operator []( size_t i )
            {
                assert( i < capacity());
                return m_buffer[i].v;
            }

            /// Get item \p i, const version
            const value_type& operator []( size_t i ) const
            {
                assert( i < capacity());
                return m_buffer[i].v;
            }

            /// Returns buffer capacity
            constexpr size_t capacity() const noexcept
            {
                return c_nCapacity;
            }

            /// Zeroize the buffer
            void zeroize()
            {
                memset( m_buffer, 0, capacity() * sizeof(m_buffer[0]));
            }

            /// Returns pointer to buffer array
            value_type * buffer() noexcept
            {
                return &( m_buffer[0].v );
            }

            /// Returns pointer to buffer array
            value_type * buffer() const noexcept
            {
                return &( m_buffer[0].v );
            }

            /// Returns <tt> idx % capacity() </tt>
            /**
                If the buffer size is a power of two, binary arithmethics is used
                instead of modulo arithmetics
            */
            size_t mod( size_t idx )
            {
                constexpr_if ( c_bExp2 )
                    return idx & ( capacity() - 1 );
                else
                    return idx % capacity();
            }

            //@cond
            template <typename I>
            typename std::enable_if< sizeof(I) != sizeof(size_t), size_t >::type mod( I idx )
            {
                constexpr_if ( c_bExp2 )
                    return static_cast<size_t>( idx & static_cast<I>( capacity() - 1 ));
                else
                    return static_cast<size_t>( idx % capacity());
            }
            //@endcond
        };

        /// Static initialized buffer
        /**
            One of available type for \p opt::buffer option.

            This buffer maintains static array of default-constructed elements.
            No dynamic memory allocation performed.

            \par Template parameters:
                - \p T - item type the buffer stores
                - \p Capacity - the capacity of buffer. The value must be power of two if \p Exp2 is \p true
                - \p Exp2 - a boolean flag. If it is \p true the buffer capacity must be power of two.
                    Otherwise it can be any positive number. Usually, it is required that the buffer has
                    size of a power of two.
        */
        template <typename T, size_t Capacity, bool Exp2 = true>
        class initialized_static_buffer
        {
        public:
            typedef T   value_type;   ///< value type
            static constexpr const size_t c_nCapacity = Capacity;    ///< Capacity
            static constexpr const bool c_bExp2 = Exp2; ///< \p Exp2 flag

            /// Rebind buffer for other template parameters
            template <typename Q, size_t Capacity2 = c_nCapacity, bool Exp22 = c_bExp2>
            struct rebind {
                typedef initialized_static_buffer<Q, Capacity2, Exp22> other;   ///< Rebind result type
            };

            // Capacity must be power of 2
            static_assert(!c_bExp2 || (c_nCapacity & (c_nCapacity - 1)) == 0, "Capacity must be power of two");

        private:
            //@cond
            value_type  m_buffer[c_nCapacity];
            //@endcond
        public:
            /// Construct static buffer
            initialized_static_buffer() noexcept
            {}

            /// Construct buffer of given capacity
            /**
                This ctor ignores \p nCapacity argument. The capacity of static buffer
                is defined by template argument \p Capacity
            */
            initialized_static_buffer( size_t nCapacity ) noexcept
            {
                CDS_UNUSED( nCapacity );
            }

            initialized_static_buffer( const initialized_static_buffer& ) = delete;
            initialized_static_buffer& operator =( const initialized_static_buffer& ) = delete;

            /// Get item \p i
            value_type& operator []( size_t i )
            {
                assert( i < capacity());
                return m_buffer[i];
            }

            /// Get item \p i, const version
            const value_type& operator []( size_t i ) const
            {
                assert( i < capacity());
                return m_buffer[i];
            }

            /// Returns buffer capacity
            constexpr size_t capacity() const noexcept
            {
                return c_nCapacity;
            }

            /// Zeroize the buffer
            void zeroize()
            {
                memset( m_buffer, 0, capacity() * sizeof(m_buffer[0]));
            }

            /// Returns pointer to buffer array
            value_type * buffer() noexcept
            {
                return m_buffer;
            }

            /// Returns pointer to buffer array
            value_type * buffer() const noexcept
            {
                return m_buffer;
            }

            /// Returns <tt> idx % capacity() </tt>
            /**
            If the buffer size is a power of two, binary arithmethics is used
            instead of modulo arithmetics
            */
            size_t mod( size_t idx )
            {
                constexpr_if ( c_bExp2 )
                    return idx & ( capacity() - 1 );
                else
                    return idx % capacity();
            }

            //@cond
            template <typename I>
            typename std::enable_if< sizeof( I ) != sizeof( size_t ), size_t >::type mod( I idx )
            {
                constexpr_if ( c_bExp2 )
                    return static_cast<size_t>( idx & static_cast<I>( capacity() - 1 ));
                else
                    return static_cast<size_t>( idx % capacity());
            }
            //@endcond
        };

        /// Dynamically allocated uninitialized buffer
        /**
            One of available type for \p opt::buffer option.

            This buffer maintains dynamically allocated array of uninitialized elements.
            You should manually construct each element when needed.
            Allocation is performed at construction time.

            \par Template parameters:
                - \p T - item type storing in the buffer
                - \p Alloc - an allocator used for allocating internal buffer (\p std::allocator interface)
                - \p Exp2 - a boolean flag. If it is \p true the buffer capacity must be power of two.
                    Otherwise it can be any positive number. Usually, it is required that the buffer has
                    size of a power of two.
        */
        template <typename T, class Alloc = CDS_DEFAULT_ALLOCATOR, bool Exp2 = true>
        class uninitialized_dynamic_buffer
        {
        public:
            typedef T     value_type;   ///< Value type
            typedef Alloc allocator;    ///< Allocator type;
            static constexpr const bool c_bExp2 = Exp2; ///< \p Exp2 flag

            /// Rebind buffer for other template parameters
            template <typename Q, typename Alloc2= allocator, bool Exp22 = c_bExp2>
            struct rebind {
                typedef uninitialized_dynamic_buffer<Q, Alloc2, Exp22> other;  ///< Rebinding result type
            };

            //@cond
            typedef typename std::allocator_traits<allocator>::template rebind_alloc<value_type> allocator_type;
            //@endcond

        private:
            //@cond
            value_type *    m_buffer;
            size_t const    m_nCapacity;
            //@endcond
        public:
            /// Allocates dynamic buffer of given \p nCapacity
            /**
                If \p Exp2 class template parameter is \p true then actual capacity
                of allocating buffer is nearest upper to \p nCapacity power of two.
            */
            uninitialized_dynamic_buffer( size_t nCapacity )
                : m_nCapacity( c_bExp2 ? beans::ceil2(nCapacity) : nCapacity )
            {
                assert( m_nCapacity >= 2 );
                // Capacity must be power of 2
                assert( !c_bExp2 || (m_nCapacity & (m_nCapacity - 1)) == 0 );

                m_buffer = allocator_type().allocate( m_nCapacity );
            }

            /// Destroys dynamically allocated buffer
            ~uninitialized_dynamic_buffer()
            {
                allocator_type().deallocate( m_buffer, m_nCapacity );
            }

            uninitialized_dynamic_buffer( const uninitialized_dynamic_buffer& ) = delete;
            uninitialized_dynamic_buffer& operator =( const uninitialized_dynamic_buffer& ) = delete;

            /// Get item \p i
            value_type& operator []( size_t i )
            {
                assert( i < capacity());
                return m_buffer[i];
            }

            /// Get item \p i, const version
            const value_type& operator []( size_t i ) const
            {
                assert( i < capacity());
                return m_buffer[i];
            }

            /// Returns buffer capacity
            size_t capacity() const noexcept
            {
                return m_nCapacity;
            }

            /// Zeroize the buffer
            void zeroize()
            {
                memset( m_buffer, 0, capacity() * sizeof(m_buffer[0]));
            }

            /// Returns pointer to buffer array
            value_type * buffer() noexcept
            {
                return m_buffer;
            }

            /// Returns pointer to buffer array
            value_type * buffer() const noexcept
            {
                return m_buffer;
            }

            /// Returns <tt> idx % capacity() </tt>
            /**
                If the buffer size is a power of two, binary arithmethics is used
                instead of modulo arithmetics
            */
            size_t mod( size_t idx )
            {
                constexpr_if ( c_bExp2 )
                    return idx & ( capacity() - 1 );
                else
                    return idx % capacity();
            }

            //@cond
            template <typename I>
            typename std::enable_if< sizeof( I ) != sizeof( size_t ), size_t >::type mod( I idx )
            {
                constexpr_if ( c_bExp2 )
                    return static_cast<size_t>( idx & static_cast<I>( capacity() - 1 ));
                else
                    return static_cast<size_t>( idx % capacity());
            }
            //@endcond
        };


        /// Dynamically allocated initialized buffer
        /**
            One of available type for \p opt::buffer option.

            This buffer maintains dynamically allocated array of initialized default-constructed elements.
            Allocation is performed at construction time.

            \par Template parameters:
                - \p T - item type storing in the buffer
                - \p Alloc - an allocator used for allocating internal buffer (\p std::allocator interface)
                - \p Exp2 - a boolean flag. If it is \p true the buffer capacity must be power of two.
                    Otherwise it can be any positive number. Usually, it is required that the buffer has
                    size of a power of two.
        */
        template <typename T, class Alloc = CDS_DEFAULT_ALLOCATOR, bool Exp2 = true>
        class initialized_dynamic_buffer
        {
        public:
            typedef T     value_type;   ///< Value type
            typedef Alloc allocator;    ///< Allocator type
            static constexpr const bool c_bExp2 = Exp2; ///< \p Exp2 flag

            /// Rebind buffer for other template parameters
            template <typename Q, typename Alloc2= allocator, bool Exp22 = c_bExp2>
            struct rebind {
                typedef initialized_dynamic_buffer<Q, Alloc2, Exp22> other;  ///< Rebinding result type
            };

            //@cond
            typedef cds::details::Allocator<value_type, allocator>   allocator_type;
            //@endcond

        private:
            //@cond
            value_type *    m_buffer;
            size_t const    m_nCapacity;
            //@endcond
        public:
            /// Allocates dynamic buffer of given \p nCapacity
            /**
                If \p Exp2 class template parameter is \p true then actual capacity
                of allocating buffer is nearest upper to \p nCapacity power of two.
            */
            initialized_dynamic_buffer( size_t nCapacity )
                : m_nCapacity( c_bExp2 ? beans::ceil2(nCapacity) : nCapacity )
            {
                assert( m_nCapacity >= 2 );
                // Capacity must be power of 2
                assert( !c_bExp2 || (m_nCapacity & (m_nCapacity - 1)) == 0 );

                allocator_type a;
                m_buffer = a.NewArray( m_nCapacity );
            }

            /// Destroys dynamically allocated buffer
            ~initialized_dynamic_buffer()
            {
                allocator_type a;
                a.Delete( m_buffer, m_nCapacity );
            }

            initialized_dynamic_buffer( const initialized_dynamic_buffer& ) = delete;
            initialized_dynamic_buffer& operator =( const initialized_dynamic_buffer& ) = delete;

            /// Get item \p i
            value_type& operator []( size_t i )
            {
                assert( i < capacity());
                return m_buffer[i];
            }

            /// Get item \p i, const version
            const value_type& operator []( size_t i ) const
            {
                assert( i < capacity());
                return m_buffer[i];
            }

            /// Returns buffer capacity
            size_t capacity() const noexcept
            {
                return m_nCapacity;
            }

            /// Zeroize the buffer
            void zeroize()
            {
                memset( m_buffer, 0, capacity() * sizeof(m_buffer[0]));
            }

            /// Returns pointer to buffer array
            value_type * buffer() noexcept
            {
                return m_buffer;
            }

            /// Returns pointer to buffer array
            value_type * buffer() const noexcept
            {
                return m_buffer;
            }

            /// Returns <tt> idx % capacity() </tt>
            /**
            If the buffer size is a power of two, binary arithmethics is used
            instead of modulo arithmetics
            */
            size_t mod( size_t idx )
            {
                constexpr_if ( c_bExp2 )
                    return idx & ( capacity() - 1 );
                else
                    return idx % capacity();
            }

            //@cond
            template <typename I>
            typename std::enable_if< sizeof( I ) != sizeof( size_t ), size_t >::type mod( I idx )
            {
                constexpr_if ( c_bExp2 )
                    return static_cast<size_t>( idx & static_cast<I>( capacity() - 1 ));
                else
                    return static_cast<size_t>( idx % capacity());
            }
            //@endcond
        };

    }   // namespace v

}}  // namespace cds::opt

#endif // #ifndef CDSLIB_OPT_BUFFER_H