Example #1
0
void AJ_WSL_ModuleInit(void)
{
    //prepare the WSL heap
    size_t heapSz;

    heapSz = AJ_PoolRequired(wsl_heapConfig, ArraySize(wsl_heapConfig));
    if (heapSz > sizeof(wsl_heap)) {
        AJ_ErrPrintf(("Heap space is too small %d required %d\n", sizeof(wsl_heap), heapSz));
        return;
    }
    AJ_InfoPrintf(("Allocated heap %d bytes\n", (int)heapSz));
    AJ_PoolInit(wsl_heap, heapSz, wsl_heapConfig, ArraySize(wsl_heapConfig));
    AJ_PoolDump();

    AJ_WSL_SOCKNUM i;
    for (i = 0; i < AJ_WSL_SOCKET_MAX; i++) {
        memset(&AJ_WSL_SOCKET_CONTEXT[i], 0, sizeof(AJ_WSL_SOCKET_CONTEXT[0]));
        AJ_WSL_SOCKET_CONTEXT[i].targetHandle = AJ_WSL_SOCKET_HANDLE_INVALID;
        AJ_WSL_SOCKET_CONTEXT[i].stashedRxList = AJ_BufListCreate();
        AJ_WSL_SOCKET_CONTEXT[i].workRxQueue = AJ_QueueCreate("RxQueue");
        AJ_WSL_SOCKET_CONTEXT[i].workTxQueue = AJ_QueueCreate("TxQueue");
    }

    AJ_WSL_WMI_ModuleInit();

}
void* AJ_PoolRealloc(void* mem, size_t newSz)
{
    Pool* p = heapPools;
    uint8_t i;

    if (mem) {
        assert((ptrdiff_t)mem >= (ptrdiff_t)heapStart);
        /*
         * Locate the pool from which the released memory was allocated
         */
        for (i = 0; i < numPools; ++i, ++p) {
            if ((ptrdiff_t)mem < (ptrdiff_t)p->endOfPool) {
                size_t oldSz = heapConfig[i].size;
                /*
                 * Don't need to do anything if the same block would be reused
                 */
                if ((newSz <= oldSz) && ((i == 0) || (newSz > heapConfig[i - 1].size))) {
                    AJ_InfoPrintf(("AJ_Realloc pool[%d] %d bytes in place\n", (int)oldSz, (int)newSz));
                } else {
                    MemBlock* block = (MemBlock*)mem;
                    AJ_InfoPrintf(("AJ_Realloc pool[%d] by AJ_Alloc(%d)\n", (int)oldSz, (int)newSz));
                    mem = AJ_PoolAlloc(newSz);
                    if (mem) {
                        memcpy(mem, (void*)block, min(oldSz, newSz));
                        /*
                         * Put old block on the free list
                         */
                        block->next = p->freeList;
                        p->freeList = block;
#ifndef NDEBUG
                        --p->use;
#endif
                    }
                }
                return mem;
            }
        }
    } else {
        return AJ_PoolAlloc(newSz);
    }
    AJ_ErrPrintf(("AJ_Realloc of %d bytes failed\n", (int)newSz));
    AJ_PoolDump();
    return NULL;
}
void* AJ_PoolAlloc(size_t sz)
{
    Pool* p = heapPools;
    uint8_t i;

    if (!p) {
        AJ_ErrPrintf(("Heap not initialized\n"));
        return NULL;
    }
    /*
     * Find pool that can satisfy the allocation
     */
    for (i = 0; i < numPools; ++i, ++p) {
        if (sz <= heapConfig[i].size) {
            MemBlock* block = (MemBlock*)p->freeList;
            if (!block) {
                /*
                 * Are we allowed to borrowing from next pool?
                 */
                if (heapConfig[i].borrow) {
                    continue;
                }
                break;
            }
            AJ_InfoPrintf(("AJ_PoolAlloc pool[%d] allocated %d\n", heapConfig[i].size, (int)sz));
            p->freeList = block->next;
#ifndef NDEBUG
            ++p->use;
            p->hwm = max(p->use, p->hwm);
            p->max = max(p->max, sz);
#endif
            return (void*)block;
        }
    }
    AJ_ErrPrintf(("AJ_PoolAlloc of %d bytes failed\n", (int)sz));
    AJ_PoolDump();
    return NULL;
}