• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FILLP_QUEUE_H
17 #define FILLP_QUEUE_H
18 
19 #include "lf_ring.h"
20 #include "spunge_mem.h"
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 typedef struct InnerfillpQueue {
26     FILLP_INT allocType;
27 #ifdef FILLP_64BIT_ALIGN
28     FILLP_UINT8 padd[4];
29 #endif
30     size_t size;
31     struct FillpLfRing ring;
32 } FillpQueue;
33 
FillpQueuePush(FillpQueue * q,void ** msg,FILLP_INT isTryPush,FILLP_UINT count)34 static __inline FillpErrorType FillpQueuePush(FillpQueue *q, void **msg, FILLP_INT isTryPush, FILLP_UINT count)
35 {
36     FILLP_INT ret;
37     FILLP_INT totalPush = 0;
38     for (;;) {
39         ret = FillpLfRingMpEnqueue(&q->ring, &msg[totalPush], count - (FILLP_UINT)totalPush);
40         if (ret > 0) {
41             totalPush += ret;
42             if ((FILLP_UINT)totalPush == count) {
43                 return ERR_OK;
44             }
45         } else if (isTryPush) {
46             return ERR_NOBUFS;
47         }
48     }
49 }
50 
FillpQueuePop(FillpQueue * q,void ** msg,FILLP_UINT count)51 static __inline FILLP_INT FillpQueuePop(FillpQueue *q, void **msg, FILLP_UINT count)
52 {
53     if ((q == FILLP_NULL_PTR) || (msg == FILLP_NULL_PTR)) {
54         return -1;
55     }
56 
57     return FillpLfRingMcDequeue(&q->ring, msg, count);
58 }
59 
QueueEmpty(FILLP_CONST FillpQueue * q)60 static __inline int QueueEmpty(FILLP_CONST FillpQueue *q)
61 {
62     return FillpRingEmpty(&q->ring);
63 }
64 
FillpQueueCalMemSize(size_t size)65 static __inline size_t FillpQueueCalMemSize(size_t size)
66 {
67     size_t tmpSize = FillpLfRingCalMemSize(size);
68     size_t memSize = tmpSize + sizeof(FillpQueue);
69 
70     if ((tmpSize == 0) || (memSize < sizeof(FillpQueue))) {
71         return 0;
72     }
73 
74     return memSize;
75 }
76 
FillpQueueSetProdSafe(FillpQueue * q,FILLP_BOOL safe)77 static __inline void FillpQueueSetProdSafe(FillpQueue *q, FILLP_BOOL safe)
78 {
79     FillpLfRingSetProdSafe(&q->ring, safe);
80 }
81 
FillpQueueSetConsSafe(FillpQueue * q,FILLP_BOOL safe)82 static __inline void FillpQueueSetConsSafe(FillpQueue *q, FILLP_BOOL safe)
83 {
84     FillpLfRingSetConsSafe(&q->ring, safe);
85 }
86 
FillpQueueInit(FillpQueue * q,char * name,size_t size,FILLP_INT allocType)87 static __inline void FillpQueueInit(FillpQueue *q, char *name, size_t size, FILLP_INT allocType)
88 {
89     FillpLfRingInit(&q->ring, name, size);
90 
91     q->allocType = allocType;
92     q->size = size;
93 }
94 
FillpQueueCreate(char * name,size_t size,FILLP_INT allocType)95 static __inline FillpQueue *FillpQueueCreate(char *name, size_t size, FILLP_INT allocType)
96 {
97     FillpQueue *q;
98     q = (FillpQueue *)SpungeAlloc(1, FillpQueueCalMemSize(size), allocType);
99     if (q == FILLP_NULL_PTR) {
100         FILLP_LOGERR("Failed to allocate the memory for queue \r\n");
101         return FILLP_NULL_PTR;
102     }
103 
104     FillpQueueInit(q, name, size, allocType);
105 
106     return q;
107 }
108 
FillpQueueDestroy(FillpQueue * q)109 static __inline void FillpQueueDestroy(FillpQueue *q)
110 {
111     if (q == FILLP_NULL_PTR) {
112         return;
113     }
114 
115     if ((q->allocType == SPUNGE_ALLOC_TYPE_MALLOC) || (q->allocType == SPUNGE_ALLOC_TYPE_CALLOC)) {
116         SpungeFree(q, q->allocType);
117     }
118 }
119 
FillpQueueValidOnes(FillpQueue * q)120 static __inline FILLP_ULONG FillpQueueValidOnes(FillpQueue *q)
121 {
122     if (q == FILLP_NULL_PTR) {
123         return 0;
124     }
125     return FillpRingValidOnes(&q->ring);
126 }
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 
132 #endif /* FILLP_QUEUE_H */
133