FAPI TM, WLS_LIB and ODULOW documentation
[o-du/phy.git] / wls_lib / testapp / pool.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2019 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19 #ifdef __KERNEL__
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #endif
23 #include <string.h>
24 #include <pthread.h>
25 #include "pool.h"
26
27 /*static void pool_mutex_destroy(pthread_mutex_t* pMutex)
28 {
29     pthread_mutex_destroy(pMutex);
30 }*/
31
32 static void pool_mutex_init(pthread_mutex_t* pMutex)
33 {
34    pthread_mutexattr_t prior;
35    pthread_mutexattr_init(&prior);
36    pthread_mutexattr_setprotocol(&prior, PTHREAD_PRIO_INHERIT);
37    pthread_mutex_init(pMutex, &prior);
38    pthread_mutexattr_destroy(&prior);
39 }
40
41 static void pool_mutex_lock(pthread_mutex_t* pMutex)
42 {
43     pthread_mutex_lock(pMutex);
44 }
45
46 static void pool_mutex_unlock(pthread_mutex_t* pMutex)
47 {
48     pthread_mutex_unlock(pMutex);
49 }
50
51
52 unsigned int PoolInit (PPOOL pPool, void * pStorage, unsigned int nBlockNum, unsigned int nBlockSize, unsigned long long* pFreePtr, unsigned long long* pUsedPtr)
53 {
54     unsigned int i;
55
56     memset (pPool, 0, sizeof (*pPool));
57
58 #ifdef __KERNEL__
59     mutex_init(&pPool->lock);
60 #else
61     pool_mutex_init(&pPool->lock);
62 #endif
63
64     pPool->StoragePtr = (unsigned char*)pStorage;
65     pPool->BlockSize  = nBlockSize;
66     pPool->BlockNum   = nBlockNum;
67
68     pPool->FreePtr = pFreePtr;
69     pPool->UsedPtr = pUsedPtr;
70
71     // to put the indexes to the free storage
72
73     i = 0;
74
75     while (i < nBlockNum)
76     {
77         PoolFree (pPool, pPool->StoragePtr + (pPool->BlockSize * i));
78         i++;
79     }
80
81     return 0;
82 }
83
84 void* PoolAlloc(PPOOL pPool)
85 {
86     unsigned long long nIndex;
87     void* ret  = NULL;
88
89 #ifdef __KERNEL__
90     mutex_lock(&pPool->lock);
91 #else
92     pool_mutex_lock(&pPool->lock);
93 #endif
94
95     if (pPool->FreeGet == pPool->FreePut){
96 #ifdef __KERNEL__
97         mutex_unlock(&pPool->lock);
98 #else
99         pool_mutex_unlock(&pPool->lock);
100 #endif
101         return ret;
102     }
103
104     nIndex = pPool->FreePtr[pPool->FreeGet++];
105
106     if (pPool->FreeGet >= (pPool->BlockNum+1))
107         pPool->FreeGet = 0;
108
109     ret = pPool->StoragePtr + (pPool->BlockSize * nIndex);
110
111 #ifdef __KERNEL__
112     mutex_unlock(&pPool->lock);
113 #else
114     pool_mutex_unlock(&pPool->lock);
115 #endif
116
117     return ret;
118 }
119
120 unsigned int PoolFree(PPOOL pPool, void * pBlock)
121 {
122     unsigned long long index;
123
124 #ifdef __KERNEL__
125     mutex_lock(&pPool->lock);
126 #else
127     pool_mutex_lock(&pPool->lock);
128 #endif
129
130     index = (U64)((U64)pBlock - (U64)pPool->StoragePtr) / pPool->BlockSize;
131
132     pPool->FreePtr [pPool->FreePut ++] = index;
133
134     if (pPool->FreePut >= (pPool->BlockNum+1))
135         pPool->FreePut = 0;
136
137 #ifdef __KERNEL__
138     mutex_unlock(&pPool->lock);
139 #else
140     pool_mutex_unlock(&pPool->lock);
141 #endif
142
143     return 1;
144 }
145
146 unsigned int PoolGetFreeNum(PPOOL pPool)
147 {
148     unsigned int nCount;
149
150     if (pPool==NULL)
151         return 0;
152
153     if (pPool->FreePut >= pPool->FreeGet)
154     {
155         nCount = pPool->FreePut - pPool->FreeGet;
156     }
157     else
158     {
159         // the queue size is bigger on one element than a partition
160         // to prevent data loss
161
162         nCount = (pPool->BlockNum+1) - (pPool->FreeGet - pPool->FreePut);
163     }
164
165     return nCount;
166 }
167
168 unsigned int PoolGetAllocNum(PPOOL pPool)
169 {
170     unsigned int nCount;
171
172     if (pPool==NULL)
173         return 0;
174
175     if (pPool->UsedPut >= pPool->UsedGet)
176     {
177         nCount = pPool->UsedPut - pPool->UsedGet;
178     }
179     else
180     {
181         // the queue size is bigger on one element than a partition
182         // to prevent data loss
183
184         nCount = (pPool->BlockNum+1) - (pPool->UsedGet - pPool->UsedPut);
185     }
186
187     return nCount;
188 }
189