Linux Face / QPFramework
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers qmp_init.cpp Source File

qmp_init.cpp

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////////
00002 // Product: QF/C++
00003 // Last Updated for Version: 4.0.00
00004 // Date of the Last Update:  Apr 07, 2008
00005 //
00006 //                    Q u a n t u m     L e a P s
00007 //                    ---------------------------
00008 //                    innovating embedded systems
00009 //
00010 // Copyright (C) 2002-2008 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
00027 //////////////////////////////////////////////////////////////////////////////
00028 #include "qf_pkg.h"
00029 #include "qassert.h"
00030 
00031 Q_DEFINE_THIS_MODULE(qmp_init)
00032 
00033 /// \file
00034 /// \ingroup qf
00035 /// \brief QMPool::init() implementation.
00036 
00037 //............................................................................
00038 void QMPool::init(void *poolSto, uint32_t poolSize, QMPoolSize blockSize) {
00039     // The memory block must be valid
00040     // and the poolSize must fit at least one free block
00041     // and the blockSize must not be too close to the top of the dynamic range
00042     Q_REQUIRE((poolSto != (void *)0)
00043               && (poolSize >= (uint32_t)sizeof(QFreeBlock))
00044               && ((QMPoolSize)(blockSize + (QMPoolSize)sizeof(QFreeBlock))
00045                     > blockSize));
00046 
00047     //lint -e923                       ignore MISRA Rule 45 in this expression
00048     uint32_t corr = ((uint32_t)poolSto
00049                       & ((uint32_t)sizeof(QFreeBlock) - (uint32_t)1));
00050     if (corr != (uint32_t)0) {                            // alignment needed?
00051         corr = (uint32_t)sizeof(QFreeBlock) - corr; // amount to align poolSto
00052         poolSize -= corr;                    // reduce the available pool size
00053     }
00054     //lint -e826   align the head of free list at the free block-size boundary
00055     m_free = (void *)((uint8_t *)poolSto + corr);
00056 
00057                 // round up the blockSize to fit an integer number of pointers
00058     m_blockSize = (QMPoolSize)sizeof(QFreeBlock);       // start with just one
00059     uint32_t nblocks = (uint32_t)1;// # free blocks that fit in a memory block
00060     while (m_blockSize < blockSize) {
00061         m_blockSize += (QMPoolSize)sizeof(QFreeBlock);
00062         ++nblocks;
00063     }
00064     blockSize = m_blockSize;          // use the rounded-up value from here on
00065 
00066                // the whole pool buffer must fit at least one rounded-up block
00067     Q_ASSERT(poolSize >= (uint32_t)blockSize);
00068 
00069                                 // chain all blocks together in a free-list...
00070     poolSize -= (uint32_t)blockSize;             // don't chain the last block
00071     m_nTot     = (QMPoolCtr)1;             // one (the last) block in the pool
00072     QFreeBlock *fb = (QFreeBlock *)m_free;//start at the head of the free list
00073     while (poolSize >= (uint32_t)blockSize) {
00074         fb->m_next = &fb[nblocks];                      // setup the next link
00075         fb = fb->m_next;                              // advance to next block
00076         poolSize -= (uint32_t)blockSize;     // reduce the available pool size
00077         ++m_nTot;                     // increment the number of blocks so far
00078     }
00079 
00080     fb->m_next = (QFreeBlock *)0;              // the last link points to NULL
00081     m_nFree    = m_nTot;                                // all blocks are free
00082     m_nMin     = m_nTot;                  // the minimum number of free blocks
00083     m_start    = poolSto;               // the original start this pool buffer
00084     m_end      = fb;                            // the last block in this pool
00085 
00086     QS_INT_LOCK_KEY_
00087     QS_BEGIN_(QS_QF_MPOOL_INIT, QS::mpObj_, m_start)
00088         QS_OBJ_(m_start);                   // the memory managed by this pool
00089         QS_MPC_(m_nTot);                         // the total number of blocks
00090     QS_END_()
00091 }