예제 #1
0
void* matrix_create(size_t width, size_t height, size_t size, const void* zero)
{
	void* matrix;
	int i;

	matrix = array_create(width, sizeof(void*), NULL);
	if (matrix == NULL)
		goto exception_matrix_bad_alloc;

	for (i = 0; i < width; i++)
	{
		((void**)matrix)[i] = array_create(height, size, zero);
		if (((void**)matrix)[i] == NULL)
			goto exception_matrix_i_bad_alloc;
	}
	return matrix;

exception_matrix_i_bad_alloc:
	for (i--; i >= 0; i--)
		array_destroy(((void**)matrix)[i], height, size, NULL);
	array_destroy(matrix, width, sizeof(void*), NULL);

exception_matrix_bad_alloc:
	return NULL;
}
예제 #2
0
void	hmap_destroy(t_hmap *map)
{
	int			i;
	int			j;
	t_array		*contents;

	i = 0;
	j = 0;
	if (map)
	{
		if (map->contents)
		{
			while (i < ARRAY_COUNT(map->contents))
			{
				contents = array_get(map->contents, i);
				if (contents)
				{
					while (j < ARRAY_COUNT(contents))
						free(array_get(contents, j));
					array_destroy(contents);
				}
			}
			array_destroy(map->contents);
		}
		free(map);
	}
}
예제 #3
0
void zc_mgr_deinit(ZoneClusterMgr* mgr)
{
    if (mgr->activeZoneClusters)
    {
        // Master is responsible for destroying the actual individual zone clusters
        // otherwise we run into an issue where it's trying to send shutdown signals after
        // the IPC path has already been closed.
        array_destroy(mgr->activeZoneClusters);
        mgr->activeZoneClusters = NULL;
    }
    
    if (mgr->zoneClustersBySourceId)
    {
        array_destroy(mgr->zoneClustersBySourceId);
        mgr->zoneClustersBySourceId = NULL;
    }
    
    if (mgr->zoneReservations)
    {
        array_destroy(mgr->zoneReservations);
        mgr->zoneReservations = NULL;
    }
    
    if (mgr->zoneShortNameMap)
    {
        zone_short_name_map_destroy(mgr->zoneShortNameMap);
        mgr->zoneShortNameMap = NULL;
    }
}
예제 #4
0
void matrix_destroy(void* matrix, size_t width, size_t height, size_t size, void (*destruct)(void*))
{
	int i;
	for (i = 0; i < width; i++)
		array_destroy(((void**)matrix)[i], height, size, destruct);
	array_destroy(matrix, width, sizeof(void*), NULL);
}
예제 #5
0
파일: route.c 프로젝트: csko/yaosp
__init int init_routes( void ) {
    if ( array_init( &static_routes ) != 0 ) {
        goto error1;
    }

    if ( array_init( &device_routes ) != 0 ) {
        goto error2;
    }

    array_set_realloc_size( &static_routes, 32 );
    array_set_realloc_size( &device_routes, 8 );

    route_mutex = mutex_create( "route mutex", MUTEX_NONE );

    if ( route_mutex < 0 ) {
        goto error3;
    }

    return 0;

 error3:
    array_destroy( &device_routes );

 error2:
    array_destroy( &static_routes );

 error1:
    return -1;
}
예제 #6
0
int main(int argc, char *argv[]) {
    char option = '\0';
    char *filepath = NULL;
    unsigned int length = 0;
    int *array = NULL;
    int *original_array = NULL;
	struct sorting_stats ops;
    /* parse the filepath given in command line arguments */
    filepath = parse_filepath(argc, argv);

    /* parse the array given in the input file */
    array = array_from_file(filepath, &length);

    /* save a copy of array used to make some checks later */
    original_array = array_duplicate(array, length);

    /* print a simple menu and do the actual sorting */
    do {
        option = print_menu();
        switch (option) {
        case INSERTION_SORT:
            ops = insertion_sort(array, length);
            break;
        case SELECTION_SORT:
            ops = selection_sort(array, length);
            break;
        case QUICK_SORT:
            ops = quick_sort(array, length);
            break;
        case EXIT:
            printf("Exiting.\n");
            return (EXIT_SUCCESS);
        default:
            printf("\n\"%c\" is invalid. Please choose a valid option."
                   "\n\n", option);
        }
    } while (!is_valid_option(option));

    /* show the ordered array in the screen */
    array_dump(array, length);
    
    // show the comparation of the algorithm
    printf("Comparisons: %d \n",(int)ops.comps);
    printf("Swaps: %d \n",(int)ops.swaps);
    
    /* check if it is sorted */
    assert(array_is_sorted(array, length));

    /* check if it is a permutation of original */
    assert(array_is_permutation_of(array, original_array, length));

    /* destroy array */
    array_destroy(array);
    array_destroy(original_array);

    return (EXIT_SUCCESS);
}
예제 #7
0
파일: thread.c 프로젝트: adi-mishra/CSC369
/*
 * Thread final cleanup.
 */
