summaryrefslogtreecommitdiff
path: root/src/utils/queue.h
blob: 941c54348e5c8a0ae49bdad7eb389de3e6793b09 (plain)
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
//------------------------------------------------------------------------------
// Author: Pavel Karneliuk
// Description: Special Queue for fixed size elements without copying them
// Copyright (c) 2013 EPAM Systems
//------------------------------------------------------------------------------
/*
    This file is part of Nfstrace.

    Nfstrace is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 2 of the License.

    Nfstrace is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Nfstrace.  If not, see <http://www.gnu.org/licenses/>.
*/
//------------------------------------------------------------------------------
#ifndef QUEUE_H
#define QUEUE_H
//------------------------------------------------------------------------------
#include <memory>
#include <type_traits>

#include "utils/block_allocator.h"
#include "utils/spinlock.h"
//------------------------------------------------------------------------------
namespace NST
{
namespace utils
{

template <typename T>
class Queue
{
    struct Element // an element of the queue
    {
        Element* prev;
        T data;
    };

    struct ElementDeleter
    {
        inline explicit ElementDeleter()         noexcept : queue{nullptr} {}
        inline explicit ElementDeleter(Queue* q) noexcept : queue{q} {}

        inline void operator()(T* const pointer) const
        {
            if(pointer /*&& queue - dont check - optimization*/)
            {
                queue->deallocate(pointer);
            }
        }

        Queue* queue;
    };

public:

    using Ptr = std::unique_ptr<T, ElementDeleter>;

    class List  // List of elements for client code
    {
    public:
        inline explicit List(Queue& q) : queue{&q}
        {
            ptr = queue->pop_list();
        }
        List(const List&)            = delete;
        List& operator=(const List&) = delete;
        inline ~List()
        {
            while(ptr)
            {
                free_current();
            }
        }

        inline operator bool() const { return ptr;       } // is empty?
        inline const T& data() const { return ptr->data; } // get data
        inline Ptr get_current() // return element and switch to next
        {
            Element* tmp {ptr};
            ptr = ptr->prev;
            return Ptr{&tmp->data, ElementDeleter{queue}};
        }
        inline void free_current() // deallocate element and switch to next
        {
            Element* tmp {ptr->prev};
            queue->deallocate(ptr);
            ptr = tmp;
        }
    private:
        Element* ptr;
        Queue* queue;
    };


    Queue(uint32_t size, uint32_t limit) : last{nullptr}, first{nullptr}
    {
        allocator.init_allocation(sizeof(Element), size, limit);
    }
    ~Queue()
    {
        List list{*this};   // deallocate items by destructor of List
    }

    inline T* allocate()
    {
        static_assert(std::is_nothrow_constructible<T>::value,
                      "The construction of T must not to throw any exception");

        Spinlock::Lock lock{a_spinlock};
            Element* e {(Element*)allocator.allocate()}; // may throw std::bad_alloc
            auto ptr = &(e->data);
            ::new(ptr)T; // only call constructor of T (placement)
            return ptr;
    }

    inline void deallocate(T* ptr)
    {
        ptr->~T(); // placement allocation functions syntax is used 
        Element* e { (Element*)( ((char*)ptr) - sizeof(Element*) ) };
        deallocate(e);
    }

    inline void push(T* ptr)
    {
        Element* e { (Element*)( ((char*)ptr) - sizeof(Element*) ) };
        Spinlock::Lock lock{q_spinlock};
            if(last)
            {
                last->prev = e;
                last = e;
            }
            else    // queue is empty
            {
                last = first = e;
            }
    }

    inline Element* pop_list() // take out list of all queued elements
    {
        Element* list {nullptr};
        if(last)
        {
            Spinlock::Lock lock{q_spinlock};
                if(last)
                {
                    list = first;
                    last->prev = nullptr;  // set end of list
                    last = first = nullptr;
                }
        }
        return list;
    }

private:
    // accessible from Queue::List and Queue::Ptr
    inline void deallocate(Element* e)
    {
        Spinlock::Lock lock{a_spinlock};
            allocator.deallocate(e);
    }

    BlockAllocator allocator;
    Spinlock a_spinlock; // for allocate/deallocate
    Spinlock q_spinlock; // for queue push/pop

    // queue empty:   last->nullptr<-first
    // queue filled:  last->e<-e<-e<-e<-first
    // queue push(i): last->i<-e<-e<-e<-e<-first
    Element* last;
    Element* first;
};

} // namespace utils
} // namespace NST
//------------------------------------------------------------------------------
#endif//QUEUE_H
//------------------------------------------------------------------------------