|
Packit Service |
310c69 |
/*
|
|
Packit Service |
310c69 |
* Copyright (c) 2020 Red Hat, Inc.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This program is free software; you can redistribute it and/or
|
|
Packit Service |
310c69 |
* modify it under the terms of the GNU General Public License
|
|
Packit Service |
310c69 |
* as published by the Free Software Foundation; either version 2
|
|
Packit Service |
310c69 |
* of the License, or (at your option) any later version.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
310c69 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
310c69 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
310c69 |
* GNU General Public License for more details.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
310c69 |
* along with this program; if not, write to the Free Software
|
|
Packit Service |
310c69 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit Service |
310c69 |
* 02110-1301, USA.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* $Id: //eng/vdo-releases/aluminum/src/c++/vdo/kernel/bufferPool.h#1 $
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
#ifndef BUFFERPOOL_H
|
|
Packit Service |
310c69 |
#define BUFFERPOOL_H
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*
|
|
Packit Service |
310c69 |
* We need bug.h because in 3.10, kernel.h (indirectly) defines
|
|
Packit Service |
310c69 |
* ARRAY_SIZE as a macro which (indirectly and conditionally) uses
|
|
Packit Service |
310c69 |
* BUILD_BUG_ON_ZERO, which is defined in bug.h, which is *not*
|
|
Packit Service |
310c69 |
* included. In earlier versions like 3.2 it Just Worked.
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
#include <linux/bug.h>
|
|
Packit Service |
310c69 |
#include <linux/kernel.h>
|
|
Packit Service |
310c69 |
#include <linux/types.h>
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
typedef struct bufferPool BufferPool;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
typedef int BufferAllocateFunction(void *poolData, void **dataPtr);
|
|
Packit Service |
310c69 |
typedef void BufferFreeFunction(void *poolData, void *data);
|
|
Packit Service |
310c69 |
typedef void BufferDumpFunction(void *poolData, void *data);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Creates a generic pool of buffer data. The elements in the pool are
|
|
Packit Service |
310c69 |
* allocated up front and placed on a free list, which manages the
|
|
Packit Service |
310c69 |
* reuse of the individual buffers in the pool.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param [in] poolName Name of the pool
|
|
Packit Service |
310c69 |
* @param [in] size The number of elements to create for this pool
|
|
Packit Service |
310c69 |
* @param [in] allocateFunction The function to call to create the actual data
|
|
Packit Service |
310c69 |
* for each element
|
|
Packit Service |
310c69 |
* @param [in] freeFunction The function to call to free the actual data
|
|
Packit Service |
310c69 |
* for each element
|
|
Packit Service |
310c69 |
* @param [in] dumpFunction The function to call to dump the actual data
|
|
Packit Service |
310c69 |
* for each element into the log
|
|
Packit Service |
310c69 |
* @param [in] poolData A pointer to the pool's associated data
|
|
Packit Service |
310c69 |
* @param [out] poolPtr A pointer to hold the pool that was created
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return a success or error code
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
int makeBufferPool(const char *poolName,
|
|
Packit Service |
310c69 |
unsigned int size,
|
|
Packit Service |
310c69 |
BufferAllocateFunction *allocateFunction,
|
|
Packit Service |
310c69 |
BufferFreeFunction *freeFunction,
|
|
Packit Service |
310c69 |
BufferDumpFunction *dumpFunction,
|
|
Packit Service |
310c69 |
void *poolData,
|
|
Packit Service |
310c69 |
BufferPool **poolPtr)
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result));
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Free a buffer pool and null out the reference to it. This will free
|
|
Packit Service |
310c69 |
* all the elements of the pool as well.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param [in] poolPtr The reference to the pool to free
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
void freeBufferPool(BufferPool **poolPtr);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Dump a buffer pool to the log.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param [in] pool The buffer pool to allocate from
|
|
Packit Service |
310c69 |
* @param [in] dumpElements True for complete output, or false for a
|
|
Packit Service |
310c69 |
* one-line summary
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
void dumpBufferPool(BufferPool *pool, bool dumpElements);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Acquires a free buffer from the free list of the pool and
|
|
Packit Service |
310c69 |
* returns it's associated data.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param [in] pool The buffer pool to allocate from
|
|
Packit Service |
310c69 |
* @param [out] dataPtr A pointer to hold the buffer data
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return a success or error code
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
int allocBufferFromPool(BufferPool *pool, void **dataPtr)
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result));
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Returns a buffer to the free list of a pool
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param [in] pool The buffer pool to return the buffer to
|
|
Packit Service |
310c69 |
* @param [in] data The buffer data to return
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
void freeBufferToPool(BufferPool *pool, void *data);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Returns a set of buffers to the free list of a pool
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param [in] pool The buffer pool to return the buffer to
|
|
Packit Service |
310c69 |
* @param [in] data The buffer data to return
|
|
Packit Service |
310c69 |
* @param [in] count Number of entries in the data array
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
void freeBuffersToPool(BufferPool *pool, void **data, int count);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Control structure for freeing (releasing back to the pool) pointers
|
|
Packit Service |
310c69 |
* in batches.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* Since the objects stored in a buffer pool are completely opaque,
|
|
Packit Service |
310c69 |
* some external data structure is needed to manage a collection of
|
|
Packit Service |
310c69 |
* them. This is a simple helper for doing that, since we're freeing
|
|
Packit Service |
310c69 |
* batches of objects in a couple different places. Within the pool
|
|
Packit Service |
310c69 |
* itself there's a pair of linked lists, but getting at them requires
|
|
Packit Service |
310c69 |
* the locking that we're trying to minimize.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* We collect pointers until the array is full or until there are no
|
|
Packit Service |
310c69 |
* more available, and we call freeBuffersToPool to release a batch
|
|
Packit Service |
310c69 |
* all at once.
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
typedef struct freeBufferPointers {
|
|
Packit Service |
310c69 |
BufferPool *pool;
|
|
Packit Service |
310c69 |
int index;
|
|
Packit Service |
310c69 |
void *pointers[30]; // size is arbitrary
|
|
Packit Service |
310c69 |
} FreeBufferPointers;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Initialize the control structure for batching buffer pointers to be
|
|
Packit Service |
310c69 |
* released to their pool.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param [out] fbp The (caller-allocated) control structure
|
|
Packit Service |
310c69 |
* @param [in] pool The buffer pool to return objects to.
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static inline void initFreeBufferPointers(FreeBufferPointers *fbp,
|
|
Packit Service |
310c69 |
BufferPool *pool)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
fbp->index = 0;
|
|
Packit Service |
310c69 |
fbp->pool = pool;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Release any buffers left in the collection.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param [in] fbp The control structure
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static inline void freeBufferPointers(FreeBufferPointers *fbp)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
freeBuffersToPool(fbp->pool, fbp->pointers, fbp->index);
|
|
Packit Service |
310c69 |
fbp->index = 0;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Add another buffer pointer to the collection, and if we're full,
|
|
Packit Service |
310c69 |
* release the whole batch to the pool.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param [in] fbp The control structure
|
|
Packit Service |
310c69 |
* @param [in] pointer The buffer pointer to release
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static inline void addFreeBufferPointer(FreeBufferPointers *fbp,
|
|
Packit Service |
310c69 |
void *pointer)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
fbp->pointers[fbp->index] = pointer;
|
|
Packit Service |
310c69 |
fbp->index++;
|
|
Packit Service |
310c69 |
if (fbp->index == ARRAY_SIZE(fbp->pointers)) {
|
|
Packit Service |
310c69 |
freeBufferPointers(fbp);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#endif /* BUFFERPOOL_H */
|