1 /******************************************************************************
3 * Copyright (c) 2021 Intel.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 *******************************************************************************/
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
27 /*static void pool_mutex_destroy(pthread_mutex_t* pMutex)
29 pthread_mutex_destroy(pMutex);
32 static void pool_mutex_init(pthread_mutex_t* pMutex)
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);
41 static void pool_mutex_lock(pthread_mutex_t* pMutex)
43 pthread_mutex_lock(pMutex);
46 static void pool_mutex_unlock(pthread_mutex_t* pMutex)
48 pthread_mutex_unlock(pMutex);
52 unsigned int PoolInit (PPOOL pPool, void * pStorage, unsigned int nBlockNum, unsigned int nBlockSize, unsigned long long* pFreePtr, unsigned long long* pUsedPtr)
56 memset (pPool, 0, sizeof (*pPool));
59 mutex_init(&pPool->lock);
61 pool_mutex_init(&pPool->lock);
64 pPool->StoragePtr = (unsigned char*)pStorage;
65 pPool->BlockSize = nBlockSize;
66 pPool->BlockNum = nBlockNum;
68 pPool->FreePtr = pFreePtr;
69 pPool->UsedPtr = pUsedPtr;
71 // to put the indexes to the free storage
77 PoolFree (pPool, pPool->StoragePtr + (pPool->BlockSize * i));
84 void* PoolAlloc(PPOOL pPool)
86 unsigned long long nIndex;
90 mutex_lock(&pPool->lock);
92 pool_mutex_lock(&pPool->lock);
95 if (pPool->FreeGet == pPool->FreePut){
97 mutex_unlock(&pPool->lock);
99 pool_mutex_unlock(&pPool->lock);
104 nIndex = pPool->FreePtr[pPool->FreeGet++];
106 if (pPool->FreeGet >= (pPool->BlockNum+1))
109 ret = pPool->StoragePtr + (pPool->BlockSize * nIndex);
112 mutex_unlock(&pPool->lock);
114 pool_mutex_unlock(&pPool->lock);
120 unsigned int PoolFree(PPOOL pPool, void * pBlock)
122 unsigned long long index;
125 mutex_lock(&pPool->lock);
127 pool_mutex_lock(&pPool->lock);
130 index = (U64)((U64)pBlock - (U64)pPool->StoragePtr) / pPool->BlockSize;
132 pPool->FreePtr [pPool->FreePut ++] = index;
134 if (pPool->FreePut >= (pPool->BlockNum+1))
138 mutex_unlock(&pPool->lock);
140 pool_mutex_unlock(&pPool->lock);
146 unsigned int PoolGetFreeNum(PPOOL pPool)
153 if (pPool->FreePut >= pPool->FreeGet)
155 nCount = pPool->FreePut - pPool->FreeGet;
159 // the queue size is bigger on one element than a partition
160 // to prevent data loss
162 nCount = (pPool->BlockNum+1) - (pPool->FreeGet - pPool->FreePut);
168 unsigned int PoolGetAllocNum(PPOOL pPool)
175 if (pPool->UsedPut >= pPool->UsedGet)
177 nCount = pPool->UsedPut - pPool->UsedGet;
181 // the queue size is bigger on one element than a partition
182 // to prevent data loss
184 nCount = (pPool->BlockNum+1) - (pPool->UsedGet - pPool->UsedPut);