Example #1
0
END_TEST

START_TEST(test_array_resize)
{
    array_t*       array;
    const size_t   capacity1    = 1;
    const size_t   capacity2    = 2;
    uint32_t       value1       = 0xf00fba11;
    uint32_t       value2       = 0xdeadbeef;
    const size_t   element_size = sizeof(typeof(value1));

    array = array_create(element_size);
    array_reserve(array, capacity1);
    ck_assert_int_eq(array_get_capacity(array), capacity1);
    ck_assert(array_get(array, typeof(value1)) != NULL);
    array_at(array, 0, typeof(value1)) = value1;

    array_reserve(array, capacity2);
    ck_assert_int_eq(array_get_capacity(array), capacity2);
    ck_assert_int_eq(array_at(array, 0, typeof(value1)), value1);

    array_at(array, 1, typeof(value1)) = value2;
    ck_assert_int_eq(array_at(array, 1, typeof(value1)), value2);

    array_reserve(array, capacity1);
    ck_assert_int_eq(array_get_capacity(array), capacity2);
    ck_assert_int_eq(array_at(array, 0, typeof(value1)), value1);
    ck_assert_int_eq(array_at(array, 1, typeof(value1)), value2);

    array_destroy(array);
}
Example #2
0
File: utils.c Project: wopl/fhem
// sets errno on error
void *array_append(Array *array) {
	void *item;

	if (array_reserve(array, array->count + 1) < 0) {
		return NULL;
	}

	++array->count;

	if (array->relocatable) {
		item = array_get(array, array->count - 1);

		memset(item, 0, array->size);
	} else {
		item = calloc(1, array->size);

		if (item == NULL) {
			--array->count;

			errno = ENOMEM;

			return NULL;
		}

		*(void **)(array->bytes + sizeof(void *) * (array->count - 1)) = item;
	}

	return item;
}
Example #3
0
File: utils.c Project: wopl/fhem
int array_resize(Array *array, int count, FreeFunction function) {
	int rc;
	int i;
	void *item;

	if (array->count < count) {
		rc = array_reserve(array, count);

		if (rc < 0) {
			return rc;
		}
	} else if (array->count > count) {
		if (function != NULL) {
			for (i = count; i < array->count; ++i) {
				item = array_get(array, i);

				function(item);

				if (!array->relocatable) {
					free(item);
				}
			}
		} else if (!array->relocatable) {
			for (i = count; i < array->count; ++i) {
				free(array_get(array, i));
			}
		}
	}

	array->count = count;

	return 0;
}
Example #4
0
void font_init()
{
	array_reserve(s_fontCtx.fonts, 16);
	s_fontCtx.fontShader = shader_create_fromfiles("font.vert", "font.frag");
	s_fontCtx.fontSettingsParam = shader_get_param(
		s_fontCtx.fontShader, 
		"FontSettings", 
		ShaderParamType_UniformBlock
	);

	vector_t translation = vector_zero();
	vector_t scale = vector(1/640.0f, 1/480.0f, 1.0f, 0.f);
	vector_t rotation = vector_zero();

	font_shader_settings_t settings;
	settings.color = vector(1.0f,1.0f,1.0f,1.0f);
	settings.transform = create_transform_matrix(translation, scale, rotation);

	s_fontCtx.fontSettingsBuffer = shader_uniformbuffer_create(
		s_fontCtx.fontSettingsParam,
		(void*) &settings
	);

	s_fontCtx.fontState.useBlend = true;
	s_fontCtx.fontState.srcBlendState = BlendState_SrcAlpha;
	s_fontCtx.fontState.dstBlendState = BlendState_One;
	s_fontCtx.fontState.useStencil = false;
}
Example #5
0
array_t *array_init(array_t *self, size_t object_size, size_t capacity, allocator_t *alloc)
{
  if (alloc == NULL)
    alloc = g_default_allocator;

  if (object_size == 0) {
    s_fatal_error(1, "Invalid object size for array: 0.");
    return NULL;
  } else if (!(alloc->malloc && alloc->free)) {
    s_fatal_error(1, "malloc & free pointers are NULL.");
    return NULL;
  } else if (NULL == self) {
    s_fatal_error(1, "Failed to allocate array.");
    return NULL;
  }

  self->allocator = alloc;
  self->obj_size = object_size;
  self->size = 0;
  self->capacity = 0;
  self->buf = NULL;

  if (!array_reserve(self, capacity)) {
    array_destroy(self);
    s_fatal_error(1, "Failed to create array with capacity %zu", capacity);
    return NULL;
  }

  return self;
}
Example #6
0
bool array_resize(array_t *self, size_t size)
{
  if (self == NULL) {
    s_fatal_error(1, "Cannot resize NULL array.");
    return false;
  }

  if (array_reserve(self, size)) {
    if (self->buf) {
      if (size == 0 && self->size != 0) {
        // simply zero the used portion of the array
        memset(self->buf, 0, self->size * self->obj_size);
      } else if (size < self->size) {
        // zero the portion of the array cut off
        memset(self->buf + (size * self->obj_size), 0, (self->size - size) * self->obj_size);
      }
    }
    self->size = size;

    return true;
  }

  s_fatal_error(1, "Failed to resize array.");

  return false;
}
Example #7
0
int _random_initialize( void )
{
	if( !_random_mutex )
	{
		int i;
		_random_mutex = mutex_allocate( "random" );

		//Allocate and seed a number of state buffers
		array_reserve( _random_state, 64 );
		array_reserve( _random_available_state, 64 );
		for( i = 0; i < 16; ++i )
		{
			unsigned int* buffer = _random_allocate_buffer();
			array_push( _random_available_state, buffer );
		}
	}
	return 0;
}
Example #8
0
void init_quarks(void)
{
  size_t index;
  
  if (quarks)
    return;

  quarks = array_new((free_function_t)array_free);
  array_reserve(quarks, HASH_SIZE);
  for (index = 0; index < HASH_SIZE; ++index)
    array_append(quarks, array_new((free_function_t)free));

  quarks_index = array_new(NULL);
}
Example #9
0
static void test_array(void **state)
{
	int ret = 0;
	array_t(int) arr;
	array_init(arr);

	/* Basic access */
	assert_int_equal(arr.len, 0);
	assert_int_equal(array_push(arr, 5), 0);
	assert_int_equal(arr.at[0], 5);
	assert_int_equal(array_tail(arr), 5);
	array_clear(arr);

	/* Reserve capacity and fill. */
	assert_true(array_reserve(arr, 5) >= 0);
	for (unsigned i = 0; i < 100; ++i) {
		ret = array_push(arr, i);
		assert_true(ret >= 0);
	}

	/* Make sure reservation holds. */
	assert_true(array_reserve(arr, 5) >= 0);

	/* Delete elements. */
	array_del(arr, 0);
	while (arr.len > 0) {
		array_pop(arr);
	}

	/* Overfill. */
	for (unsigned i = 0; i < 4096; ++i) {
		ret = array_push(arr, i);
		assert_true(ret >= 0);
	}

	array_clear(arr);
}
Example #10
0
void
hashmap_initialize(hashmap_t* map, size_t buckets, size_t bucketsize) {
	size_t ibucket;

	if (bucketsize < HASHMAP_MINBUCKETSIZE)
		bucketsize = HASHMAP_MINBUCKETSIZE;

	map->num_buckets = buckets;
	map->num_nodes   = 0;

	for (ibucket = 0; ibucket < buckets; ++ibucket) {
		map->bucket[ibucket] = 0;
		array_reserve(map->bucket[ibucket], bucketsize);
	}
}
Example #11
0
static int
blast_client_initialize(blast_client_t* client, network_address_t** address) {
	int iaddr, addrsize = 0;

	memset(client, 0, sizeof(blast_client_t));
	client->address = address;
	client->state = BLAST_STATE_HANDSHAKE;
	client->begin_send = time_current();
	array_reserve(client->pending, 1024);

	for (iaddr = 0, addrsize = array_size(client->address); iaddr < addrsize; ++iaddr) {
		socket_t* sock = udp_socket_allocate();
		array_push(client->socks, sock);
		socket_set_blocking(sock, false);
	}

	return BLAST_RESULT_OK;
}
Example #12
0
int array_resize(struct array* a, unsigned int num) {
	int result;

	if (a == NULL) {
		return EINVAL;
	}

	if (num < a->num) {
		a->num = num;
		return 0;
	}

	if ((result = array_reserve(a, num))) {
		return result;
	}

	memset(&a->a[a->num], (num - a->num) * sizeof(void*), 0);

	a->num = num;
	return 0;
}
Example #13
0
END_TEST

