alloc_handle_t * AllocInit()
{
    alloc_handle_t *newpool;

    root = (alloc_root_t *) malloc(sizeof(alloc_root_t));
    if (root == NULL) return(NULL);
    if ( (root->first = AllocHdr()) == NULL) return(NULL);
    root->current = root->first;
    newpool = (alloc_handle_t *) root;
    return(newpool);
}
Esempio n. 2
0
alloc_handle_t * AllocInit(Project* project)
{
    alloc_handle_t *newpool;

    project->root = (alloc_root_t *) malloc(sizeof(alloc_root_t));
    if (project->root == NULL) return(NULL);
    if ( (project->root->first = AllocHdr()) == NULL) return(NULL);
    project->root->current = project->root->first;
    newpool = (alloc_handle_t *) project->root;
    return(newpool);
}
char * Alloc(long size)
{
    alloc_hdr_t  *hdr = root->current;
    char         *ptr;

    /*
    **  Align to 4 byte boundary - should be ok for most machines.
    **  Change this if your machine has weird alignment requirements.
    */
    size = (size + 3) & 0xfffffffc;

    ptr = hdr->free;
    hdr->free += size;

    /* Check if the current block is exhausted. */

    if (hdr->free >= hdr->end)
    {
        /* Is the next block already allocated? */

        if (hdr->next != NULL)
        {
            /* re-use block */
            hdr->next->free = hdr->next->block;
            root->current = hdr->next;
        }
        else
        {
            /* extend the pool with a new block */
            if ( (hdr->next = AllocHdr()) == NULL) return(NULL);
            root->current = hdr->next;
        }

        /* set ptr to the first location in the next block */
        ptr = root->current->free;
        root->current->free += size;
    }

    /* Return pointer to allocated memory. */

    return(ptr);
}