Ejemplo n.º 1
0
static MemBlock *GetBlock(size_t len)
{
    ScopedCritSec cs(&gMemMutex);

    // first try current block
    if (gCurrBlock && (gCurrBlock->Left() >= len))
        return gCurrBlock;

    MemBlock *newBlock;
    size_t dataSize;
    if (gBlocksFreeList && gBlocksFreeList->size >= len) {
        newBlock = gBlocksFreeList;
        dataSize = newBlock->size;
        gBlocksFreeList = gBlocksFreeList->next;
    } else {
        // allocate new block
        dataSize = len;
        if (dataSize < MIN_BLOCK_SIZE)
            dataSize = MIN_BLOCK_SIZE;
        // TODO: have at least 2 MemBlocks allocated as a static memory?
        newBlock = (MemBlock*)HeapAlloc(gHeap, 0, sizeof(MemBlock) + dataSize);
        if (!newBlock) {
            lf("memtrace.dll: failed to allocate a block");
            return NULL;
        }
    }

    //lf("memtrace.dll: allocated a new block");
    ++gBlocksAllocated;
    newBlock->next = NULL;
    newBlock->size = dataSize;
    newBlock->used = 0;
    newBlock->sent = 0;

    if (gCurrBlock)
        InsertAtEnd(&gBlocksToSend, gCurrBlock);

    gCurrBlock= newBlock;
    return newBlock;
}
Ejemplo n.º 2
0
int main()
{
    struct node *head = (struct node*)malloc(sizeof(struct node));
    struct node *second = (struct node*)malloc(sizeof(struct node));
    head->data =1;
    head->next = second;

    second->data = 2;
    second->next = NULL;
    //Insert a node at first:
    InsertAtFirst(&head);
    //Insert a node at the end:
    InsertAtEnd(&head);
    //Insert after a given node:
    InsertAfterNode(&head, 11);

    //Delete a given node:
    DeleteNode(&head, 1);

    //PrintLinkNode:
    printList(head);

    return 0;
}