QP/C
qmp_init.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * Product: QF/C
00003 * Last Updated for Version: 4.4.00
00004 * Date of the Last Update:  Jan 14, 2012
00005 *
00006 *                    Q u a n t u m     L e a P s
00007 *                    ---------------------------
00008 *                    innovating embedded systems
00009 *
00010 * Copyright (C) 2002-2012 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 
00039 /*..........................................................................*/
00040 void QMPool_init(QMPool *me, void *poolSto,
00041                  uint32_t poolSize, QMPoolSize blockSize)
00042 {
00043     QFreeBlock *fb;
00044     uint32_t nblocks;
00045     QS_CRIT_STAT_
00046 
00047     /* The memory block must be valid
00048     * and the poolSize must fit at least one free block
00049     * and the blockSize must not be too close to the top of the dynamic range
00050     */
00051     Q_REQUIRE((poolSto != (void *)0)
00052               && (poolSize >= (uint32_t)sizeof(QFreeBlock))
00053               && ((QMPoolSize)(blockSize + (QMPoolSize)sizeof(QFreeBlock))
00054                     > blockSize));
00055 
00056     me->free_head = poolSto;
00057 
00058      /* round up the blockSize to fit an integer # free blocks, no division */
00059     me->blockSize = (QMPoolSize)sizeof(QFreeBlock);  /* start with just one */
00060     nblocks = (uint32_t)1;    /* # free blocks that fit in one memory block */
00061     while (me->blockSize < blockSize) {
00062         me->blockSize += (QMPoolSize)sizeof(QFreeBlock);
00063         ++nblocks;
00064     }
00065     blockSize = me->blockSize;      /* use the rounded-up value from now on */
00066 
00067                   /* the pool buffer must fit at least one rounded-up block */
00068     Q_ASSERT(poolSize >= (uint32_t)blockSize);
00069 
00070                              /* chain all blocks together in a free-list... */
00071     poolSize -= (uint32_t)blockSize;          /* don't count the last block */
00072     me->nTot  = (QMPoolCtr)1;         /* the last block already in the pool */
00073     fb = (QFreeBlock *)me->free_head; /* start at the head of the free list */
00074     while (poolSize >= (uint32_t)blockSize) {
00075         fb->next = &QF_PTR_AT_(fb, nblocks);/*point next link to next block */
00076         fb = fb->next;                         /* advance to the next block */
00077         poolSize -= (uint32_t)blockSize;  /* reduce the available pool size */
00078         ++me->nTot;                /* increment the number of blocks so far */
00079     }
00080 
00081     fb->next  = (QFreeBlock *)0;            /* the last link points to NULL */
00082     me->nFree = me->nTot;                            /* all blocks are free */
00083     me->nMin  = me->nTot;              /* the minimum number of free blocks */
00084     me->start = poolSto;             /* the original start this pool buffer */
00085     me->end   = fb;                          /* the last block in this pool */
00086 
00087     QS_BEGIN_(QS_QF_MPOOL_INIT, QS_mpObj_, me->start)
00088         QS_OBJ_(me->start);              /* the memory managed by this pool */
00089         QS_MPC_(me->nTot);                    /* the total number of blocks */
00090     QS_END_()
00091 }
00092