static int list_devices(void) { struct vector v; vector_init(&v, sizeof(const char *)); if (fet_db_enum(add_fet_device, &v) < 0) { pr_error("couldn't allocate memory"); vector_destroy(&v); return -1; } printc("Devices supported by FET driver:\n"); namelist_print(&v); vector_destroy(&v); vector_init(&v, sizeof(const char *)); if (fet_olimex_db_enum(add_fet_olimex_device, &v) < 0) { pr_error("couldn't allocate memory"); vector_destroy(&v); return -1; } printc("\n"); printc("Devices supported by Olimex FET driver:\n"); namelist_print(&v); vector_destroy(&v); return 0; }
int cvector_read_moments(complex_t *scnd, complex_t *thrd, complex_t *frth, vector_t *vec, complex_t org) { int n; vector_t *moment, *diff; assert(scnd); assert(thrd); assert(frth); assert(vec); moment = vector_new_and_copy(vec); diff = vector_new_and_copy(vec); cvector_copy_cvector_subtract_scalar(diff, vec, org); cvector_copy_cvector_multiply_cvector(moment, diff, diff); n = vector_get_dimension(vec); *scnd = cvector_get_sum(moment); scnd->real /= (real_t)n, scnd->imag /= (real_t)n; cvector_multiply_cvector(moment, diff); *thrd = cvector_get_sum(moment); thrd->real /= (real_t)n, thrd->imag /= (real_t)n; cvector_multiply_cvector(moment, diff); *frth = cvector_get_sum(moment); frth->real /= (real_t)n, frth->imag /= (real_t)n; vector_destroy(diff); vector_destroy(moment); return n; }
int ivector_read_moments(real_t *scnd, real_t *thrd, real_t *frth, vector_t *vec, real_t org) { vector_t *moment, *diff; assert(scnd); assert(thrd); assert(frth); assert(vec); moment = vector_new_and_copy(vec); diff = vector_new_and_copy(vec); ivector_copy_ivector_subtract_scalar(diff, vec, org); ivector_copy_ivector_multiply_ivector(moment, diff, diff); *scnd = ivector_get_sum(moment) / vector_get_dimension(moment); ivector_multiply_ivector(moment, diff); *thrd = ivector_get_sum(moment) / vector_get_dimension(moment); ivector_multiply_ivector(moment, diff); *frth = ivector_get_sum(moment) / vector_get_dimension(moment); vector_destroy(diff); vector_destroy(moment); return vector_get_dimension(vec); }
int main(int argc, char **argv) { const int n = 3; // init our vectors of size n struct vector *x = vector_create(n); struct vector *y = vector_create(n); struct vector *z = vector_create(n); struct vector *z_ref = vector_create(n); // initialize values for testing for (int i = 0; i < n; i++) { VEC(x, i) = i + 1.f; VEC(y, i) = i + 2.f; VEC(z_ref, i) = 2*i + 3.f; } // test the vector_add() vector_add(x, y, z); // and make sure it equals the reference result assert(vector_is_equal(z, z_ref)); // clean up vector_destroy(x); vector_destroy(y); vector_destroy(z); vector_destroy(z_ref); return 0; }
/* free the memory used by the vetting structure */ void mtsFreeWork (vetting_t ** w) { vetting_t *work = (*w); if (work->desired.X != -SPECIAL || work->desired.Y != -SPECIAL) { heap_free (work->untested.h, free); heap_destroy (&work->untested.h); heap_free (work->no_fix.h, free); heap_destroy (&work->no_fix.h); heap_free (work->no_hi.h, free); heap_destroy (&work->no_hi.h); heap_free (work->hi_candidate.h, free); heap_destroy (&work->hi_candidate.h); } else { while (!vector_is_empty (work->untested.v)) free (vector_remove_last (work->untested.v)); vector_destroy (&work->untested.v); while (!vector_is_empty (work->no_fix.v)) free (vector_remove_last (work->no_fix.v)); vector_destroy (&work->no_fix.v); while (!vector_is_empty (work->no_hi.v)) free (vector_remove_last (work->no_hi.v)); vector_destroy (&work->no_hi.v); while (!vector_is_empty (work->hi_candidate.v)) free (vector_remove_last (work->hi_candidate.v)); vector_destroy (&work->hi_candidate.v); } free (work); (*w) = NULL; }
/* * Unit test for the vector_prepend function. Runs some example * operations and checks that results are as expected, writing * messages to the terminal so that errors can be detected and * pinpointed. * * \return zero on success. */ int test_prepend (void) { Vector vec; int ii; printf ("\nrunning test_prepend...\n"); vector_init (&vec); printf ("initialized: "); vector_dump (&vec); for (ii = -3; ii < 10; ++ii) { if (0 != vector_prepend (&vec, ii)) { printf ("test_prepend ERROR: could not prepend %d\n", ii); vector_destroy (&vec); return -1; } printf ("prepended %2d: ", ii); vector_dump (&vec); } for (ii = 0; ii < 13; ++ii) if (vec.arr[ii] != 9-ii) { printf ("test_prepend ERROR: arr[%d] should be %d but is %d\n", ii, 9-ii, vec.arr[ii]); vector_destroy (&vec); return -1; } printf ("test_prepend SUCCESS\n"); vector_destroy (&vec); return 0; }
/** * Clean up torrent context by freeing all dynamically * allocated memory. */ void torrent_cleanup(torrent_ctx *ctx) { assert(ctx); vector_destroy(&ctx->blocks_hashes.blocks); vector_destroy(&ctx->files); free(ctx->program_name); free(ctx->announce); ctx->announce = ctx->program_name = 0; str_free(ctx->torrent); }
void call_destroy(sip_call_t *call) { // Remove all call messages vector_destroy(call->msgs); // Remove all call streams vector_destroy(call->streams); // Remove all call attributes vector_destroy(call->attrs); // Remove all call rtp packets vector_destroy(call->rtp_packets); // Deallocate call memory sng_free(call); }
void vector_resize(struct Vector * vector) { void * new_storage; int new_length; if (vector->population == vector->length) { new_length = vector->length * 2; new_storage = realloc(vector->storage, sizeof(void *) * vector->length * 2); } else if ((float)vector->population/(float)vector->length < 0.25) { new_length = vector->length / 2; new_storage = realloc(vector->storage, sizeof(void *) * vector->length / 2); } else { return; } /* not sure if this is morally correct. On one hand, the system has no more memory to give. On the other, such a trivial error should not bring down the program. Comments? */ if (new_storage == NULL) { vector_destroy(vector); fprintf(stderr, "could not allocate more memory for vector. Sorry\n"); exit (1); } vector->storage = new_storage; vector->length = new_length; }
int main(int argc, char **argv) { MPI_Init(&argc, &argv); libmrc_params_init(argc, argv); struct vector *vec = vector_create(MPI_COMM_WORLD); vector_set_from_options(vec); vector_setup(vec); vector_view(vec); int nr_elements; vector_get_param_int(vec, "nr_elements", &nr_elements); for (int i = 0; i < nr_elements; i++) { vector_set_element(vec, i, 2. * i); } for (int i = 0; i < nr_elements; i++) { assert(vector_get_element(vec, i) == 2. * i); } vector_destroy(vec); MPI_Finalize(); }
int main() { struct vector_t myVector, secondVector; vector_init(&myVector); vector_push_back(&myVector,3); vector_push_back(&myVector,1); vector_push_back(&myVector,4); vector_push_back(&myVector,1); vector_push_back(&myVector,5); vector_push_back(&myVector,9); vector_push_back(&myVector,2); vector_push_back(&myVector,6); vector_copy(&myVector, &secondVector); printf("Size of copied vector: %d\n", vector_get_size(secondVector)); printf("Size: %d\n", vector_get_size(myVector)); printf("Pop: %d\n", vector_pop_back(&myVector)); printf("Pop: %d\n", vector_pop_back(&myVector)); printf("Pop: %d\n", vector_pop_back(&myVector)); printf("Pop: %d\n", vector_pop_back(&myVector)); printf("Pop: %d\n", vector_pop_back(&myVector)); printf("Size: %d\n", vector_get_size(myVector)); printf("Pop: %d\n", vector_pop_back(&myVector)); printf("Pop: %d\n", vector_pop_back(&myVector)); printf("Pop: %d\n", vector_pop_back(&myVector)); printf("Size: %d\n", vector_get_size(myVector)); vector_destroy(&myVector); return 0; }
int main(int argc, char **argv) { vector *c = NULL; int v1 = 4; int v2 = 1; int v3 = 3; int v4 = 2; int v5 = 16; int v6 = 9; int v7 = 10; int v8 = 14; int v9 = 8; int v10 = 7; c = vector_create(); c->push(c, (void *)(&v1 )); c->push(c, (void *)(&v2 )); c->push(c, (void *)(&v3 )); c->push(c, (void *)(&v4 )); c->push(c, (void *)(&v5 )); c->push(c, (void *)(&v6 )); c->push(c, (void *)(&v7 )); c->push(c, (void *)(&v8 )); c->push(c, (void *)(&v9 )); c->push(c, (void *)(&v10)); Print(c, visit); make_heap(c, compare); Print(c, visit); printf("\n===============================\n"); heap_sort(c, compare); Print(c, visit); vector_destroy(&c, NULL); return 0; }
int cmd_help(char **arg) { const char *topic = get_arg(arg); if (topic) { struct cmddb_record cmd; struct opdb_key key; if (!cmddb_get(topic, &cmd)) { printc("\x1b[1mCOMMAND: %s\x1b[0m\n\n%s\n", cmd.name, cmd.help); return 0; } if (!opdb_get(topic, &key, NULL)) { printc("\x1b[1mOPTION: %s (%s)\x1b[0m\n\n%s\n", key.name, type_text(key.type), key.help); return 0; } printc_err("help: unknown command: %s\n", topic); return -1; } else { struct vector v; vector_init(&v, sizeof(const char *)); if (!cmddb_enum(push_command_name, &v)) { printc("Available commands:\n"); namelist_print(&v); printc("\n"); } else { pr_error("help: can't allocate memory for command list"); } vector_realloc(&v, 0); if (!opdb_enum(push_option_name, &v)) { printc("Available options:\n"); namelist_print(&v); printc("\n"); } else { pr_error("help: can't allocate memory for option list"); } vector_destroy(&v); printc("Type \"help <topic>\" for more information.\n"); printc("Use the \"opt\" command (\"help opt\") to set " "options.\n"); #if defined(__Windows__) && !defined(USE_READLINE) printc("Press Ctrl+Z, Enter to quit.\n"); #else printc("Press Ctrl+D to quit.\n"); #endif } return 0; }
void mnist_images_destroy(mnist_images_t images) { assert(images); for (size_t i = 0; i < images->size; i++) { vector_destroy(images->imgs[i]); } Free(images); }
static void tcas_renderer_fin(TcasStPtr pTcas) { if (pTcas->indexStreams) vector_destroy(pTcas->indexStreams); pTcas->indexStreams = NULL; if (pTcas->chunksCache.streamCount > 0) libtcas_free_chunks_cache(&pTcas->chunksCache); libtcas_close_file(&pTcas->file); }
void test4(void) { vector_t vec = vector_from_string_list(NULL_VECTOR, "1920x1080,1280x720,720x576,720x480,704x576", NULL); vector_foreach(vec, test4_walker, NULL); printf("size: %d, capacity: %d\n", vector_size(vec), vector_capacity(vec)); vector_clear(vec); vector_destroy(vec); }
int main() { struct vector_t vec; struct vector_t vec2; int i; printf("Original vector test\n"); vector_init(&vec); printf("Pushing into vector\n"); for(i=0; i<10; i++) { vector_push_back(&vec, i); printf("Index: %d\n", vector_get_size(vec)); } printf("Popping out of vector\n"); for(i=0; i<10; i++) { vector_pop_back(&vec); printf("Index: %d\n", vector_get_size(vec)); } vector_copy(&vec, &vec2); vector_destroy(&vec); printf("Copied vector test\n"); printf("Pushing in vector\n"); for(i=0; i<10; i++) { vector_push_back(&vec2, i); printf("Index: %d\n", vector_get_size(vec2)); } printf("Popping out of vector\n"); for(i=0; i<10; i++) { vector_pop_back(&vec2); printf("Index: %d\n", vector_get_size(vec2)); } vector_destroy(&vec2); return 0; }
void capture_deinit() { // Close pcap handler capture_close(); // Deallocate vectors vector_set_destroyer(capture_cfg.sources, vector_generic_destroyer); vector_destroy(capture_cfg.sources); vector_set_destroyer(capture_cfg.tcp_reasm, packet_destroyer); vector_destroy(capture_cfg.tcp_reasm); vector_set_destroyer(capture_cfg.ip_reasm, packet_destroyer); vector_destroy(capture_cfg.ip_reasm); // Remove capture mutex pthread_mutex_destroy(&capture_cfg.lock); }
/* Open new pptp_connection. Returns NULL on failure. */ PPTP_CONN * pptp_conn_open(int inet_sock, int isclient, pptp_conn_cb callback) { PPTP_CONN *conn; /* Allocate structure */ if ((conn = malloc(sizeof(*conn))) == NULL) return NULL; if ((conn->call = vector_create()) == NULL) { free(conn); return NULL; } /* Initialize */ conn->inet_sock = inet_sock; conn->conn_state = CONN_IDLE; conn->ka_state = KA_NONE; conn->ka_id = 1; conn->call_serial_number = 0; conn->callback = callback; /* Create I/O buffers */ conn->read_size = conn->write_size = 0; conn->read_alloc = conn->write_alloc = INITIAL_BUFSIZE; conn->read_buffer = malloc(sizeof(*(conn->read_buffer)) * conn->read_alloc); conn->write_buffer = malloc(sizeof(*(conn->write_buffer)) * conn->write_alloc); if (conn->read_buffer == NULL || conn->write_buffer == NULL) { if (conn->read_buffer != NULL) free(conn->read_buffer); if (conn->write_buffer != NULL) free(conn->write_buffer); vector_destroy(conn->call); free(conn); return NULL; } /* Make this socket non-blocking. */ fcntl(conn->inet_sock, F_SETFL, O_NONBLOCK); /* Request connection from server, if this is a client */ if (isclient) { struct pptp_start_ctrl_conn packet = { PPTP_HEADER_CTRL(PPTP_START_CTRL_CONN_RQST), hton16(PPTP_VERSION), 0, 0, hton32(PPTP_FRAME_CAP), hton32(PPTP_BEARER_CAP), hton16(PPTP_MAX_CHANNELS), hton16(PPTP_FIRMWARE_VERSION), PPTP_HOSTNAME, PPTP_VENDOR }; /* fix this packet, if necessary */ int idx, rc; idx = get_quirk_index(); if (idx != -1 && pptp_fixups[idx].start_ctrl_conn) { if ((rc = pptp_fixups[idx].start_ctrl_conn(&packet))) warn("calling the start_ctrl_conn hook failed (%d)", rc); } if (pptp_send_ctrl_packet(conn, &packet, sizeof(packet))) conn->conn_state = CONN_WAIT_CTL_REPLY; else return NULL; /* could not send initial start request. */ } /* Set up interval/keep-alive timer */ /* First, register handler for SIGALRM */ sigpipe_create(); sigpipe_assign(SIGALRM); global.conn = conn; /* Reset event timer */ pptp_reset_timer(); /* all done. */ return conn; }
int get_heading (data_t* data) { // Initialize heading value - it's an integer because it is // going to eventually be binned into North, South, East, or West vector north, east, mag_vector, accel_vector, from; int heading; // Initialize north and east vectors vector_init(&north, 0, 0, 0); vector_init(&east, 0, 0, 0); // Put magnetometer and accelerometer data into vector format vector_init(&mag_vector, data->Mx, data->My, data-> Mz); vector_init(&accel_vector, data->Ax, data->Ay, data-> Az); // Initialize "from" vector based on carrier board design vector_init(&from, 1, 0, 0); // Subtract calibration offset from magnetometer readings mag_vector.x -= ((float) MAG_MAX_X + MAG_MIN_X) / 2.0; mag_vector.y -= ((float) MAG_MAX_Y + MAG_MIN_Y) / 2.0; mag_vector.z -= ((float) MAG_MAX_Z + MAG_MIN_Z) / 2.0; // Compute East and North vector_cross(&mag_vector, &accel_vector, &east); vector_norm(&east); vector_cross(&accel_vector, &east, &north); vector_norm(&north); // Compute the heading and make sure it's positive heading = (int)(atan2(vector_dot(&east, &from), vector_dot(&north, &from))* 180 / PI); if (heading < 0) { heading += 360; } // Memory cleanup vector_destroy(&north); vector_destroy(&east); vector_destroy(&mag_vector); vector_destroy(&accel_vector); vector_destroy(&from); return heading; }
void test1(void) { vector_t vec = vector_create2(0, 4); int value = 1; for (int i = 0; i < 1000; i++) vector_push_back(vec, &value); vector_foreach(vec, test1_walker, NULL); vector_destroy(vec); }
static void watchfile_destroy(void) { for (int i = 0; i < watchfile_entries.size; ++i) { struct watchfile_entry *entry = vector_at(&watchfile_entries, i); free(entry->name); adex_destroy(&entry->adex); } vector_destroy(&watchfile_entries); }
/** * free the vector * @param vec the vector to free */ void vector_free(vector_t * vec) { #ifdef DEBUG assert(NULL != vec); #endif vector_destroy(vec); free(vec); }
void test__type_get_varg_value__cstl_builtin(void** state) { _typeinfo_t t_info; size_t i = 0; vector_t* pvec_result = create_vector(int); vector_t* pvec_input = create_vector(int); _test__get_type(&t_info, "vector_t"); t_info._t_style = _TYPE_CSTL_BUILTIN; vector_init(pvec_result); vector_init_elem(pvec_input, 10, 100); _test__type_get_varg_value__stub(pvec_result, &t_info, pvec_input); assert_true(vector_size(pvec_result) == 10); for (i = 0; i < 10; ++i) { assert_true(*(int*)vector_at(pvec_result, i) == 100); } vector_destroy(pvec_result); vector_destroy(pvec_input); }
void media_destroyer(void *item) { sdp_media_t *media = (sdp_media_t *) item; if (!item) return; vector_destroy(media->formats); sng_free(media); }
/* * read_images: Reads the images from and mnist image db file and puts the * images into and array of vectors. * * Parameters: * - train: boolean value, read from training db if 0, test db if 1 * * Return value: * - array of vectors containing mnist images */ mnist_images_t read_images(uint32_t train) { // Set the filename based the mode (train or test) char *full_path; if (train) { full_path = concat_fname(mnist_path, train_image_fname); } else { full_path = concat_fname(mnist_path, test_image_fname); } // Open the file for reading char *mode = FILE_MODE; FILE *fp = Fopen(full_path, mode); // Read the header of the file uint8_t header[IMAGE_HEADER_SIZE]; Fread(header, sizeof(uint8_t), IMAGE_HEADER_SIZE, fp); // Extract size info from mnist db header uint32_t num_images = read_word(header, NUM_ITEMS_OFFSET); uint32_t rows = read_word(header, ROW_OFFSET); uint32_t cols = read_word(header, COL_OFFSET); uint32_t img_size = rows * cols; // Create array of mnist image vectors vector_t *mnist_data = (vector_t*) Calloc(num_images, sizeof(vector_t)); // Populate vectors with one image each for (uint32_t i = 0; i < num_images; i++) { mnist_data[i] = Vector((size_t) img_size); uint8_t *image_bytes = (uint8_t*) Calloc(img_size, sizeof(uint8_t)); uint32_t actual_size; // Read img_size bytes from the db file into the byte array if ((actual_size = fread(image_bytes, sizeof(uint8_t), img_size, fp)) < img_size) { Free(image_bytes); for (uint32_t i = 0; i < num_images; i++) vector_destroy(mnist_data[i]); return NULL; } // Move 8 bit data to 32 bit integer for more precision uint32_t *vector_data = (uint32_t*) Calloc(img_size, sizeof(uint32_t)); for (uint32_t j = 0; j < img_size; j++) { vector_data[j] = (uint32_t) image_bytes[j]; } mnist_data[i]->data = vector_data; Free(image_bytes); } // Create the mnist_images_t pointer and populate the struct it points to mnist_images_t mnist_imgs = Mnist_images((size_t) num_images); mnist_imgs->imgs = mnist_data; Free(full_path); Fclose(fp); return mnist_imgs; }
static void BM_VectorDestroy(benchmark::State& state) { vector_t * vector = NULL; while(state.KeepRunning()) { vector = vector_new(sizeof(int)); vector_destroy(vector); } }
void del_value(values *v, const char *key) { vector *values_set; if (hash_get(v, key, (void**)(&values_set)) == ERR_HASH_NOTFOUND) { return; } vector_destroy(values_set); return; }
void test__create_vector__registed_type(void** state) { typedef struct _tag__create_vector__registed_type{int n_elem;}_create_vector__registed_type_t; vector_t* pvec = NULL; type_register(_create_vector__registed_type_t, NULL, NULL, NULL, NULL); pvec = _create_vector("_create_vector__registed_type_t"); assert_true(pvec != NULL); vector_destroy(pvec); }
void hashtbl_destroy(hashtbl_t *h) { if (!h) return; for (size_t i = 0; i < h->bktnum; ++i) { if (h->buckets[i]) { vector_destroy(h->buckets[i]); } } free(h->buckets); free(h); }