129407905973180351e8903a9826c6443d9d433f
[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     NS_VERIFY_NOT_NULL_V(queue);
42
43     NSConsumerQueueObject * node = NSPopQueue(queue);
44     while(node)
45     {
46         NSConsumerQueueObject * next = (NSConsumerQueueObject *)node->next;
47         NSOICFree(node->data);
48         NSOICFree(node);
49
50         node = next;
51     }
52
53     NSOICFree(queue);
54 }
55
56 bool NSPushQueue(NSConsumerQueue * queue, NSConsumerQueueObject * object)
57 {
58     NS_VERIFY_NOT_NULL(queue, false);
59     NS_VERIFY_NOT_NULL(object, false);
60
61     if (!(queue->head))
62     {
63         queue->head = object;
64     }
65     else
66     {
67         (queue->tail)->next = object;
68     }
69
70     queue->tail = object;
71     queue->size++;
72
73     return true;
74 }
75
76 NSConsumerQueueObject * NSPopQueue(NSConsumerQueue * queue)
77 {
78     NSConsumerQueueObject * retObject = NULL;
79
80     NS_VERIFY_NOT_NULL(queue, NULL);
81     NS_VERIFY_NOT_NULL(queue->head, NULL);
82
83     if (queue->size <= 0)
84     {
85         return NULL;
86     }
87
88     retObject = queue->head;
89
90     queue->head = (NSConsumerQueueObject *)(retObject->next);
91     if (!(queue->head))
92     {
93         queue->tail = NULL;
94     }
95     retObject->next = NULL;
96     queue->size--;
97
98     return retObject;
99 }
100
101 int NSGetQueueSize(NSConsumerQueue * queue)
102 {
103     return queue->size;
104 }
105
106 bool NSIsQueueEmpty(NSConsumerQueue * queue)
107 {
108     return (queue->size <= 0);
109 }