2016-06-08 22:25:48 +02:00
|
|
|
/** Memory pool for fixed size objects.
|
2016-01-14 22:59:57 +01:00
|
|
|
*
|
2016-06-08 22:25:48 +02:00
|
|
|
* This datastructure is based on a lock-less stack (lstack).
|
2016-01-14 22:59:57 +01:00
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-03-03 20:20:13 -04:00
|
|
|
*********************************************************************************/
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2017-02-16 09:04:12 -03:00
|
|
|
#pragma once
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
#include <stddef.h>
|
2016-09-22 21:20:21 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2018-06-29 09:06:04 +02:00
|
|
|
#include <villas/queue.h>
|
|
|
|
#include <villas/common.h>
|
|
|
|
#include <villas/memory.h>
|
2016-09-22 21:20:21 -04:00
|
|
|
|
2018-06-28 13:42:50 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-08-28 23:55:51 -04:00
|
|
|
/** A thread-safe memory pool */
|
2016-01-14 22:59:57 +01:00
|
|
|
struct pool {
|
2018-07-04 15:07:54 +02:00
|
|
|
enum state state;
|
|
|
|
|
2018-06-29 08:37:14 +02:00
|
|
|
off_t buffer_off; /**< Offset from the struct address to the underlying memory area */
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-06-29 08:37:14 +02:00
|
|
|
size_t len; /**< Length of the underlying memory area */
|
|
|
|
size_t blocksz; /**< Length of a block in bytes */
|
|
|
|
size_t alignment; /**< Alignment of a block in bytes */
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-16 02:33:36 -04:00
|
|
|
struct queue queue; /**< The queue which is used to keep track of free blocks */
|
2016-01-14 22:59:57 +01:00
|
|
|
};
|
|
|
|
|
2017-05-05 19:24:16 +00:00
|
|
|
#define INLINE static inline __attribute__((unused))
|
2016-06-08 22:25:48 +02:00
|
|
|
|
2017-03-12 17:13:37 -03:00
|
|
|
/** Initiazlize a pool
|
|
|
|
*
|
|
|
|
* @param[inout] p The pool data structure.
|
|
|
|
* @param[in] cnt The total number of blocks which are reserverd by this pool.
|
|
|
|
* @param[in] blocksz The size in bytes per block.
|
|
|
|
* @param[in] mem The type of memory which should be used for this pool.
|
|
|
|
* @retval 0 The pool has been successfully initialized.
|
|
|
|
* @retval <>0 There was an error during the pool initialization.
|
|
|
|
*/
|
2018-07-02 14:17:50 +02:00
|
|
|
int pool_init(struct pool *p, size_t cnt, size_t blocksz, struct memory_type *mem);
|
2016-06-08 22:25:48 +02:00
|
|
|
|
|
|
|
/** Destroy and release memory used by pool. */
|
2016-08-28 23:55:51 -04:00
|
|
|
int pool_destroy(struct pool *p);
|
2016-06-08 22:25:48 +02:00
|
|
|
|
2017-03-12 17:13:37 -03:00
|
|
|
/** Pop up to \p cnt values from the stack an place them in the array \p blocks.
|
|
|
|
*
|
|
|
|
* @return The number of blocks actually retrieved from the pool.
|
|
|
|
* This number can be smaller than the requested \p cnt blocks
|
|
|
|
* in case the pool currently holds less than \p cnt blocks.
|
|
|
|
*/
|
2018-06-29 08:37:14 +02:00
|
|
|
INLINE ssize_t pool_get_many(struct pool *p, void *blocks[], size_t cnt)
|
|
|
|
{
|
2016-10-16 02:33:36 -04:00
|
|
|
return queue_pull_many(&p->queue, blocks, cnt);
|
2016-06-08 22:25:48 +02:00
|
|
|
}
|
|
|
|
|
2017-03-12 17:13:37 -03:00
|
|
|
/** Push \p cnt values which are giving by the array values to the stack. */
|
2018-06-29 08:37:14 +02:00
|
|
|
INLINE ssize_t pool_put_many(struct pool *p, void *blocks[], size_t cnt)
|
|
|
|
{
|
2016-10-16 02:33:36 -04:00
|
|
|
return queue_push_many(&p->queue, blocks, cnt);
|
2016-06-08 22:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Get a free memory block from pool. */
|
2018-06-29 08:37:14 +02:00
|
|
|
INLINE void * pool_get(struct pool *p)
|
|
|
|
{
|
2016-08-28 23:55:51 -04:00
|
|
|
void *ptr;
|
2016-10-16 02:33:36 -04:00
|
|
|
return queue_pull(&p->queue, &ptr) == 1 ? ptr : NULL;
|
2016-06-08 22:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Release a memory block back to the pool. */
|
2018-06-29 08:37:14 +02:00
|
|
|
INLINE int pool_put(struct pool *p, void *buf)
|
|
|
|
{
|
2016-10-16 02:33:36 -04:00
|
|
|
return queue_push(&p->queue, buf);
|
2017-07-24 19:33:35 +02:00
|
|
|
}
|
2018-06-28 13:42:50 +02:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|