Ignore:
Timestamp:
Sep 2, 2007, 10:23:54 PM (18 years ago)
Author:
bird
Message:

adding some missing stuff. kHlp\Bare\kHlpBareHeap.c

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kStuff/kHlp/Bare/kHlpBareHeap.c

    r3573 r3576  
    2525 */
    2626
    27 #define KLDRHEAP_STRICT
     27#define KHLPHEAP_STRICT
    2828
    2929/*******************************************************************************
     
    3131*******************************************************************************/
    3232#include <k/kHlpAlloc.h>
     33#include <k/kHlpString.h>
    3334
    3435#if K_OS == K_OS_OS2
     
    4950 * A heap block.
    5051 */
    51 typedef struct KLDRHEAPBLOCK
     52typedef struct KHLPHEAPBLOCK
    5253{
    5354    /** Next block in the global list. */
    54     struct KLDRHEAPBLOCK   *pNext;
     55    struct KHLPHEAPBLOCK   *pNext;
    5556    /** Previous block in the global list. */
    56     struct KLDRHEAPBLOCK   *pPrev;
     57    struct KHLPHEAPBLOCK   *pPrev;
    5758    /** The size of this block including this header. */
    5859    KSIZE                   cb;
    5960    /** The flags. */
    6061    KSIZE                   fFlags;
    61 } KLDRHEAPBLOCK, *PKLDRHEAPBLOCK;
     62} KHLPHEAPBLOCK, *PKHLPHEAPBLOCK;
    6263
    6364/** Indicates whether the block is free (set) or allocated (clear). */
    64 #define KLDRHEAPBLOCK_FLAG_FREE     ((KSIZE)1)
     65#define KHLPHEAPBLOCK_FLAG_FREE     ((KSIZE)1)
    6566/** Valid flag mask. */
    66 #define KLDRHEAPBLOCK_FLAG_MASK     ((KSIZE)1)
     67#define KHLPHEAPBLOCK_FLAG_MASK     ((KSIZE)1)
    6768
    6869/** Checks if the block is freed. */
    69 #define KLDRHEAPBLOCK_IS_FREE(pB)       ( (pB)->fFlags & KLDRHEAPBLOCK_FLAG_FREE )
     70#define KHLPHEAPBLOCK_IS_FREE(pB)       ( (pB)->fFlags & KHLPHEAPBLOCK_FLAG_FREE )
    7071/** Check if the block is allocated. */
    71 #define KLDRHEAPBLOCK_IS_ALLOCATED(pB)  !KLDRHEAPBLOCK_IS_FREE(pB)
     72#define KHLPHEAPBLOCK_IS_ALLOCATED(pB)  !KHLPHEAPBLOCK_IS_FREE(pB)
    7273/** Checks if the two blocks are adjacent.
    7374 * Assumes pB1 < pB2. */
    74 #define KLDRHEAPBLOCK_IS_ADJACENT(pB1, pB2) \
     75#define KHLPHEAPBLOCK_IS_ADJACENT(pB1, pB2) \
    7576    ( ((KUPTR)(pB1) + (pB1)->cb) == (KUPTR)(pB2) )
    7677
    7778/** The block alignment. */
    78 #define KLDRHEAPBLOCK_ALIGNMENT     sizeof(KLDRHEAPBLOCK)
    79 
    80 /** @def KLDRHEAP_ASSERT
     79#define KHLPHEAPBLOCK_ALIGNMENT     sizeof(KHLPHEAPBLOCK)
     80
     81/** @def KHLPHEAP_ASSERT
    8182 * Heap assertion. */
    82 /** @def KLDRHEAP_ASSERT_BLOCK
     83/** @def KHLPHEAP_ASSERT_BLOCK
    8384 * Assert that a heap block is valid. */
    84 /** @def KLDRHEAP_ASSERT_FREE
     85/** @def KHLPHEAP_ASSERT_FREE
    8586 * Assert that a heap free block is valid. */
    86 #ifdef KLDRHEAP_STRICT
    87 # define KLDRHEAP_ASSERT(expr)      kHlpAssert(expr)
    88 
    89 # define KLDRHEAP_ASSERT_BLOCK(pHeap, pBlock) \
     87#ifdef KHLPHEAP_STRICT
     88# define KHLPHEAP_ASSERT(expr)      kHlpAssert(expr)
     89
     90# define KHLPHEAP_ASSERT_BLOCK(pHeap, pBlock) \
    9091    do { \
    91         KLDRHEAP_ASSERT(!((pBlock)->fFlags & ~KLDRHEAPBLOCK_FLAG_MASK)); \
    92         KLDRHEAP_ASSERT(!((pBlock)->cb & (KLDRHEAPBLOCK_ALIGNMENT - 1))); \
    93         KLDRHEAP_ASSERT((KUPTR)(pBlock)->pPrev < (KUPTR)(pBlock)); \
    94         KLDRHEAP_ASSERT((KUPTR)(pBlock)->pNext > (KUPTR)(pBlock) || !(pBlock)->pNext); \
     92        KHLPHEAP_ASSERT(!((pBlock)->fFlags & ~KHLPHEAPBLOCK_FLAG_MASK)); \
     93        KHLPHEAP_ASSERT(!((pBlock)->cb & (KHLPHEAPBLOCK_ALIGNMENT - 1))); \
     94        KHLPHEAP_ASSERT((KUPTR)(pBlock)->pPrev < (KUPTR)(pBlock)); \
     95        KHLPHEAP_ASSERT((KUPTR)(pBlock)->pNext > (KUPTR)(pBlock) || !(pBlock)->pNext); \
    9596    } while (0)
    9697
    97 # define KLDRHEAP_ASSERT_FREE(pHeap, pFree) \
     98# define KHLPHEAP_ASSERT_FREE(pHeap, pFree) \
    9899    do { \
    99         KLDRHEAP_ASSERT_BLOCK(pHeap, &(pFree)->Core); \
    100         KLDRHEAP_ASSERT((KUPTR)(pFree)->pPrev < (KUPTR)(pFree)); \
    101         KLDRHEAP_ASSERT((KUPTR)(pFree)->pNext > (KUPTR)(pFree) || !(pFree)->pNext); \
     100        KHLPHEAP_ASSERT_BLOCK(pHeap, &(pFree)->Core); \
     101        KHLPHEAP_ASSERT((KUPTR)(pFree)->pPrev < (KUPTR)(pFree)); \
     102        KHLPHEAP_ASSERT((KUPTR)(pFree)->pNext > (KUPTR)(pFree) || !(pFree)->pNext); \
    102103    } while (0)
    103104
    104105#else
    105 # define KLDRHEAP_ASSERT(expr)          do { } while (0)
    106 # define KLDRHEAP_ASSERT_BLOCK(pH, pB)  do { } while (0)
    107 # define KLDRHEAP_ASSERT_FREE(pH, pF)   do { } while (0)
     106# define KHLPHEAP_ASSERT(expr)          do { } while (0)
     107# define KHLPHEAP_ASSERT_BLOCK(pH, pB)  do { } while (0)
     108# define KHLPHEAP_ASSERT_FREE(pH, pF)   do { } while (0)
    108109#endif
    109110
     
    112113 * A free heap block.
    113114 */
    114 typedef struct KLDRHEAPFREE
     115typedef struct KHLPHEAPFREE
    115116{
    116117    /** The core bit which we have in common with used blocks. */
    117     KLDRHEAPBLOCK           Core;
     118    KHLPHEAPBLOCK           Core;
    118119    /** The next free block. */
    119     struct KLDRHEAPFREE    *pNext;
     120    struct KHLPHEAPFREE    *pNext;
    120121    /** The previous free block. */
    121     struct KLDRHEAPFREE    *pPrev;
    122 } KLDRHEAPFREE, *PKLDRHEAPFREE;
     122    struct KHLPHEAPFREE    *pPrev;
     123} KHLPHEAPFREE, *PKHLPHEAPFREE;
    123124
    124125
     
    126127 * A heap segment.
    127128 */
    128 typedef struct KLDRHEAPSEG
     129typedef struct KHLPHEAPSEG
    129130{
    130131    /** The base address of the segment. */
    131     void   *pvBase;
     132    void                   *pvBase;
    132133    /** The length of the segment (in bytes). */
    133     KSIZE   cb;
    134 } KLDRHEAPSEG, *PKLDRHEAPSEG;
     134    KSIZE                   cb;
     135} KHLPHEAPSEG, *PKHLPHEAPSEG;
    135136
    136137/**
    137138 * Bundle of heap segments.
    138139 */
    139 typedef struct KLDRHEAPSEGS
     140typedef struct KHLPHEAPSEGS
    140141{
    141142    /** Pointer to the next segment bundle. */
    142     struct KLDRHEAPSEGS    *pNext;
     143    struct KHLPHEAPSEGS    *pNext;
    143144    /** The number of segments used. */
    144145    KU32                    cSegs;
    145146    /** Array of chunks. */
    146     KLDRHEAPSEG             aSegs[64];
    147 } KLDRHEAPSEGS, *PKLDRHEAPSEGS;
     147    KHLPHEAPSEG             aSegs[64];
     148} KHLPHEAPSEGS, *PKHLPHEAPSEGS;
    148149
    149150
     
    151152 * Heap anchor block.
    152153 */
    153 typedef struct KLDRHEAPANCHOR
     154typedef struct KHLPHEAPANCHOR
    154155{
    155156    /** Head of the block list. */
    156     PKLDRHEAPBLOCK      pHead;
     157    PKHLPHEAPBLOCK          pHead;
    157158    /** Tail of the block list. */
    158     PKLDRHEAPBLOCK      pTail;
     159    PKHLPHEAPBLOCK          pTail;
    159160    /** Head of the free list. */
    160     PKLDRHEAPFREE       pFreeHead;
     161    PKHLPHEAPFREE           pFreeHead;
    161162    /** Head segment bundle.
    162163     * The order of this list is important, but a bit peculiar.
    163164     * Logically, SegsHead::pNext is the tail pointer. */
    164     KLDRHEAPSEGS        SegsHead;
    165 } KLDRHEAPANCHOR, *PKLDRHEAPANCHOR;
     165    KHLPHEAPSEGS            SegsHead;
     166} KHLPHEAPANCHOR, *PKHLPHEAPANCHOR;
    166167
    167168
     
    171172*******************************************************************************/
    172173/** The heap anchor block. */
    173 static KLDRHEAPANCHOR g_Heap;
     174static KHLPHEAPANCHOR      g_Heap;
    174175
    175176
     
    177178*   Internal Functions                                                         *
    178179*******************************************************************************/
    179 static int      kLdrHeapInit(PKLDRHEAPANCHOR pHeap);
    180 static void     kLdrHeapDelete(PKLDRHEAPANCHOR pHeap);
    181 static void *   kLdrHeapAlloc(PKLDRHEAPANCHOR pHeap, KSIZE cb);
    182 static void     kLdrHeapFree(PKLDRHEAPANCHOR pHeap, void *pv);
    183 static void     kLdrHeapDonate(PKLDRHEAPANCHOR pHeap, void *pv, KSIZE cb);
    184 static int      kLdrHeapSegAlloc(PKLDRHEAPSEG pSeg, KSIZE cb);
    185 static void     kLdrHeapSegFree(PKLDRHEAPSEG pSeg);
     180static int      khlpHeapInit(PKHLPHEAPANCHOR pHeap);
     181static void     khlpHeapDelete(PKHLPHEAPANCHOR pHeap);
     182static void *   khlpHeapAlloc(PKHLPHEAPANCHOR pHeap, KSIZE cb);
     183static void     khlpHeapFree(PKHLPHEAPANCHOR pHeap, void *pv);
     184static KSIZE    khlpHeapBlockSize(PKHLPHEAPANCHOR pHeap, void *pv);
     185static void     khlpHeapDonate(PKHLPHEAPANCHOR pHeap, void *pv, KSIZE cb);
     186static int      khlpHeapSegAlloc(PKHLPHEAPSEG pSeg, KSIZE cb);
     187static void     khlpHeapSegFree(PKHLPHEAPSEG pSeg);
    186188
    187189
     
    193195KHLP_DECL(int) kHlpHeapInit(void)
    194196{
    195     return kLdrHeapInit(&g_Heap);
     197    return khlpHeapInit(&g_Heap);
    196198}
    197199
     
    202204KHLP_DECL(void) kHlpHeapTerm(void)
    203205{
    204     kLdrHeapDelete(&g_Heap);
    205 }
    206 
    207 
    208 /**
    209  * Allocates memory from the kLdr heap.
    210  *
    211  * @returns Pointer to the allocated heap block. NULL if we can't satify the request.
    212  * @param   cb      The requested heap block size.
    213  */
     206    khlpHeapDelete(&g_Heap);
     207}
     208
     209
    214210KHLP_DECL(void *) kHlpAlloc(KSIZE cb)
    215211{
    216     return kLdrHeapAlloc(&g_Heap, cb);
    217 }
    218 
    219 
    220 /**
    221  * Allocates zero initialized memory from the kLdr heap.
    222  *
    223  * @returns Pointer to the allocated heap block. NULL if we can't satify the request.
    224  * @param   cb      The requested heap block size.
    225  */
     212    return khlpHeapAlloc(&g_Heap, cb);
     213}
     214
     215
    226216KHLP_DECL(void *) kHlpAllocZ(KSIZE cb)
    227217{
    228     void *pv = kLdrHeapAlloc(&g_Heap, cb);
     218    void *pv = khlpHeapAlloc(&g_Heap, cb);
    229219    if (pv)
    230220        kHlpMemSet(pv, 0, cb);
     
    233223
    234224
    235 /**
    236  * Frees memory allocated off the kLdr heap.
    237  *
    238  * @param   pv      Pointer to the heap block returned by kHlpAlloc().
    239  */
     225KHLP_DECL(void *) kHlpDup(const void *pv, KSIZE cb)
     226{
     227    void *pvNew = khlpHeapAlloc(&g_Heap, cb);
     228    if (pvNew)
     229        kHlpMemCopy(pvNew, pv, cb);
     230    return pvNew;
     231}
     232
     233
     234KHLP_DECL(char *) kHlpStrDup(const char *psz)
     235{
     236    return (char *)kHlpDup(psz, kHlpStrLen(psz) + 1);
     237}
     238
     239
     240KHLP_DECL(void *) kHlpRealloc(void *pv, KSIZE cb)
     241{
     242    void *pvNew;
     243    if (!cb)
     244    {
     245        kHlpFree(pv);
     246        pvNew = NULL;
     247    }
     248    else if (!pv)
     249        pvNew = khlpHeapAlloc(&g_Heap, cb);
     250    else
     251    {
     252        KSIZE cbToCopy = khlpHeapBlockSize(&g_Heap, pv);
     253        pvNew = khlpHeapAlloc(&g_Heap, cb);
     254        if (pvNew)
     255        {
     256            kHlpMemCopy(pvNew, pv, cb);
     257            kHlpFree(pv);
     258        }
     259    }
     260    return pvNew;
     261}
     262
     263
    240264KHLP_DECL(void) kHlpFree(void *pv)
    241265{
    242     kLdrHeapFree(&g_Heap, pv);
     266    khlpHeapFree(&g_Heap, pv);
    243267}
    244268
     
    252276KHLP_DECL(void) kHlpHeapDonate(void *pv, KSIZE cb)
    253277{
    254     kLdrHeapDonate(&g_Heap, pv, cb);
     278    khlpHeapDonate(&g_Heap, pv, cb);
    255279}
    256280
     
    263287 * @param   pHeap   The heap anchor to be initialized.
    264288 */
    265 static int kLdrHeapInit(PKLDRHEAPANCHOR pHeap)
     289static int khlpHeapInit(PKHLPHEAPANCHOR pHeap)
    266290{
    267291    pHeap->pHead = NULL;
     
    280304 * @param   pHeap   The heap to be deleted.
    281305 */
    282 static void kLdrHeapDelete(PKLDRHEAPANCHOR pHeap)
     306static void khlpHeapDelete(PKHLPHEAPANCHOR pHeap)
    283307{
    284308    /*
     
    292316        /* find the tail. */
    293317        KU32            iSeg;
    294         PKLDRHEAPSEGS   pSegs = pHeap->SegsHead.pNext;
     318        PKHLPHEAPSEGS   pSegs = pHeap->SegsHead.pNext;
    295319        if (!pSegs)
    296320            pSegs = &pHeap->SegsHead;
     
    304328        iSeg = pSegs->cSegs;
    305329        while (iSeg-- > 0)
    306             kLdrHeapSegFree(&pSegs->aSegs[iSeg]);
     330            khlpHeapSegFree(&pSegs->aSegs[iSeg]);
    307331        pSegs->cSegs = 0;
    308332    }
     
    320344 * Internal heap block allocator.
    321345 */
    322 static void *   kldrHeapAllocSub(PKLDRHEAPANCHOR pHeap, KSIZE cb)
     346static void *   kldrHeapAllocSub(PKHLPHEAPANCHOR pHeap, KSIZE cb)
    323347{
    324348    /*
    325349     * Find a fitting free block.
    326350     */
    327     const KSIZE     cbReq = K_ALIGN_Z(cb + sizeof(KLDRHEAPBLOCK), KLDRHEAPBLOCK_ALIGNMENT);
    328     PKLDRHEAPFREE   pCur = pHeap->pFreeHead;
     351    const KSIZE     cbReq = K_ALIGN_Z(cb + sizeof(KHLPHEAPBLOCK), KHLPHEAPBLOCK_ALIGNMENT);
     352    PKHLPHEAPFREE   pCur = pHeap->pFreeHead;
    329353    while (pCur)
    330354    {
     
    334358            {
    335359                /* check and see if there is a better match close by. */
    336                 PKLDRHEAPFREE pCur2 = pCur->pNext;
     360                PKHLPHEAPFREE pCur2 = pCur->pNext;
    337361                unsigned i = 16;
    338362                while (i-- > 0 && pCur2)
     
    350374
    351375                    /* next */
    352                     KLDRHEAP_ASSERT_FREE(pHeap, pCur2);
     376                    KHLPHEAP_ASSERT_FREE(pHeap, pCur2);
    353377                    pCur2 = pCur2->pNext;
    354378                }
     
    358382
    359383        /* next */
    360         KLDRHEAP_ASSERT_FREE(pHeap, pCur);
     384        KHLPHEAP_ASSERT_FREE(pHeap, pCur);
    361385        pCur = pCur->pNext;
    362386    }
    363387    if (!pCur)
    364388        return NULL;
    365     KLDRHEAP_ASSERT_FREE(pHeap, pCur);
     389    KHLPHEAP_ASSERT_FREE(pHeap, pCur);
    366390
    367391    /*
    368392     * Do we need to split out a block?
    369393     */
    370     if (pCur->Core.cb - cbReq >= KLDRHEAPBLOCK_ALIGNMENT * 2)
    371     {
    372         PKLDRHEAPBLOCK pNew;
     394    if (pCur->Core.cb - cbReq >= KHLPHEAPBLOCK_ALIGNMENT * 2)
     395    {
     396        PKHLPHEAPBLOCK pNew;
    373397
    374398        pCur->Core.cb -= cbReq;
    375399
    376         pNew = (PKLDRHEAPBLOCK)((KUPTR)pCur + pCur->Core.cb);
     400        pNew = (PKHLPHEAPBLOCK)((KUPTR)pCur + pCur->Core.cb);
    377401        pNew->fFlags = 0;
    378402        pNew->cb = cbReq;
     
    385409        pCur->Core.pNext = pNew;
    386410
    387         KLDRHEAP_ASSERT_FREE(pHeap, pCur);
    388         KLDRHEAP_ASSERT_BLOCK(pHeap, pNew);
     411        KHLPHEAP_ASSERT_FREE(pHeap, pCur);
     412        KHLPHEAP_ASSERT_BLOCK(pHeap, pNew);
    389413        return pNew + 1;
    390414    }
     
    399423    else
    400424        pHeap->pFreeHead = pCur->pNext;
    401     pCur->Core.fFlags &= ~KLDRHEAPBLOCK_FLAG_FREE;
    402 
    403     KLDRHEAP_ASSERT_BLOCK(pHeap, &pCur->Core);
     425    pCur->Core.fFlags &= ~KHLPHEAPBLOCK_FLAG_FREE;
     426
     427    KHLPHEAP_ASSERT_BLOCK(pHeap, &pCur->Core);
    404428    return &pCur->Core + 1;
    405429}
     
    413437 * @param   cb      The requested heap block size.
    414438 */
    415 static void *   kLdrHeapAlloc(PKLDRHEAPANCHOR pHeap, KSIZE cb)
     439static void *   khlpHeapAlloc(PKHLPHEAPANCHOR pHeap, KSIZE cb)
    416440{
    417441    void *pv;
    418442
    419443    /* adjust the requested block size. */
    420     cb = K_ALIGN_Z(cb, KLDRHEAPBLOCK_ALIGNMENT);
     444    cb = K_ALIGN_Z(cb, KHLPHEAPBLOCK_ALIGNMENT);
    421445    if (!cb)
    422         cb = KLDRHEAPBLOCK_ALIGNMENT;
     446        cb = KHLPHEAPBLOCK_ALIGNMENT;
    423447
    424448    /* try allocate the block. */
     
    429453         * Failed, add another segment and try again.
    430454         */
    431         KLDRHEAPSEG Seg;
    432         if (kLdrHeapSegAlloc(&Seg, cb + sizeof(KLDRHEAPSEGS) + sizeof(KLDRHEAPBLOCK) * 16))
     455        KHLPHEAPSEG Seg;
     456        if (khlpHeapSegAlloc(&Seg, cb + sizeof(KHLPHEAPSEGS) + sizeof(KHLPHEAPBLOCK) * 16))
    433457            return NULL;
    434458
    435459        /* donate before insterting the segment, this makes sure we got heap to expand the segment list. */
    436         kLdrHeapDonate(pHeap, Seg.pvBase, Seg.cb);
     460        khlpHeapDonate(pHeap, Seg.pvBase, Seg.cb);
    437461
    438462        /* insert the segment. */
     
    444468        else
    445469        {
    446             PKLDRHEAPSEGS pSegs = (PKLDRHEAPSEGS)kldrHeapAllocSub(pHeap, sizeof(*pSegs));
    447             KLDRHEAP_ASSERT(pSegs);
     470            PKHLPHEAPSEGS pSegs = (PKHLPHEAPSEGS)kldrHeapAllocSub(pHeap, sizeof(*pSegs));
     471            KHLPHEAP_ASSERT(pSegs);
    448472            pSegs->pNext = pHeap->SegsHead.pNext;
    449473            pHeap->SegsHead.pNext = pSegs;
     
    454478        /* retry (should succeed) */
    455479        pv = kldrHeapAllocSub(pHeap, cb);
    456         KLDRHEAP_ASSERT(pv);
     480        KHLPHEAP_ASSERT(pv);
    457481    }
    458482
     
    465489 *
    466490 * @param   pHeap   The heap.
    467  * @param   pv      The pointer returned by kLdrHeapAlloc().
    468  */
    469 static void     kLdrHeapFree(PKLDRHEAPANCHOR pHeap, void *pv)
    470 {
    471     PKLDRHEAPFREE pFree, pLeft, pRight;
     491 * @param   pv      The pointer returned by khlpHeapAlloc().
     492 */
     493static void     khlpHeapFree(PKHLPHEAPANCHOR pHeap, void *pv)
     494{
     495    PKHLPHEAPFREE pFree, pLeft, pRight;
    472496
    473497    /* ignore NULL pointers. */
     
    475499        return;
    476500
    477     pFree = (PKLDRHEAPFREE)((PKLDRHEAPBLOCK)pv - 1);
    478     KLDRHEAP_ASSERT_BLOCK(pHeap, &pFree->Core);
    479     KLDRHEAP_ASSERT(KLDRHEAPBLOCK_IS_ALLOCATED(&pFree->Core));
     501    pFree = (PKHLPHEAPFREE)((PKHLPHEAPBLOCK)pv - 1);
     502    KHLPHEAP_ASSERT_BLOCK(pHeap, &pFree->Core);
     503    KHLPHEAP_ASSERT(KHLPHEAPBLOCK_IS_ALLOCATED(&pFree->Core));
    480504
    481505    /*
    482506     * Merge or link with left node?
    483507     */
    484     pLeft = (PKLDRHEAPFREE)pFree->Core.pPrev;
     508    pLeft = (PKHLPHEAPFREE)pFree->Core.pPrev;
    485509    if (    pLeft
    486         &&  KLDRHEAPBLOCK_IS_FREE(&pLeft->Core)
    487         &&  KLDRHEAPBLOCK_IS_ADJACENT(&pLeft->Core, &pFree->Core)
     510        &&  KHLPHEAPBLOCK_IS_FREE(&pLeft->Core)
     511        &&  KHLPHEAPBLOCK_IS_ADJACENT(&pLeft->Core, &pFree->Core)
    488512       )
    489513    {
     
    502526    {
    503527        /* link left */
    504         while (pLeft && !KLDRHEAPBLOCK_IS_FREE(&pLeft->Core))
    505             pLeft = (PKLDRHEAPFREE)pLeft->Core.pPrev;
     528        while (pLeft && !KHLPHEAPBLOCK_IS_FREE(&pLeft->Core))
     529            pLeft = (PKHLPHEAPFREE)pLeft->Core.pPrev;
    506530        if (pLeft)
    507531        {
     
    520544            pHeap->pFreeHead = pFree;
    521545        }
    522         pFree->Core.fFlags |= KLDRHEAPBLOCK_FLAG_FREE;
    523     }
    524     KLDRHEAP_ASSERT_FREE(pHeap, pFree);
     546        pFree->Core.fFlags |= KHLPHEAPBLOCK_FLAG_FREE;
     547    }
     548    KHLPHEAP_ASSERT_FREE(pHeap, pFree);
    525549
    526550    /*
    527551     * Merge right?
    528552     */
    529     pRight = (PKLDRHEAPFREE)pFree->Core.pNext;
     553    pRight = (PKHLPHEAPFREE)pFree->Core.pNext;
    530554    if (    pRight
    531         &&  KLDRHEAPBLOCK_IS_FREE(&pRight->Core)
    532         &&  KLDRHEAPBLOCK_IS_ADJACENT(&pFree->Core, pRight)
     555        &&  KHLPHEAPBLOCK_IS_FREE(&pRight->Core)
     556        &&  KHLPHEAPBLOCK_IS_ADJACENT(&pFree->Core, pRight)
    533557       )
    534558    {
     
    553577
    554578/**
     579 * Calcs the size of a heap block.
     580 *
     581 * @returns The block size (in bytes).
     582 * @param   pHeap       The heap.
     583 * @param   pv          Pointer to an in-use heap block.
     584 */
     585static KSIZE khlpHeapBlockSize(PKHLPHEAPANCHOR pHeap, void *pv)
     586{
     587    PKHLPHEAPBLOCK pBlock  = (PKHLPHEAPBLOCK)pv - 1;
     588    KHLPHEAP_ASSERT_BLOCK(pHeap, pBlock);
     589    KHLPHEAP_ASSERT(KHLPHEAPBLOCK_IS_ALLOCATED(pBlock));
     590    return (KU8 *)pBlock->pNext - (KU8 *)pv;
     591}
     592
     593
     594/**
    555595 * Donates memory to the heap.
    556596 *
     
    561601 * @param cb    Size of the donated memory.
    562602 */
    563 static void     kLdrHeapDonate(PKLDRHEAPANCHOR pHeap, void *pv, KSIZE cb)
    564 {
    565     PKLDRHEAPBLOCK pBlock;
     603static void     khlpHeapDonate(PKHLPHEAPANCHOR pHeap, void *pv, KSIZE cb)
     604{
     605    PKHLPHEAPBLOCK pBlock;
    566606
    567607    /*
    568608     * Don't bother with small donations.
    569609     */
    570     if (cb < KLDRHEAPBLOCK_ALIGNMENT * 4)
     610    if (cb < KHLPHEAPBLOCK_ALIGNMENT * 4)
    571611        return;
    572612
     
    574614     * Align the donation on a heap block boundrary.
    575615     */
    576     if ((KUPTR)pv & (KLDRHEAPBLOCK_ALIGNMENT - 1))
     616    if ((KUPTR)pv & (KHLPHEAPBLOCK_ALIGNMENT - 1))
    577617    {
    578618        cb -= (KUPTR)pv & 31;
    579         pv = K_ALIGN_P(pv, KLDRHEAPBLOCK_ALIGNMENT);
    580     }
    581     cb &= ~(KSIZE)(KLDRHEAPBLOCK_ALIGNMENT - 1);
     619        pv = K_ALIGN_P(pv, KHLPHEAPBLOCK_ALIGNMENT);
     620    }
     621    cb &= ~(KSIZE)(KHLPHEAPBLOCK_ALIGNMENT - 1);
    582622
    583623    /*
    584624     * Create an allocated block, link it and free it.
    585625     */
    586     pBlock = (PKLDRHEAPBLOCK)pv;
     626    pBlock = (PKHLPHEAPBLOCK)pv;
    587627    pBlock->pNext = NULL;
    588628    pBlock->pPrev = NULL;
     
    617657    {
    618658        /* in list (unlikely) */
    619         PKLDRHEAPBLOCK pPrev = pHeap->pHead;
    620         PKLDRHEAPBLOCK pCur = pPrev->pNext;
     659        PKHLPHEAPBLOCK pPrev = pHeap->pHead;
     660        PKHLPHEAPBLOCK pCur = pPrev->pNext;
    621661        for (;;)
    622662        {
    623             KLDRHEAP_ASSERT_BLOCK(pHeap, pCur);
     663            KHLPHEAP_ASSERT_BLOCK(pHeap, pCur);
    624664            if ((KUPTR)pCur > (KUPTR)pBlock)
    625665                break;
     
    633673        pCur->pPrev = pBlock;
    634674    }
    635     KLDRHEAP_ASSERT_BLOCK(pHeap, pBlock);
     675    KHLPHEAP_ASSERT_BLOCK(pHeap, pBlock);
    636676
    637677    /* free it */
    638     kLdrHeapFree(pHeap, pBlock + 1);
     678    khlpHeapFree(pHeap, pBlock + 1);
    639679}
    640680
     
    648688 * @param   cbMin   The minimum segment size.
    649689 */
    650 static int kLdrHeapSegAlloc(PKLDRHEAPSEG pSeg, KSIZE cbMin)
     690static int khlpHeapSegAlloc(PKHLPHEAPSEG pSeg, KSIZE cbMin)
    651691{
    652692#if K_OS == K_OS_OS2
     
    687727 * @param   pSeg    The segment to be freed.
    688728 */
    689 static void kLdrHeapSegFree(PKLDRHEAPSEG pSeg)
     729static void khlpHeapSegFree(PKHLPHEAPSEG pSeg)
    690730{
    691731#if K_OS == K_OS_OS2
    692732    APIRET rc = DosFreeMem(pSeg->pvBase);
    693     KLDRHEAP_ASSERT(!rc); (void)rc;
     733    KHLPHEAP_ASSERT(!rc); (void)rc;
    694734
    695735#elif  K_OS == K_OS_WINDOWS
    696736    BOOL fRc = VirtualFree(pSeg->pvBase, 0 /*pSeg->cb*/, MEM_RELEASE);
    697     KLDRHEAP_ASSERT(fRc); (void)fRc;
     737    KHLPHEAP_ASSERT(fRc); (void)fRc;
    698738
    699739#else
Note: See TracChangeset for help on using the changeset viewer.