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