Beispiel #1
0
/*
 *   Ensure that we have space for a given number of entries 
 */
void CVmMetaTable::ensure_space(size_t entries, size_t increment)
{
    /* if we don't have enough space, allocate more */
    if (entries >= alloc_)
    {
        size_t new_size;
        
        /* increase the allocation size by the given increment */
        alloc_ += increment;

        /* if it's still too small, bump it up to the required size */
        if (alloc_ < entries)
            alloc_ = entries;

        /* compute the new size */
        new_size = alloc_ * sizeof(table_[0]);
        
        /* 
         *   if we have a table already, reallocate it at the larger size;
         *   otherwise, allocate a new table 
         */
        if (table_ != 0)
            table_ = (vm_meta_entry_t *)t3realloc(table_, new_size);
        else
            table_ = (vm_meta_entry_t *)t3malloc(new_size);
    }
}
/*
 *   allocate a new page 
 */
void CTcDataStream::alloc_page()
{
    /* 
     *   if we're coming back to a page that was previously allocated, we
     *   need merely re-establish the existing page 
     */
    if (page_cur_ + 1 < page_cnt_)
    {
        /* move to the next page */
        ++page_cur_;

        /* start writing at the start of the page */
        wp_ = pages_[page_cur_];
        rem_ = TCCS_PAGE_SIZE;

        /* we're done */
        return;
    }

    /* 
     *   if we don't have room for a new page in the page array, expand
     *   the page array 
     */
    if (page_cnt_ >= page_slots_)
    {
        /* increase the page slot count */
        page_slots_ += 100;

        /* allocate or reallocate the page array */
        if (pages_ == 0)
            pages_ = (char **)t3malloc(page_slots_ * sizeof(pages_[0]));
        else
            pages_ = (char **)t3realloc(pages_,
                                        page_slots_ * sizeof(pages_[0]));

        /* if that failed, throw an error */
        if (pages_ == 0)
            err_throw(TCERR_CODEGEN_NO_MEM);
    }

    /* allocate the new page */
    pages_[page_cnt_] = (char *)t3malloc(TCCS_PAGE_SIZE);

    /* throw an error if we couldn't allocate the page */
    if (pages_[page_cnt_] == 0)
        err_throw(TCERR_CODEGEN_NO_MEM);

    /* start writing at the start of the new page */
    wp_ = pages_[page_cnt_];

    /* the entire page is free */
    rem_ = TCCS_PAGE_SIZE;

    /* make the new page the current page */
    page_cur_ = page_cnt_;

    /* count the new page */
    ++page_cnt_;
}
Beispiel #3
0
    /* reserve space for an addition 'b' bytes */
    void reserve(size_t b)
    {
        if (wrtidx + b + 1 > len)
        {
            /* increase by the increment until big enough */
            while (wrtidx + b + 1 > len)
                len += inc;

            /* reallocate at the increased size */
            buf = (char *)t3realloc(buf, len);
        }
    }
/*
 *   Allocate line record pages 
 */
void CTcCodeStream::alloc_line_pages(size_t number_to_add)
{
    size_t siz;
    size_t i;

    /* create or expand the master page array */
    siz = (line_pages_alloc_ + number_to_add) * sizeof(tcgen_line_page_t *);
    if (line_pages_ == 0)
        line_pages_ = (tcgen_line_page_t **)t3malloc(siz);
    else
        line_pages_ = (tcgen_line_page_t **)t3realloc(line_pages_, siz);

    /* allocate the new pages */
    for (i = line_pages_alloc_ ; i < line_pages_alloc_ + number_to_add ; ++i)
    {
        /* allocate this page */
        line_pages_[i] = (tcgen_line_page_t *)
                         t3malloc(sizeof(tcgen_line_page_t));
    }

    /* remember the new allocation */
    line_pages_alloc_ += number_to_add;
}