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