Ejemplo n.º 1
0
Darray Darray_create(NOARGS)
    {
    Darray_rep *temp = dcreate();
    
    temp->length = 0;
    temp->storage = (VOIDP *)Memory_allocate(sizeof(VOIDP));
    temp->storage_length = 1;
    temp->storage_offset = 0;
    return draise(temp);
    }
Ejemplo n.º 2
0
void *Memory_reallocate(void *memory, u32 size)
{
    void *new = Memory_allocate(size);
    Memory_Header *header = Memory_Header_fromBlock(memory);
    Memory_Header_assertMagic(header);
    String_copy(new, memory, (size < header->size) ? size : header->size);
    Memory_free(memory);

    return new;
}
Ejemplo n.º 3
0
/* Grow in the specified direction by the specified amount (number of 
 * new array slots).  It amount is zero, grow by some default amount.
 */
static NORET grow(Darray_rep *array_rep, enum grow_direction direction, unsigned amount)
    {
    int grow_amount;
    int new_length;
    VOIDP *temp_new_space;
    VOIDP *p, *q;
    
    assert(direction == GROW_HIGH || direction == GROW_LOW);
    
    if (amount == 0) 
        grow_amount = (MAX_GROW_STEP < array_rep->storage_length) ? MAX_GROW_STEP : array_rep->storage_length;
    else
        grow_amount = amount;
    
    new_length = array_rep->storage_length + grow_amount;
    
    /* sort of like realloc, but not really */
    if (direction == GROW_HIGH) 
        {
        /* Maybe this should not use realloc, since there may be unused 
        slots at the low end of the darray (before storage_offset), and it
        is a waste to copy them */
        array_rep->storage = (VOIDP *)Memory_reallocate(array_rep->storage,
					       sizeof(VOIDP) * new_length);
        array_rep->storage_length = new_length;
        }
    else 
        {			/* direction == GROW_LOW */
        temp_new_space = (VOIDP *)Memory_allocate(sizeof(VOIDP) * new_length);
        for ( p=temp_new_space+grow_amount+array_rep->storage_offset, 
              q=array_rep->storage+array_rep->storage_offset
            ; q < array_rep->storage+array_rep->storage_offset+array_rep->length
            ; ++p, ++q
            ) 
            {
            *p = *q;
            }
        Memory_free(array_rep->storage);
        array_rep->storage = temp_new_space;
        array_rep->storage_length = new_length;
        array_rep->storage_offset += grow_amount;
        }
    }