summaryrefslogtreecommitdiff
path: root/source/LCM/Queue.cpp
blob: d2ef64ebf5501a4a30aebab716560cb8a3af7554 (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
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
/*******************************************************************************
 * Copyright (C) ST-Ericsson SA 2011
 * License terms: 3-clause BSD license
 ******************************************************************************/
/*
 *  @addtogroup ldr_utilities
 *  @{
 *    @addtogroup queue
 *    @{
 */

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "Queue.h"
#include "LCDriverMethods.h"
#include <cstdlib>
#include <cstring>

/*******************************************************************************
 * Types, constants
 ******************************************************************************/

typedef struct Queue_s {
    /* Head index of the buffer. This is where items are inserted.*/
    uint32 Head;
    /* Tail index of the buffer. This is where items are removed.*/
    uint32 Tail;
    /* Size of the buffer.*/
    uint32 Size;
    /* Buffer pointer.*/
    void **Buffer_pp;
    /* Empty buffer callback pointer. */
    void (*EmptyCallback)(const void *const Queue_p, void *Param_p);
    /* The parameter is used for the empty buffer transition callback.*/
    void *EmptyParam_p;
    /* NonEmpty buffer callback pointer. */
    void (*NonEmptyCallback)(const void *const Queue_p, void *Param_p);
    /* The parameter used for the non-empty buffer transition callback.*/
    void *NonEmptyParam_p;
    /* Pointer to a function for deallocating any resources that are reserved for
     * the element. This function is provided as an input parameter when the queue
     * is created.
     */
    void (*DestroyElement)(void *Value_p);
} Queue_t;

Queue::Queue(void)
{
    logger_ = NULL;
}

Queue::~Queue(void)
{
}

/*******************************************************************************
 * Definition of external functions
 ******************************************************************************/
void Queue::Create(void **const Queue_pp, const uint32 MaxLength, void (*DestroyElement)(void *Element_p))
{
    Queue_t *Queue_p = new Queue_t;

    if (Queue_p != 0) {
        Queue_p->Size = MaxLength;
        Queue_p->Head = 0;
        Queue_p->Tail = 0;
        Queue_p->Buffer_pp = new void*[MaxLength + 1];
        Queue_p->DestroyElement = DestroyElement;
        Queue_p->NonEmptyCallback = 0;
        Queue_p->NonEmptyParam_p = 0;
        Queue_p->EmptyCallback = 0;
        Queue_p->EmptyParam_p = 0;

        *Queue_pp = Queue_p;

#ifdef _QUEUEDEBUG
        PrintF("Queue: Create - Queue 0x%p", Queue_p);
#endif
    } else {
#ifdef _QUEUEDEBUG
        PrintF("Queue: Create - Not enough memory Queue 0x%p", 0);
#endif
    }
}

void Queue::Destroy(void **const Queue_pp)
{
    Queue_t *Queue_p = (Queue_t *) * Queue_pp;
    void *Object_p = 0;

    if (NULL != Queue_p->DestroyElement) {
        Object_p = Dequeue(Queue_p);

        while (0 != Object_p) {
            Queue_p->DestroyElement(Object_p);
            Object_p = Dequeue(Queue_p);
        }
    }

#ifdef _QUEUEDEBUG
    PrintF("Queue: Destroy - Queue 0x%p", Queue_p);
#endif

    delete[] Queue_p->Buffer_pp;
    delete Queue_p;
    *Queue_pp = NULL;
}

ErrorCode_e Queue::Enqueue(void *const Queue_p, void *const Object_p)
{
    Queue_t *TempQueue_p = (Queue_t *)Queue_p;
    uint32 Head = (TempQueue_p->Head + 1) % TempQueue_p->Size;
    boolean NonEmptyCall = (TempQueue_p->Head == TempQueue_p->Tail);

    if (Head != TempQueue_p->Tail) {
#ifdef _QUEUEDEBUG
        PrintF("Queue: Enqueue - Queue 0x%p", Queue_p);
        PrintF("Queue: Enqueue - Object 0x%p", Object_p);
#endif
        TempQueue_p->Buffer_pp[Head] = Object_p;
        TempQueue_p->Head = Head;

        if (Head == TempQueue_p->Tail) {
#ifdef _QUEUEDEBUG
            PrintF("Queue: Enqueue - return E_FAILED_TO_STORE_IN_FIFO, Object 0x%p", Object_p);
#endif
            return E_FAILED_TO_STORE_IN_FIFO;
        }

        if (NonEmptyCall && NULL != TempQueue_p->NonEmptyCallback) {
#ifdef _QUEUEDEBUG
            PrintF("Queue: Enqueue - Executing NonEmptyCallback Queue 0x%p", Queue_p);
#endif
            TempQueue_p->NonEmptyCallback(TempQueue_p, TempQueue_p->NonEmptyParam_p);
        }

        return E_SUCCESS;
    } else {
#ifdef _QUEUEDEBUG
        PrintF("Queue: Enqueue - return E_FAILED_TO_STORE_IN_FIFO, Object 0x%p", Object_p);
#endif
        return E_FAILED_TO_STORE_IN_FIFO;
    }
}

void *Queue::Dequeue(void *const Queue_p)
{
    Queue_t *TempQueue_p = (Queue_t *)Queue_p;
    uint32 Tail = TempQueue_p->Tail;
    void *Object_p = NULL;

    if (Tail != TempQueue_p->Head) {
#ifdef _QUEUEDEBUG
        PrintF("Queue: Dequeue - Queue 0x%p", Queue_p);
#endif
        Tail = (Tail + 1) % TempQueue_p->Size;
        Object_p = TempQueue_p->Buffer_pp[Tail];
        TempQueue_p->Tail = Tail;

        if (Tail == TempQueue_p->Head && NULL != TempQueue_p->EmptyCallback) {
#ifdef _QUEUEDEBUG
            PrintF("Queue: Dequeue - Executing EmptyCallback Queue 0x%p", Queue_p);
#endif
            TempQueue_p->EmptyCallback(TempQueue_p, TempQueue_p->EmptyParam_p);
        }

#ifdef _QUEUEDEBUG
        PrintF("Queue: Dequeue - Object 0x%p", Object_p);
#endif
    }

    return Object_p;
}

QueueCallback_fn Queue::SetCallback(void *const Queue_p,
                                    const QueueCallbackType_e Type,
                                    const QueueCallback_fn Callback,
                                    void *const Param_p)
{
    Queue_t *TempQueue_p = (Queue_t *)Queue_p;
    QueueCallback_fn OldCallback;

    if (Type == QUEUE_EMPTY) {
        OldCallback = TempQueue_p->EmptyCallback;
        TempQueue_p->EmptyCallback = Callback;
        TempQueue_p->EmptyParam_p = Param_p;
    } else {
        OldCallback = TempQueue_p->NonEmptyCallback;
        TempQueue_p->NonEmptyCallback = Callback;
        TempQueue_p->NonEmptyParam_p = Param_p;
    }

    return OldCallback;
}

boolean Queue::IsEmpty(const void *const Queue_p)
{
    Queue_t *TempQueue_p = (Queue_t *)Queue_p;

    if (TempQueue_p->Tail == TempQueue_p->Head) {
        return TRUE;
    } else {
        return FALSE;
    }
}

boolean Queue::IsMember(const void *const Queue_p, void *Value_p, boolean(*Match)(void *Value1_p, void *Value2_p))
{
    Queue_t *TempQueue_p = (Queue_t *)Queue_p;

    for (size_t i = TempQueue_p->Head; i < TempQueue_p->Tail; i++) {
        if (Match(Value_p, TempQueue_p->Buffer_pp[i])) {
            return TRUE;
        }
    }

    return FALSE;
}

int Queue::GetNrOfElements(const void *const Queue_p)
{
    Queue_t *TempQueue_p = (Queue_t *)Queue_p;
    int Return = 0;

    if (TempQueue_p->Head >= TempQueue_p->Tail) {
        Return = TempQueue_p->Head - TempQueue_p->Tail;
    } else {
        Return = TempQueue_p->Size - (TempQueue_p->Tail - TempQueue_p->Head);
    }

    return Return;
}

void Queue::RCreate(void **const Queue_pp, const uint32 MaxLength, void (*DestroyElement)(void *))
{
    CLockCS QueueLock(QueueCSO);
    Create(Queue_pp, MaxLength, DestroyElement);
}

void Queue::RDestroy(void **const Queue_pp)
{
    CLockCS QueueLock(QueueCSO);
    Destroy(Queue_pp);
}

ErrorCode_e Queue::REnqueue(void *const Queue_p, void *const Value_p)
{
    CLockCS QueueLock(QueueCSO);
    return Enqueue(Queue_p, Value_p);
}

void *Queue::RDequeue(void *const Queue_p)
{
    CLockCS QueueLock(QueueCSO);
    return Dequeue(Queue_p);
}

QueueCallback_fn Queue::RSetCallback(void *const Queue_p,
                                     const QueueCallbackType_e Type,
                                     const QueueCallback_fn Callback,
                                     void *const Param_p)
{
    CLockCS QueueLock(QueueCSO);
    return SetCallback(Queue_p, Type, Callback, Param_p);
}

boolean Queue::RIsEmpty(const void *const Queue_p)
{
    CLockCS QueueLock(QueueCSO);
    return IsEmpty(Queue_p);
}

boolean Queue::RIsMember(const void *const Queue_p, void *Value_p, boolean(*Match)(void *Value1_p, void *Value2_p))
{
    CLockCS QueueLock(QueueCSO);
    return IsMember(Queue_p, Value_p, Match);
}

int Queue::RGetNrOfElements(const void *const Queue_p)
{
    CLockCS QueueLock(QueueCSO);
    return GetNrOfElements(Queue_p);
}

void Queue::PrintF(const char *text, void *pVoid)
{
    if (NULL != logger_) {
        logger_->log(text, pVoid);
    }
}