added cacheadapter interface and memorycache of notification.
[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     if (!newQueue)
31     {
32         return NULL;
33     }
34
35     newQueue->size = 0;
36     newQueue->head = NULL;
37     newQueue->tail = NULL;
38
39     return newQueue;
40 }
41
42 void NSDestroyQueue(NSConsumerQueue * queue)
43 {
44     NSConsumerQueueObject * node = NSPopQueue(queue);
45     while(node)
46     {
47         node = (NSConsumerQueueObject *)node->next;
48         OICFree(node->data);
49         OICFree(node);
50     }
51
52     OICFree(queue);
53 }
54
55 bool NSPushQueue(NSConsumerQueue * queue, NSConsumerQueueObject * object)
56 {
57     if (!queue)
58     {
59         return false;
60     }
61
62     if (!object)
63     {
64         NS_LOG(ERROR, "object is null. can not insert to queue");
65     }
66
67     if (!(queue->head))
68     {
69         queue->head = object;
70     }
71     else
72     {
73         (queue->tail)->next = object;
74     }
75
76     queue->tail = object;
77     queue->size++;
78
79     return true;
80 }
81
82 NSConsumerQueueObject * NSPopQueue(NSConsumerQueue * queue)
83 {
84     NSConsumerQueueObject * retObject = NULL;
85
86     if (!queue)
87     {
88         return NULL;
89     }
90
91     if (queue->size <= 0)
92     {
93         return NULL;
94     }
95
96     if (!(queue->head))
97     {
98         return NULL;
99     }
100
101     retObject = queue->head;
102
103     queue->head = (NSConsumerQueueObject *)(retObject->next);
104     if (!(queue->head))
105     {
106         queue->tail = NULL;
107     }
108     retObject->next = NULL;
109     queue->size--;
110
111     return retObject;
112 }
113
114 int NSGetQueueSize(NSConsumerQueue * queue)
115 {
116     return queue->size;
117 }
118
119 bool NSIsEmptyQueue(NSConsumerQueue * queue)
120 {
121     return (queue->size <= 0);
122 }