void
thread_shutdown(void)
{
	array_destroy(sleepers);
	sleepers = NULL;
	array_destroy(zombies);
	zombies = NULL;
	// Don't do this - it frees our stack and we blow up
	//thread_destroy(curthread);
}
예제 #8
0
/**
 * Destroy a single test suite and associated data
 */
static void destroy_suite(test_suite_t *suite)
{
	test_case_t *tcase;

	while (array_remove(suite->tcases, 0, &tcase))
	{
		array_destroy(tcase->functions);
		array_destroy(tcase->fixtures);
	}
	free(suite);
}
예제 #9
0
파일: main.cpp 프로젝트: yilirin/CPPHW1
int main()
{
    CArray array;
    array_initial(&array);
    
    array_recap(&array, 10);
    assert(array_capacity(&array) == 10);
    
    //////////////////////////////////////////////////////////////////////////
    for (int i = 0; i < 20; ++i)
    {
        array_append(&array, i);
    }
    assert(array_size(&array) == 20);
    
//    printarray(&array);
    
    for (int i = 0; i < array_size(&array); ++i)
    {
        assert(*array_at(&array, i) == i);
    }
    
    //////////////////////////////////////////////////////////////////////////
    CArray array2, array3;
    array_initial(&array2);
    array_initial(&array3);
    
    array_copy(&array, &array2);

    assert(array_compare(&array, &array2) == true);
    array_copy(&array, &array3);
    assert(array_compare(&array, &array3) == true);
//    printarray(&array2);
    
    //////////////////////////////////////////////////////////////////////////
    array_insert(&array2, 2, 3);
    assert(array_compare(&array, &array2) == false);
//    printarray(&array2);

    
    //////////////////////////////////////////////////////////////////////////
    *array_at(&array3, 2) = 5;
    assert(array_compare(&array, &array3) == false);
//    printarray(&array3);

    //////////////////////////////////////////////////////////////////////////
    array_destroy(&array);
    array_destroy(&array2);
    array_destroy(&array3);
    
    return 0;
}
예제 #10
0
void inventory_exit(void) {
	log_debug("Shutting down inventory subsystem");

	// destroy all sessions to ensure that all external references are released
	// before starting to destroy the remaining objects. then all remaining
	// relations between objects that constrains the destruction order are known
	array_destroy(&_sessions, inventory_destroy_session);

	// unlock and release all stock string objects
	array_destroy(&_stock_strings, inventory_unlock_and_release_string);

	// object types have to be destroyed in a specific order. if objects of
	// type A can use (have a reference to) objects of type B then A has to be
	// destroyed before B. so A has a chance to properly release its references
	// to type B objects. otherwise type B object could already be destroyed
	// when type A objects try to release them
	//
	// there are the following relationships:
	// - program uses process, list and string
	// - process uses file, list and string
	// - directory uses string
	// - file uses string
	// - list can contain any object as item, currently only string is used
	// - string doesn't use other objects
	array_destroy(&_objects[OBJECT_TYPE_PROGRAM], inventory_destroy_object);
	array_destroy(&_objects[OBJECT_TYPE_PROCESS], inventory_destroy_object);
	array_destroy(&_objects[OBJECT_TYPE_DIRECTORY], inventory_destroy_object);
	array_destroy(&_objects[OBJECT_TYPE_FILE], inventory_destroy_object);
	array_destroy(&_objects[OBJECT_TYPE_LIST], inventory_destroy_object);
	array_destroy(&_objects[OBJECT_TYPE_STRING], inventory_destroy_object);
}
예제 #11
0
void text_renderer_destroy(text_renderer_p renderer) {
	texture_destroy(renderer->texture);
	
	for(size_t i = 0; i < renderer->lines->length; i++)
		array_destroy( array_elem(renderer->lines, text_renderer_line_t, i).cells );
	array_destroy(renderer->lines);
	
	for(hash_elem_t e = hash_start(renderer->fonts); e != NULL; e = hash_next(renderer->fonts, e))
		text_renderer_font_destroy(renderer, hash_key(e));
	hash_destroy(renderer->fonts);
	
	FT_Error error = FT_Done_FreeType(renderer->freetype);
	if (error)
		printf("FT_Done_FreeType error\n");
}
예제 #12
0
파일: network.c 프로젝트: vszurma/brickd
void network_exit(void) {
	log_debug("Shutting down network subsystem");

	array_destroy(&_clients, (ItemDestroyFunction)client_destroy); // might call network_create_zombie
	array_destroy(&_zombies, (ItemDestroyFunction)zombie_destroy);

	if (_plain_server_socket_open) {
		event_remove_source(_plain_server_socket.base.handle, EVENT_SOURCE_TYPE_GENERIC);
		socket_destroy(&_plain_server_socket);
	}

	if (_websocket_server_socket_open) {
		event_remove_source(_websocket_server_socket.base.handle, EVENT_SOURCE_TYPE_GENERIC);
		socket_destroy(&_websocket_server_socket);
	}
}
예제 #13
0
int matrix_resize(void* matrix, size_t width, size_t height, size_t new_width, size_t new_height, size_t size, const void* zero, void (*destruct)(void*))
{
	void* new_matrix;
	int i;

	if (matrix == NULL)
		goto exception_matrix_null;

	new_matrix = matrix_create(new_width, new_height, size, NULL);
	if (new_matrix == NULL)
		goto exception_new_matrix_bad_alloc;

	for (i = 0; (i < new_width && zero != NULL) || i < width; i++)
	{
		if (i < new_width && i < width)
			array_get_into((*(void***)matrix)[i], height, ((void**)new_matrix)[i], new_height, size, zero, destruct);
		else if (i < new_width)
			array_fill(((void**)new_matrix)[i], new_height, size, zero);
		else if (i < width)
			array_destroy((*(void***)matrix)[i], height, size, destruct);
	}
	*(void**)matrix = new_matrix;
	return 1;

exception_new_matrix_bad_alloc:
exception_matrix_null:
	return 0;
}
예제 #14
0
파일: dynarray.c 프로젝트: nilium/snow-palm
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;
}
예제 #15
0
void conf_value_destroy(conf_value *cv)
{
    conf_value **cv_sub;
    
    if(cv == NULL){
        return;
    }
    
    if(cv->type == CONF_VALUE_UNKNOW){
        rmt_free(cv);
        return;
    }else if(cv->type == CONF_VALUE_STRING){
        if(cv->value != NULL){
            sdsfree(cv->value);
        }
    }else if(cv->type == CONF_VALUE_ARRAY){
        if(cv->value != NULL){
            while(array_n(cv->value) > 0){
                cv_sub = array_pop(cv->value);
                conf_value_destroy(*cv_sub);
            }

            array_destroy(cv->value);
        }
    }else{
        NOT_REACHED();
    }

    rmt_free(cv);
}
예제 #16
0
void
msg_put(struct msg *msg)
{
    listNode *node;
    struct mbuf *mbuf;

    log_debug(LOG_VVERB, "put msg %p id %"PRIu64"", msg, msg->id);

    while (listLength(msg->data) > 0) {

        node = listFirst(msg->data);
        mbuf = listNodeValue(node);

        listDelNode(msg->data, node);

        mbuf_put(mbuf);
    }

    listRelease(msg->data);
    msg->data = NULL;

    if (msg->frag_seq) {
        rmt_free(msg->frag_seq);
        msg->frag_seq = NULL;
    }

    if (msg->keys) {
        msg->keys->nelem = 0; /* a hack here */
        array_destroy(msg->keys);
        msg->keys = NULL;
    }

    msg->mb = NULL;
}
예제 #17
0
void _hashtable_rehash(HASHTABLE *t, size_t newsize)
{
	ARRAY table;
	DLIST *bucket;
	size_t i, j, hash;
	DLIST_ITER it, end;

	array_init(&table, sizeof(DLIST));
	array_resize(&table, newsize);
	for (i = 0; i != newsize; ++i) {
		dlist_init((DLIST*)array_at(&table, i), t->element_size);
	}
	
	j = array_size(&t->table);
	for (i = 0; i != j; ++i) {
		bucket = (DLIST*)array_at(&t->table, i);
		if (dlist_size(bucket)) {
			end = dlist_end(bucket);
			for (it = dlist_begin(bucket); it != end; it = dlist_next(it)) {
				hash = _hashtable_hash(t, dlist_at(it)) % newsize;
				dlist_push((DLIST*)array_at(&table, hash), dlist_at(it));
			}	
		}
	}
	array_destroy(&t->table);
	memcpy(&t->table, &table, sizeof(ARRAY));
}
예제 #18
0
파일: 2_14.cpp 프로젝트: venustom85/gragon
int g_main(int argc, char ** argv)
{
    int * array = NULL;
    int length = 0;
    int case_count = 0;

    while (true) {
        case_count++;
        array = array_read<int>(cin, length);
        if (array == NULL) {
            break;
        }

        cout << "Case " << case_count << ": ";
        array_print(array, length) << '\n';

        partition(array, array + length, compare_to_zero());

        cout << "Result: ";
        array_print(array, length) << "\n\n";

        array_destroy(array);
    }

    return 0;
}
예제 #19
0
파일: check_array.c 프로젝트: dholm/ev3
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);
}
예제 #20
0
void
as_destroy(struct addrspace *as)
{
    /*
     * Clean up as needed.
     */
    unsigned len, i;
    pt_destroy(as, as->pagetable);
    struct region *region_ptr;

    for (i = 0; i < PT_LEVEL_SIZE; i++) {
    	if (as->pt_locks[i])
	    	lock_destroy(as->pt_locks[i]);
    }
    
    len = array_num(as->as_regions);
    // for (i = len - 1; i > 0; i--){
    //  region_ptr = array_get(as->as_regions, i);
    //  kfree(region_ptr);
    //  array_remove(as->as_regions, i);
    // }

    i = len - 1;
    while (len > 0){
        region_ptr = array_get(as->as_regions, i);
        kfree(region_ptr);
        array_remove(as->as_regions, i);
        if (i == 0)
            break;
        i--;
    }
    array_destroy(as->as_regions);
    kfree(as);
}
예제 #21
0
int test1()
{
    double pre, post;
    int *tmp;
    array_t a = array_init( int, 1 );
    pre = gettime_ms();
    for ( int i = 0; i < 1E7; i++ ) array_int_append( &a, i );
    post = gettime_ms();

    int success = 0;
    int counter = 0;
    for ( int i = 0; i < 1E7; i++ ) {
        tmp = array_int( a );
        /* char *dame = array_char( a ); /\**< incorrect type will be asserted *\/ */
        /* printf( "%c\n", dame[i] ); */
        if ( tmp[i] == i ) success++;
        counter++;
    }
    printf( "# array has %lld elems and malloced %lld Bytes\n",
            array_size( a ), a.alloced * sizeof(int) );
    printf( "# %5d / %5d are correct.\n", success, counter );
    printf( "# %6.2lf[ms] is elapsed.\n", post - pre );

    array_destroy( &a );

    return success==counter;
}
예제 #22
0
void image_destroy(struct vg_image *img)
{
   struct vg_context *ctx = vg_current_context();
   vg_context_remove_object(ctx, VG_OBJECT_IMAGE, img);


   if (img->parent) {
      /* remove img from the parent child array */
      int idx;
      struct vg_image **array =
         (struct vg_image **)img->parent->children_array->data;

      for (idx = 0; idx < img->parent->children_array->num_elements; ++idx) {
         struct vg_image *child = array[idx];
         if (child == img) {
            break;
         }
      }
      debug_assert(idx < img->parent->children_array->num_elements);
      array_remove_element(img->parent->children_array, idx);
   }

   if (img->children_array && img->children_array->num_elements) {
      /* reparent the children */
      VGint i;
      struct vg_image *parent = img->parent;
      struct vg_image **children =
         (struct vg_image **)img->children_array->data;
      if (!parent) {
         VGint min_x = children[0]->x;
         parent = children[0];

         for (i = 1; i < img->children_array->num_elements; ++i) {
            struct vg_image *child = children[i];
            if (child->x < min_x) {
               parent = child;
            }
         }
      }

      for (i = 0; i < img->children_array->num_elements; ++i) {
         struct vg_image *child = children[i];
         if (child != parent) {
            child->parent = parent;
            if (!parent->children_array) {
               parent->children_array = array_create(
                  sizeof(struct vg_image*));
            }
            array_append_data(parent->children_array,
                              &child, 1);
         } else
            child->parent = NULL;
      }
      array_destroy(img->children_array);
   }

   pipe_texture_reference(&img->texture, NULL);
   free(img);
}
예제 #23
0
void spellbook_deinit(Spellbook* spellbook)
{
    if (spellbook->knownSpells)
    {
        array_destroy(spellbook->knownSpells);
        spellbook->knownSpells = NULL;
    }
}
예제 #24
0
/**
 * Destroys the Array structure along with all the data it holds.
 *
 * @note
 * This function should not be called on a array that has some of its elements
 * allocated on the stack.
 *
 * @param[in] ar the array that is being destroyed
 */
