static sg_error sg_vector_clone_into_int(sg_vector **dest, const sg_vector *src){ /* we can assume that everything is correct from caller perspective */ size_t i; sg_vector *tmp = ((*dest)->used_count == src->used_count) ? *dest : sg_vector_resize(*dest, src->used_count); char const *src_data = VECTOR_DATA_CONST(src); char *dest_data = VECTOR_DATA(tmp); size_t item_size = src->info.item_size; assert(src->info.copy_fn); if( !tmp ) { RETURN_FROM_PREVIOUS_ERROR( "vector", sg_get_error() ); } for( i = 0; i < src->used_count; ++i ) { sg_error rc = src->info.copy_fn( src_data + i * item_size, dest_data + i * item_size ); if( SG_ERROR_NONE != rc ) { sg_vector_free( tmp ); *dest = NULL; return rc; } } *dest = tmp; return SG_ERROR_NONE; }
void *sg_vector_resize(void *vector, vector_header *h, int count) { int new_count, i; /* Destroy any now-unused items. * * Note that there's an assumption here that making the vector smaller * will never fail; if it did, then we would have destroyed items here * but not actually got rid of the vector pointing to them before the * error return.) */ if (count < h->used_count && h->destroy_fn != NULL) { for (i = count; i < h->used_count; i++) { //h->destroy_fn((void *) (vector + i * h->item_size)); h->destroy_fn((void *) ((char *) vector + i * h->item_size)); } } /* Round up the desired size to the next multiple of the block size. */ new_count = ((count - 1 + h->block_size) / h->block_size) * h->block_size; /* Resize the vector if necessary. */ if (new_count != h->alloc_count) { char *new_vector; new_vector = sg_realloc(vector, new_count * h->item_size); if (new_vector == NULL && new_count != 0) { /* Out of memory -- free the contents of the vector. */ sg_vector_resize(vector, h, 0); h->error = -1; return vector; } vector = new_vector; h->alloc_count = new_count; } /* Initialise any new items. */ if (count > h->used_count && h->init_fn != NULL) { for (i = h->used_count; i < count; i++) { //h->init_fn((void *) (vector + i * h->item_size)); h->init_fn((void *) ((char *) vector + i * h->item_size)); } } h->used_count = count; h->error = 0; return vector; }