qpcpp/qf/source/qmp_init.cpp

107 lines
4.7 KiB
C++
Raw Normal View History

2013-10-10 20:01:51 -04:00
//****************************************************************************
2012-08-14 18:00:48 -04:00
// Product: QF/C++
2013-12-30 17:41:15 -05:00
// Last Updated for Version: 5.2.0
// Date of the Last Update: Dec 27, 2013
2012-08-14 18:00:48 -04:00
//
// Q u a n t u m L e a P s
// ---------------------------
// innovating embedded systems
//
2013-10-10 20:01:51 -04:00
// Copyright (C) 2002-2013 Quantum Leaps, LLC. All rights reserved.
2012-08-14 18:00:48 -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:00:48 -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:
// Quantum Leaps Web sites: http://www.quantum-leaps.com
// http://www.state-machine.com
// e-mail: info@quantum-leaps.com
2013-10-10 20:01:51 -04:00
//****************************************************************************
2012-08-14 18:00:48 -04:00
#include "qf_pkg.h"
#include "qassert.h"
/// \file
/// \ingroup qf
2013-12-30 17:41:15 -05:00
/// \brief QMPool::QMPool() and QMPool::init() implementation.
2012-08-14 18:00:48 -04:00
2013-10-10 20:01:51 -04:00
namespace QP {
2012-08-14 18:00:48 -04:00
Q_DEFINE_THIS_MODULE("qmp_init")
//............................................................................
2013-12-30 17:41:15 -05:00
QMPool::QMPool(void)
: m_start(null_void),
m_end(null_void),
m_free_head(null_void),
m_blockSize(static_cast<QMPoolSize>(0)),
m_nTot(static_cast<QMPoolCtr>(0)),
m_nFree(static_cast<QMPoolCtr>(0)),
m_nMin(static_cast<QMPoolCtr>(0))
{}
//............................................................................
void QMPool::init(void * const poolSto, uint_t poolSize, uint_t blockSize) {
2012-08-14 18:00:48 -04:00
// The memory block must be valid
// and the poolSize must fit at least one free block
// and the blockSize must not be too close to the top of the dynamic range
Q_REQUIRE((poolSto != null_void)
2013-12-30 17:41:15 -05:00
&& (poolSize >= static_cast<uint_t>(sizeof(QFreeBlock)))
&& ((blockSize + static_cast<uint_t>(sizeof(QFreeBlock)))
2012-08-14 18:00:48 -04:00
> blockSize));
2013-10-10 20:01:51 -04:00
m_free_head = poolSto;
2012-08-14 18:00:48 -04:00
// round up the blockSize to fit an integer number of pointers
m_blockSize = static_cast<QMPoolSize>(sizeof(QFreeBlock));//start with one
2013-12-30 17:41:15 -05:00
uint_t nblocks = u_1; //# free blocks in a mem block
while (m_blockSize < static_cast<QMPoolSize>(blockSize)) {
2012-08-14 18:00:48 -04:00
m_blockSize += static_cast<QMPoolSize>(sizeof(QFreeBlock));
++nblocks;
}
2013-12-30 17:41:15 -05:00
blockSize = static_cast<uint_t>(m_blockSize); // use rounded-up value
2012-08-14 18:00:48 -04:00
// the whole pool buffer must fit at least one rounded-up block
2013-12-30 17:41:15 -05:00
Q_ASSERT(poolSize >= blockSize);
2012-08-14 18:00:48 -04:00
// chain all blocks together in a free-list...
2013-12-30 17:41:15 -05:00
poolSize -= blockSize; // don't count the last block
2012-08-14 18:00:48 -04:00
m_nTot = static_cast<QMPoolCtr>(1); // one (the last) block in the pool
//start at the head of the free list
2013-10-10 20:01:51 -04:00
QFreeBlock *fb = static_cast<QFreeBlock *>(m_free_head);
2013-12-30 17:41:15 -05:00
while (poolSize >= blockSize) {
2012-08-14 18:00:48 -04:00
fb->m_next = &QF_PTR_AT_(fb, nblocks); // setup the next link
fb = fb->m_next; // advance to next block
2013-12-30 17:41:15 -05:00
poolSize -= blockSize; // reduce the available pool size
2012-08-14 18:00:48 -04:00
++m_nTot; // increment the number of blocks so far
}
fb->m_next = static_cast<QFreeBlock *>(0); // the last link points to NULL
m_nFree = m_nTot; // all blocks are free
m_nMin = m_nTot; // the minimum number of free blocks
m_start = poolSto; // the original start this pool buffer
m_end = fb; // the last block in this pool
QS_CRIT_STAT_
2013-10-10 20:01:51 -04:00
QS_BEGIN_(QS_QF_MPOOL_INIT, QS::priv_.mpObjFilter, m_start)
2012-08-14 18:00:48 -04:00
QS_OBJ_(m_start); // the memory managed by this pool
QS_MPC_(m_nTot); // the total number of blocks
QS_END_()
}
2013-10-10 20:01:51 -04:00
} // namespace QP