void array_destroy_free(Array *ar)
{
    size_t i;
    for (i = 0; i < ar->size; i++)
        ar->mem_free(ar->buffer[i]);

    array_destroy(ar);
}
예제 #25
0
파일: heap.c 프로젝트: plurmiscuous/JinxOS
void
heap_destroy(struct heap* h) {
    if (h != NULL) {
        ASSERT_HEAP(h);
        array_destroy(h->vals);
    }
    kfree(h);
}
예제 #26
0
/**
 * Destroys the Array structure along with all the data it holds.
 *
 * @note
 * This function should not be called on a array that has some of its elements
 * allocated on the stack.
 *
 * @param[in] ar the array that is being destroyed
 */
void array_destroy_cb(Array *ar, void (*cb) (void*))
{
    size_t i;
    for (i = 0; i < ar->size; i++)
        cb(ar->buffer[i]);

    array_destroy(ar);
}
예제 #27
0
void hardware_exit(void) {
	log_debug("Shutting down hardware subsystem");

	if (_stacks.count > 0) {
		log_warn("Still %d stack(s) connected", _stacks.count);
	}

	array_destroy(&_stacks, NULL);
}
예제 #28
0
void hashtable_destroy(HASHTABLE *t)
{
	size_t i, j;
	j = array_size(&t->table);
	for (i = 0; i != j; ++i) {
		dlist_destroy((DLIST*)array_at(&t->table, i));
	}
	array_destroy(&t->table);
}
예제 #29
0
파일: execfile.c 프로젝트: wodz/open21xx
void objects_destroy(void)
{
    OBJECT_FILE *file;

    for ( file = objects ; file ; )
    {
        file = objects->next_object;
        object_close( objects );
        objects = file;
    }

    array_destroy( &scn_array );
    array_destroy( &link_array );
    if ( map_file_name )
    {
        free( map_file_name );
    }
}
예제 #30
0
/**
 * Unload and destroy test suites and associated data
 */
static void unload_suites(array_t *suites)
{
	test_suite_t *suite;

	while (array_remove(suites, 0, &suite))
	{
		destroy_suite(suite);
	}
	array_destroy(suites);
}