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
|
2022-12-14 17:41:58 +01:00
|
|
|
* @author Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2022-07-04 18:20:03 +02:00
|
|
|
* @license Apache 2.0
|
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
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstddef>
|
2016-09-22 21:20:21 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2018-06-29 09:06:04 +02:00
|
|
|
#include <villas/queue.h>
|
2020-03-04 12:41:55 +01:00
|
|
|
#include <villas/common.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node/memory.hpp>
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
2016-09-22 21:20:21 -04:00
|
|
|
|
2016-08-28 23:55:51 -04:00
|
|
|
/** A thread-safe memory pool */
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Pool {
|
2019-06-23 16:13:23 +02:00
|
|
|
enum State state;
|
2018-07-04 15:07:54 +02:00
|
|
|
|
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
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
struct CQueue queue; /**< The queue which is used to keep track of free blocks */
|
2016-01-14 22:59:57 +01:00
|
|
|
};
|
|
|
|
|
2018-08-13 12:32:04 +02:00
|
|
|
#define pool_buffer(p) ((char *) (p) + (p)->buffer_off)
|
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.
|
|
|
|
*/
|
2021-08-10 10:12:48 -04:00
|
|
|
int pool_init(struct Pool *p, size_t cnt, size_t blocksz, struct memory::Type *mem = memory::default_type) __attribute__ ((warn_unused_result));
|
2016-06-08 22:25:48 +02:00
|
|
|
|
|
|
|
/** Destroy and release memory used by pool. */
|
2021-08-10 10:12:48 -04:00
|
|
|
int pool_destroy(struct Pool *p) __attribute__ ((warn_unused_result));
|
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.
|
|
|
|
*/
|
2022-11-11 03:16:53 -05:00
|
|
|
ssize_t pool_get_many(struct Pool *p, void *blocks[], size_t 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. */
|
2022-11-11 03:16:53 -05:00
|
|
|
ssize_t pool_put_many(struct Pool *p, void *blocks[], size_t cnt);
|
2016-06-08 22:25:48 +02:00
|
|
|
|
|
|
|
/** Get a free memory block from pool. */
|
2022-11-11 03:16:53 -05:00
|
|
|
void * pool_get(struct Pool *p);
|
2016-06-08 22:25:48 +02:00
|
|
|
|
|
|
|
/** Release a memory block back to the pool. */
|
2022-11-11 03:16:53 -05:00
|
|
|
int pool_put(struct Pool *p, void *buf);
|
2021-08-10 10:12:48 -04:00
|
|
|
|
|
|
|
} /* namespace node */
|
|
|
|
} /* namespace villas */
|