ae6cd2e472d691cb5f0fc6746668aacc3266e3bf
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerQueue.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "NSConsumerQueue.h"
22
23 #include "NSConstants.h"
24 #include "oic_malloc.h"
25 #include "NSConsumerCommon.h"
26
27 NSConsumerQueue * NSCreateQueue()
28 {
29     NSConsumerQueue * newQueue = (NSConsumerQueue *)OICMalloc(sizeof(NSConsumerQueue));
30     NS_VERIFY_NOT_NULL(newQueue, NULL);
31
32     newQueue->size = 0;
33     newQueue->head = NULL;
34     newQueue->tail = NULL;
35
36     return newQueue;
37 }
38
39 void NSDestroyQueue(NSConsumerQueue * queue)
40 {
41     NSConsumerQueueObject * node = NSPopQueue(queue);
42     while(node)
43     {
44         node = (NSConsumerQueueObject *)node->next;
45         OICFree(node->data);
46         OICFree(node);
47     }
48
49     OICFree(queue);
50 }
51
52 bool NSPushQueue(NSConsumerQueue * queue, NSConsumerQueueObject * object)
53 {
54     NS_VERIFY_NOT_NULL(queue, false);
55     NS_VERIFY_NOT_NULL(object, false);
56
57     if (!(queue->head))
58     {
59         queue->head = object;
60     }
61     else
62     {
63         (queue->tail)->next = object;
64     }
65
66     queue->tail = object;
67     queue->size++;
68
69     return true;
70 }
71
72 NSConsumerQueueObject * NSPopQueue(NSConsumerQueue * queue)
73 {
74     NSConsumerQueueObject * retObject = NULL;
75
76     NS_VERIFY_NOT_NULL(queue, NULL);
77     NS_VERIFY_NOT_NULL(queue->head, NULL);
78
79     if (queue->size <= 0)
80     {
81         return NULL;
82     }
83
84     retObject = queue->head;
85
86     queue->head = (NSConsumerQueueObject *)(retObject->next);
87     if (!(queue->head))
88     {
89         queue->tail = NULL;
90     }
91     retObject->next = NULL;
92     queue->size--;
93
94     return retObject;
95 }
96
97 int NSGetQueueSize(NSConsumerQueue * queue)
98 {
99     return queue->size;
100 }
101
102 bool NSIsQueueEmpty(NSConsumerQueue * queue)
103 {
104     return (queue->size <= 0);
105 }