START_TEST(test_array_assign)
{
    array_t*       array;
    uint32_t       value        = 0xdeadbeef;
    const size_t   element_size = sizeof(typeof(value));
    const size_t   capacity     = 1;

    array = array_create(element_size);
    ck_assert(array != NULL);
    ck_assert(array_get(array, typeof(value)) == NULL);

    array_reserve(array, capacity);
    ck_assert(array_get(array, typeof(value)) != NULL);
    ck_assert_int_eq(array_get_capacity(array), capacity);

    array_at(array, 0, typeof(value)) = value;
    ck_assert_int_eq((array_get(array, typeof(value)))[0], value);

    array_destroy(array);
}
Example #14
0
hashmap_t* hashmap_allocate( unsigned int buckets, unsigned int bucketsize )
{
	hashmap_t* map;
	unsigned int ibucket;

	if( buckets < HASHMAP_MINBUCKETS )
		buckets = HASHMAP_MINBUCKETS;
	if( bucketsize < HASHMAP_MINBUCKETSIZE )
		bucketsize = HASHMAP_MINBUCKETSIZE;
	
	map = memory_allocate( sizeof( hashmap_t ) + sizeof( hashmap_bucket_t ) * buckets, 0, MEMORY_PERSISTENT );

	map->num_buckets = buckets;
	map->num_nodes   = 0;

	for( ibucket = 0; ibucket < buckets; ++ibucket )
	{
		map->bucket[ibucket] = 0;
		array_reserve( map->bucket[ibucket], (int)bucketsize );
	}
	
	return map;
}
Example #15
0
void array_resize(void *array_, allocator_t *alloc, uint element_size, uint new_size)
{
	void_array *array = array_;
	array_reserve(array_, alloc, element_size, new_size);
	array->size = new_size;
}
Example #16
0
int main(int argc, const char* argv[]) {
    array_t(int) a = NULL;
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);

    array_alloc(a, 0, destructed_element_count_destructor);
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);


    array_append(a, 1);
    test(array_size(a) == 1);
    test(array_capacity(a) >= 1);
    test(a[0] == 1);


    array_append(a, 2);
    test(array_size(a) == 2);
    test(array_capacity(a) >= 2);
    test(a[0] == 1);
    test(a[1] == 2);


    array_append(a, 3);
    test(array_size(a) == 3);
    test(array_capacity(a) >= 3);
    test(a[0] == 1);
    test(a[1] == 2);
    test(a[2] == 3);


    array_insert(a, 0, 0);
    test(array_size(a) == 4);
    test(array_capacity(a) >= 4);
    test(a[0] == 0);
    test(a[1] == 1);
    test(a[2] == 2);
    test(a[3] == 3);


    array_reserve(a, 16);
    test(array_size(a) == 4);
    test(array_capacity(a) == 16);
    test(a[0] == 0);
    test(a[1] == 1);
    test(a[2] == 2);
    test(a[3] == 3);


    array_shrink(a);
    test(array_size(a) == 4);
    test(array_capacity(a) == 4);
    test(a[0] == 0);
    test(a[1] == 1);
    test(a[2] == 2);
    test(a[3] == 3);


    array_remove(a, 0);
    test(array_size(a) == 3);
    test(array_capacity(a) == 4);
    test(a[0] == 1);
    test(a[1] == 2);
    test(a[2] == 3);
    test(destructed_element_count == 1);
    destructed_element_count = 0;


    array_remove_unordered(a,0);
    test(array_size(a) == 2);
    test(array_capacity(a) == 4);
    test(a[0] == 3);
    test(a[1] == 2);
    test(destructed_element_count == 1);
    destructed_element_count = 0;


    array_clear(a);
    test(array_size(a) == 0);
    test(array_capacity(a) >= 0);
    test(destructed_element_count == 2);
    destructed_element_count = 0;


    array_append(a, 0);
    array_append(a, 1);
    array_append(a, 2);
    test(array_size(a) == 3);
    test(array_capacity(a) >= 3);
    test(destructed_element_count == 0);


    array_free(a);
    test(a == NULL);
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);
    test(destructed_element_count == 3);
    destructed_element_count = 0;


    enum { TEST_LENGTH = 1024 };


    array_alloc(a, 0, destructed_element_count_destructor);
    for (int i = 0; i < TEST_LENGTH; ++i) {
        array_append(a, i);
        test(a[i] == i);
    }
    test(array_size(a) == TEST_LENGTH);
    test(array_capacity(a) >= TEST_LENGTH);
    for (int i = 0; i < TEST_LENGTH; ++i) {
        test(a[i] == i);
    }
    {
        int i = 0;
        const int* const end = array_end(a);
        for (int* itr = array_begin(a); itr < end; ++itr) {
            test(*itr == i++);
        }
    }
    {
        int i = 0;
        while (array_size(a)) {
            test(a[0] == i++);
            array_remove(a,0);
        }
        test(array_size(a) == 0);
        test(array_capacity(a) >= TEST_LENGTH);
        test(destructed_element_count == TEST_LENGTH);
        destructed_element_count = 0;
    }
    array_free(a);
    test(a == NULL);
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);


    array_alloc(a, 0, destructed_element_count_destructor);
    for (int i = 0; i < TEST_LENGTH; ++i) {
        array_insert(a, 0, i);
    }
    test(array_size(a) == TEST_LENGTH);
    test(array_capacity(a) >= TEST_LENGTH);
    for (int i = 0; i < TEST_LENGTH; ++i) {
        test(a[i] == (TEST_LENGTH - 1) - i);
    }
    array_free(a);
    test(a == NULL);
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);


    puts("array tests passed");
}
Example #17
0
	ObjectPtr<Array> create_array_with_size(uint32_t sz) {
		ObjectPtr<Array> array = create_array();
		array_reserve(array, sz);
		return array;
	}