qpc/include/qmpool.h

165 lines
5.1 KiB
C
Raw Normal View History

2014-04-06 11:43:13 -04:00
/**
* \file
* \brief platform-independent memory pool interface.
* \ingroup qf
* \cond
******************************************************************************
* Product: QF/C
* Last updated for version 5.3.0
* Last updated on 2014-02-24
2012-08-14 18:07:04 -04:00
*
* Q u a n t u m L e a P s
* ---------------------------
* innovating embedded systems
*
2014-04-06 11:43:13 -04:00
* Copyright (C) Quantum Leaps, www.state-machine.com.
2012-08-14 18:07:04 -04:00
*
* This program is open source 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
2012-08-14 18:07:04 -04:00
* (at your option) any later version.
*
* Alternatively, this program may be distributed and modified under the
* terms of Quantum Leaps commercial licenses, which expressly supersede
* the GNU General Public License and are specifically designed for
* licensees interested in retaining the proprietary status of their code.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Contact information:
2014-04-06 11:43:13 -04:00
* Web: www.state-machine.com
* Email: info@state-machine.com
******************************************************************************
* \endcond
*/
2012-08-14 18:07:04 -04:00
#ifndef qmpool_h
#define qmpool_h
/****************************************************************************/
#ifndef QF_MPOOL_SIZ_SIZE
2014-04-06 11:43:13 -04:00
/*! macro to override the default ::QMPoolSize size.
2012-08-14 18:07:04 -04:00
* Valid values 1, 2, or 4; default 2
*/
#define QF_MPOOL_SIZ_SIZE 2
#endif
#if (QF_MPOOL_SIZ_SIZE == 1)
2014-04-06 11:43:13 -04:00
/*! The data type to store the block-size based on the macro
* #QF_MPOOL_SIZ_SIZE. */
/**
* \description
2012-08-14 18:07:04 -04:00
* The dynamic range of this data type determines the maximum size
* of blocks that can be managed by the native QF event pool.
*/
typedef uint8_t QMPoolSize;
#elif (QF_MPOOL_SIZ_SIZE == 2)
typedef uint16_t QMPoolSize;
#elif (QF_MPOOL_SIZ_SIZE == 4)
typedef uint32_t QMPoolSize;
#else
#error "QF_MPOOL_SIZ_SIZE defined incorrectly, expected 1, 2, or 4"
#endif
/****************************************************************************/
#ifndef QF_MPOOL_CTR_SIZE
2014-04-06 11:43:13 -04:00
/*! macro to override the default ::QMPoolCtr size.
2012-08-14 18:07:04 -04:00
* Valid values 1, 2, or 4; default 2
*/
#define QF_MPOOL_CTR_SIZE 2
#endif
#if (QF_MPOOL_CTR_SIZE == 1)
2014-04-06 11:43:13 -04:00
/*! The data type to store the block-counter based on the macro
* #QF_MPOOL_CTR_SIZE. */
/**
* \description
2012-08-14 18:07:04 -04:00
* The dynamic range of this data type determines the maximum number
* of blocks that can be stored in the pool.
*/
typedef uint8_t QMPoolCtr;
#elif (QF_MPOOL_CTR_SIZE == 2)
typedef uint16_t QMPoolCtr;
#elif (QF_MPOOL_CTR_SIZE == 4)
typedef uint32_t QMPoolCtr;
#else
#error "QF_MPOOL_CTR_SIZE defined incorrectly, expected 1, 2, or 4"
#endif
/****************************************************************************/
2014-04-06 11:43:13 -04:00
/*! Native QF Memory Pool */
/**
* \description
* This class describes the native QF memory pool, which can be used as
2012-08-14 18:07:04 -04:00
* the event pool for dynamic event allocation, or as a fast, deterministic
* fixed block-size heap for any other objects in your application.
*
2014-04-06 11:43:13 -04:00
* \note
* ::QMPool contains only data members for managing a memory pool, but
* does not contain the pool storage, which must be provided externally
* during the pool initialization.
2012-08-14 18:07:04 -04:00
*
2014-04-06 11:43:13 -04:00
* \note
2012-08-14 18:07:04 -04:00
* The native QF event pool is configured by defining the macro
* #QF_EPOOL_TYPE_ as QMPool in the specific QF port header file.
*/
2014-04-06 11:43:13 -04:00
typedef struct {
/*! The head of the linked list of free blocks */
2013-09-23 14:34:35 -04:00
void * volatile free_head;
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! the original start this pool */
2012-08-14 18:07:04 -04:00
void *start;
2014-04-06 11:43:13 -04:00
/*! the last memory block managed by this memory pool */
2012-08-14 18:07:04 -04:00
void *end;
2014-04-06 11:43:13 -04:00
/*! maximum block size (in bytes) */
2012-08-14 18:07:04 -04:00
QMPoolSize blockSize;
2014-04-06 11:43:13 -04:00
/*! total number of blocks */
2012-08-14 18:07:04 -04:00
QMPoolCtr nTot;
2014-04-06 11:43:13 -04:00
/*! number of free blocks remaining */
2013-09-23 14:34:35 -04:00
QMPoolCtr volatile nFree;
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! minimum number of free blocks ever present in this pool */
/**
* \description
* this attribute remembers the low watermark of the pool, which
* provides a valuable information for sizing event pools.
2013-12-30 17:37:40 -05:00
* \sa QF_getPoolMin().
2012-08-14 18:07:04 -04:00
*/
QMPoolCtr nMin;
} QMPool;
/* public functions: */
2014-04-06 11:43:13 -04:00
/*! Initializes the native QF memory pool */
2012-08-14 18:07:04 -04:00
void QMPool_init(QMPool * const me, void * const poolSto,
2014-04-06 11:43:13 -04:00
uint_fast16_t poolSize, uint_fast16_t blockSize);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Obtains a memory block from a memory pool. */
void *QMPool_get(QMPool * const me, uint_fast16_t const margin);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Recycles a memory block back to a memory pool. */
2012-08-14 18:07:04 -04:00
void QMPool_put(QMPool * const me, void *b);
2014-04-06 11:43:13 -04:00
/*! Memory pool element to allocate correctly aligned storage
2012-08-14 18:07:04 -04:00
* for QMPool class.
*/
2014-04-06 11:43:13 -04:00
/**
* \arguments
* \arg[in] \c evType_ event type (name of the subclass of QEvt)
*/
#define QF_MPOOL_EL(evType_) \
struct { void *sto_[((sizeof(evType_) - 1U)/sizeof(void*)) + 1U]; }
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
#endif /* qmpool_h */
2012-08-14 18:07:04 -04:00