QP/C++
qmp_init.cpp
Go to the documentation of this file.
00001 
00002 // Product: QF/C++
00003 // Last Updated for Version: 4.3.00
00004 // Date of the Last Update:  Nov 01, 2011
00005 //
00006 //                    Q u a n t u m     L e a P s
00007 //                    ---------------------------
00008 //                    innovating embedded systems
00009 //
00010 // Copyright (C) 2002-2011 Quantum Leaps, LLC. All rights reserved.
00011 //
00012 // This software may be distributed and modified under the terms of the GNU
00013 // General Public License version 2 (GPL) as published by the Free Software
00014 // Foundation and appearing in the file GPL.TXT included in the packaging of
00015 // this file. Please note that GPL Section 2[b] requires that all works based
00016 // on this software must also be made publicly available under the terms of
00017 // the GPL ("Copyleft").
00018 //
00019 // Alternatively, this software may be distributed and modified under the
00020 // terms of Quantum Leaps commercial licenses, which expressly supersede
00021 // the GPL and are specifically designed for licensees interested in
00022 // retaining the proprietary status of their code.
00023 //
00024 // Contact information:
00025 // Quantum Leaps Web site:  http://www.quantum-leaps.com
00026 // e-mail:                  info@quantum-leaps.com
00028 #include "qf_pkg.h"
00029 #include "qassert.h"
00030 
00034 
00035 #ifdef Q_USE_NAMESPACE
00036 namespace QP {
00037 #endif
00038 
00039 Q_DEFINE_THIS_MODULE(qmp_init)
00040 
00041 //............................................................................
00042 void QMPool::init(void *poolSto, uint32_t poolSize, QMPoolSize blockSize) {
00043     // The memory block must be valid
00044     // and the poolSize must fit at least one free block
00045     // and the blockSize must not be too close to the top of the dynamic range
00046     Q_REQUIRE((poolSto != (void *)0)
00047               && (poolSize >= (uint32_t)sizeof(QFreeBlock))
00048               && ((QMPoolSize)(blockSize + (QMPoolSize)sizeof(QFreeBlock))
00049                     > blockSize));
00050 
00051     m_free = poolSto;
00052 
00053                 // round up the blockSize to fit an integer number of pointers
00054     m_blockSize = (QMPoolSize)sizeof(QFreeBlock);       // start with just one
00055     uint32_t nblocks = (uint32_t)1;// # free blocks that fit in a memory block
00056     while (m_blockSize < blockSize) {
00057         m_blockSize += (QMPoolSize)sizeof(QFreeBlock);
00058         ++nblocks;
00059     }
00060     blockSize = m_blockSize;          // use the rounded-up value from here on
00061 
00062                // the whole pool buffer must fit at least one rounded-up block
00063     Q_ASSERT(poolSize >= (uint32_t)blockSize);
00064 
00065                                 // chain all blocks together in a free-list...
00066     poolSize -= (uint32_t)blockSize;             // don't chain the last block
00067     m_nTot     = (QMPoolCtr)1;             // one (the last) block in the pool
00068     QFreeBlock *fb = (QFreeBlock *)m_free;//start at the head of the free list
00069     while (poolSize >= (uint32_t)blockSize) {
00070         fb->m_next = &fb[nblocks];                      // setup the next link
00071         fb = fb->m_next;                              // advance to next block
00072         poolSize -= (uint32_t)blockSize;     // reduce the available pool size
00073         ++m_nTot;                     // increment the number of blocks so far
00074     }
00075 
00076     fb->m_next = (QFreeBlock *)0;              // the last link points to NULL
00077     m_nFree    = m_nTot;                                // all blocks are free
00078     m_nMin     = m_nTot;                  // the minimum number of free blocks
00079     m_start    = poolSto;               // the original start this pool buffer
00080     m_end      = fb;                            // the last block in this pool
00081 
00082     QS_CRIT_STAT_
00083     QS_BEGIN_(QS_QF_MPOOL_INIT, QS::mpObj_, m_start)
00084         QS_OBJ_(m_start);                   // the memory managed by this pool
00085         QS_MPC_(m_nTot);                         // the total number of blocks
00086     QS_END_()
00087 }
00088 
00089 #ifdef Q_USE_NAMESPACE
00090 }                                                              // namespace QP
00091 #endif
00092