void sip_list_sorter(vector_t *vector, void *item) { sip_call_t *prev, *cur = (sip_call_t *)item; int count = vector_count(vector); int i; // First item is alway sorted if (vector_count(vector) == 1) return; prev = vector_item(vector, vector_count(vector) - 2); // Check if the item is already sorted if (call_attr_compare(cur, prev, calls.sort.by) == 0) { return; } for (i = count - 2 ; i >= 0; i--) { // Get previous item prev = vector_item(vector, i); // Check if the item is already in a sorted position int cmp = call_attr_compare(cur, prev, calls.sort.by); if ((calls.sort.asc && cmp > 0) || (!calls.sort.asc && cmp < 0)) { vector_insert(vector, item, i + 1); return; } } // Put this item at the begining of the vector vector_insert(vector, item, 0); }
int main(void) { vector v; vector_init(&v); vector_add(&v, "emil"); vector_add(&v, "hannes"); vector_add(&v, "lydia"); vector_add(&v, "olle"); vector_add(&v, "erik"); int i; printf("first round:\n"); for (i = 0; i < vector_count(&v); i++) { printf("%s\n", vector_get(&v, i)); } vector_delete(&v, 1); vector_delete(&v, 3); printf("second round:\n"); for (i = 0; i < vector_count(&v); i++) { printf("%s\n", vector_get(&v, i)); } vector_free(&v); return 0; }
memi document_block_at(Document *document, memi offset, memi search_begin, memi search_end) { hale_assert(search_begin <= search_end); hale_assert(search_begin < vector_count(document->blocks)); hale_assert(search_end < vector_count(document->blocks)); hale_assert(offset <= _buffer_length(document->buffer)); if (offset >= vector_last(&document->blocks).end) { return vector_count(document->blocks) - 1; } // memi search_begin = search_begin; // memi search_end = search_end < 0 ? document->blocks.count - 1 : search_end; for (;;) { switch (search_end - search_begin) { case 0: if (document->blocks[search_begin].end <= offset) return search_begin + 1; else return search_begin; case 1: if (document->blocks[search_begin].end <= offset) { if(document->blocks[search_end].end <= offset) return search_end + 1; else return search_end; } else return search_begin; default: memi pivot = (search_end + search_begin) / 2; memi value = document->blocks[pivot].end; if (value == offset) { return pivot + 1; } else if (value < offset) { search_begin = pivot + 1; } else { search_end = pivot - 1; } break; } } }
int main(int argc, char **argv) { int *a, *b, *c, *d, *e, *f, *g; a = (int *)malloc(sizeof(int)); b = (int *)malloc(sizeof(int)); c = (int *)malloc(sizeof(int)); d = (int *)malloc(sizeof(int)); e = (int *)malloc(sizeof(int)); f = (int *)malloc(sizeof(int)); g = (int *)malloc(sizeof(int)); *a = 6; *b = 1; *c = 99; *d = -17; *e = 22; *f = 9; *g = 6; struct Vector *iv = create_vector(2, free); vector_push_back(iv, a); vector_push_back(iv, b); vector_push_back(iv, c); vector_push_back(iv, d); vector_push_front(iv, e); vector_push_front(iv, f); vector_insert(iv, g, 5); vector_foreach(iv, print); printf("\nCount: %d, Capacity: %d--------------\n", vector_count(iv), vector_capacity(iv)); printf("%d\n", *f); vector_remove(iv, f, compare, TRUE); vector_foreach(iv, print); printf("\nCount: %d, Capacity: %d--------------\n", vector_count(iv), vector_capacity(iv)); vector_sort(iv, compare); vector_foreach(iv, print); printf("\nCount: %d, Capacity: %d--------------\n", vector_count(iv), vector_capacity(iv)); printf("\nBsearch: %d, Lower: %d, Upper: %d\n", vector_bsearch(iv, a, compare), vector_lower(iv, a, compare), vector_upper(iv, a, compare)); vector_shuffle(iv); vector_foreach(iv, print); printf("\nCount: %d, Capacity: %d--------------\n", vector_count(iv), vector_capacity(iv)); destroy_vector(iv, TRUE); }
void* Allocator_Allocate(Allocator *allocator,size_t bytes) { if (bytes > MemoryPageSize) { fprintf(stderr, "ask for too much memory\n"); exit(1); } if (allocator->flag_using_lock_memory) { if (allocator->remaining_bytes < bytes) { if (allocator->current_block + 1 < vector_count(allocator->blocks)) { allocator->current_block ++; allocator->remaining_bytes = MemoryPageSize; } else { char *page_start; page_start=(char*)malloc(bytes*sizeof(char)); //implemented malloc just see afterwards vector_add(allocator->blocks,page_start); allocator->remaining_bytes = 0; return (void *) page_start; } } /*char* alloc_addr = vector_get(allocator->blocks,allocator->current_block)+ // some jhol maybe there (MemoryPageSize - allocator->remaining_bytes);*/ char* alloc_addr; //added for comipilng allocator->remaining_bytes -= bytes; return (void *) alloc_addr; } else { char *page_start; page_start=(char*)malloc(bytes*sizeof(char)); //tan::implemented malloc just see afterwards vector_add(allocator->blocks,page_start); return (void *) page_start; } }
void buf_dump(Buf *buffer, Vector<ch8> *string) { vector_clear(string); ch8 *i = buf_page_begin(buffer); for (; i < buffer->gap_start; ++i) { vector_push(string, *i); } if (buffer->gap_start == buffer->gap_end) { vector_push(string, (ch8)'|'); } else { for (; i < buffer->gap_end; ++i) { vector_push(string, (ch8)'#'); } } for (; i < buf_data_end(buffer); ++i) { vector_push(string, *i); } for (; i < buf_page_end(buffer); ++i) { vector_push(string, (ch8)'-'); } #ifdef HALE_DEBUG hale_assert(vector_count(string) == buf_capacity); #endif vector_push(string, (ch8)'\n'); }
void capture_packet_time_sorter(vector_t *vector, void *item) { struct timeval curts, prevts; int count = vector_count(vector); int i; // TODO Implement multiframe packets curts = packet_time(item); prevts = packet_time(vector_last(vector)); // Check if the item is already sorted if (timeval_is_older(curts, prevts)) { return; } for (i = count - 2 ; i >= 0; i--) { // Get previous packet prevts = packet_time(vector_item(vector, i)); // Check if the item is already in a sorted position if (timeval_is_older(curts, prevts)) { vector_insert(vector, item, i + 1); return; } } // Put this item at the begining of the vector vector_insert(vector, item, 0); }
/* VRRP handlers */ static void vrrp_sync_group_handler(vector_t *strvec) { list l; element e; vrrp_sgroup_t *sg; char* gname; if (vector_count(strvec) != 2) { log_message(LOG_INFO, "vrrp_sync_group must have a name - skipping"); skip_block(); return; } gname = vector_slot(strvec, 1); /* check group doesn't already exist */ if (!LIST_ISEMPTY(vrrp_data->vrrp_sync_group)) { l = vrrp_data->vrrp_sync_group; for (e = LIST_HEAD(l); e; ELEMENT_NEXT(e)) { sg = ELEMENT_DATA(e); if (!strcmp(gname,sg->gname)) { log_message(LOG_INFO, "vrrp sync group %s already defined", gname); skip_block(); return; } } } alloc_vrrp_sync_group(gname); }
void capture_close() { capture_info_t *capinfo; // Nothing to close if (vector_count(capture_cfg.sources) == 0) return; // Stop all captures vector_iter_t it = vector_iterator(capture_cfg.sources); while ((capinfo = vector_iterator_next(&it))) { //Close PCAP file if (capinfo->handle) { pcap_breakloop(capinfo->handle); pthread_join(capinfo->capture_t, NULL); pcap_close(capinfo->handle); } } // Close dump file if (capture_cfg.pd) { dump_close(capture_cfg.pd); } }
void test_Vector() { struct vector* alist=(struct vector*)malloc(sizeof(struct vector)); vector_init(alist); int i = 0; for (i = 0; i < 15; i++) { char* tmp = (char*)malloc(sizeof(char)* 512); memset(tmp, 0, sizeof(char)* 512); sprintf(tmp, "hehe%d", i); printf("%s\n", tmp); vector_add(alist, tmp); } int len = vector_count(alist); for (i = 0; i < len; i++) { char* tmp; vector_get(alist, i,&tmp); printf("%s\n",tmp); } vector_free(alist); }
static void vrrp_handler(vector_t *strvec) { list l; element e; vrrp_t *vrrp; char *iname; if (vector_count(strvec) != 2) { log_message(LOG_INFO, "vrrp_instance must have a name"); skip_block(); return; } iname = vector_slot(strvec,1); /* Make sure the vrrp instance doesn't already exist */ if (!LIST_ISEMPTY(vrrp_data->vrrp)) { l = vrrp_data->vrrp; for (e = LIST_HEAD(l); e; ELEMENT_NEXT(e)) { vrrp = ELEMENT_DATA(e); if (!strcmp(iname,vrrp->iname)) { log_message(LOG_INFO, "vrrp instance %s already defined", iname ); skip_block(); return; } } } alloc_vrrp(iname); }
int network_manager_unregister_peer_for_polling(struct network_manager* nm, struct network_peer* peer) { uintptr_t p = (uintptr_t)peer; int* psock = NULL; int sock; JLG(psock, nm->poll_socket_by_peer, p); if(psock == NULL) return -1; // doesn't exist sock = *psock; int rc; JLD(rc, nm->poll_socket_by_peer, p); FD_CLR(sock, &nm->poll_read_fds); FD_CLR(sock, &nm->poll_write_fds); FD_CLR(sock, &nm->poll_exception_fds); if(sock == nm->poll_max_fd) { size_t num_fds = vector_count(&nm->poll_fds); // Need to figure out what the previous max fd was, which means.. qsort(vector_data(&nm->poll_fds), (size_t)num_fds, sizeof(uintptr_t), compint); int old_fd = (int)vector_pop(&nm->poll_fds); assert(old_fd == sock); nm->poll_max_fd = 0; if(num_fds > 1) { nm->poll_max_fd = vector_get(&nm->poll_fds, num_fds - 2); } } return 0; }
void capture_packet_time_sorter(vector_t *vector, void *item) { capture_packet_t *prev, *cur; int count = vector_count(vector); int i; // Get current item cur = (capture_packet_t *) item; prev = vector_item(vector, count - 2); // Check if the item is already sorted if (prev && timeval_is_older(cur->header->ts, prev->header->ts)) { return; } for (i = count - 2 ; i >= 0; i--) { // Get previous packet prev = vector_item(vector, i); // Check if the item is already in a sorted position if (timeval_is_older(cur->header->ts, prev->header->ts)) { vector_insert(vector, item, i + 1); return; } } // Put this item at the begining of the vector vector_insert(vector, item, 0); }
void fixed_gap_arena_insert(FixedGapArena *arena, memi offset, const ch8 *data, memi size) { // TODO: In case we can take ownership of data, we can just wrap it with gap buffers and be done. // TODO: In case we cannot take ownership of data, but the data is persistent, we can wrap it with copy-on-write gap buffers. // TODO: Remove the checks. if (size == 0) { return; } hale_assert(offset <= arena->size); hale_assert_debug(vector_count(arena->buffers)); Buf *it0 = find_buf_GTE(arena, &offset, vector_begin(&arena->buffers), vector_end(&arena->buffers)); // Move these to tests. hale_assert_requirement(it0); // hale_assert(buf_length(it) != 0); hale_assert(buf_length(it0) >= offset); if (buf_available(it0) >= size) { buf_insert(it0, offset, data, size); } else { insert_non_crit_branch(arena, offset, data, size, it0); } arena->size += size; }
sip_msg_t * call_group_get_next_msg(sip_call_group_t *group, sip_msg_t *msg) { sip_msg_t *next = NULL; sip_msg_t *cand; vector_iter_t msgs; sip_call_t *call; int i; for (i = 0; i < vector_count(group->calls); i++) { call = vector_item(group->calls, i); msgs = vector_iterator(call->msgs); if (msg && call == msg_get_call(msg)) vector_iterator_set_current(&msgs, msg->index); if (group->sdp_only) vector_iterator_set_filter(&msgs, msg_has_sdp); cand = NULL; while ((cand = vector_iterator_next(&msgs))) { // candidate must be between msg and next if (msg_is_older(cand, msg) && (!next || !msg_is_older(cand, next))) { next = cand; break; } } } return sip_parse_msg(next); }
rtp_stream_t * call_group_get_next_stream(sip_call_group_t *group, rtp_stream_t *stream) { rtp_stream_t *next = NULL; rtp_stream_t *cand; sip_call_t *call; vector_iter_t streams; int i; for (i = 0; i < vector_count(group->calls); i++) { call = vector_item(group->calls, i); streams = vector_iterator(call->streams); while ( (cand = vector_iterator_next(&streams))) { if (!stream_get_count(cand)) continue; // candidate must be between msg and next if (stream_is_older(cand, stream) && (!next || stream_is_older(next, cand))) { next = cand; } } } return next; }
void director_release(director_handle_t handle) { /* TODO: some possible troubles with release code in case * a thread is still running. we could hold on to references * of the threads, or have a counter w/ a mutex. */ size_t count = 0; size_t i = 0; loop_t* p_loop = NULL; status_t status = NO_ERROR; if (NULL == handle) { return; } vector_count(handle->loops, &count); for (i = 0; i < count; i++) { status = vector_element_copy(handle->loops, i, (void**)&p_loop); if (NO_ERROR != status) { _loop_release(p_loop); } } frame_store_release(handle->frame_store); vector_release(handle->loops); _loop_release(handle->p_current_loop); pthread_mutex_destroy(&(handle->loops_mutex)); pthread_mutex_destroy(&(handle->frame_store_mutex)); motion_detector_release(handle->motion_detector); free(handle); }
static void call_fatal_cleanup(void) { int i; const int size = vector_count(&fatal_cleanup); for (i=0; i<size; ++i) { void (*func)() = vector_getvalue(&fatal_cleanup, void *, i); (*func)(); } }
char* xyzsh_job_title(int n) { if(n < vector_count(gJobs) && n >= 0) { sObject* job = vector_item(gJobs, n); return SJOB(job).mName; } else { return NULL; } }
static void delete_session(struct libwebsocket *wsi) { int count = vector_count(sessions); for (int i = 0; i < count; i++) { server_session_t *s = vector_get(sessions, i); if (s != NULL && s->wsi == wsi) { vector_delete(sessions, i); break; } } }
static void send_command(server_command_t command) { int count = vector_count(sessions); for (int i = 0; i < count; i++) { server_session_t *s = vector_get(sessions, i); if (s != NULL) { s->next_command = command; libwebsocket_callback_on_writable(context, s->wsi); } } }
void call_add_message(sip_call_t *call, sip_msg_t *msg) { // Set the message owner msg->call = call; // Put this msg at the end of the msg list msg->index = vector_append(call->msgs, msg); // Store message count call_set_attribute(call, SIP_ATTR_MSGCNT, "%d", vector_count(call->msgs)); }
void cstr_vector_data_free(vector*v) { int i; cstring *s; for (i = 0; i < vector_count(v); i++){ s = vector_get(v, i); cstr_free(s); } vector_data_free(v); }
char * capture_last_error() { capture_info_t *capinfo; if (vector_count(capture_cfg.sources) == 1) { capinfo = vector_first(capture_cfg.sources); return pcap_geterr(capinfo->handle); } return NULL; }
const char * capture_device() { capture_info_t *capinfo; if (vector_count(capture_cfg.sources) == 1) { capinfo = vector_first(capture_cfg.sources); return capinfo->device; } return NULL; }
static void update_peers(struct network_manager* nm) { // Perform the select first fd_set read_fds = nm->poll_read_fds, write_fds = nm->poll_write_fds, exception_fds = nm->poll_exception_fds; struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 0; if(select(nm->poll_max_fd + 1, &read_fds, &write_fds, &exception_fds, &timeout) < 0) { // weird error? perror("select"); return; } // Then loop over all peers passing in the select status struct network_peer** ppeer = NULL; Word_t index = 0; struct vector disconnected_peers; vector_init(&disconnected_peers); JLF(ppeer, nm->peer_list, index); while(ppeer != NULL) { int action_flags = 0; int* psock = NULL; uintptr_t p = (uintptr_t)*ppeer; JLG(psock, nm->poll_socket_by_peer, p); if(psock != NULL) { if(FD_ISSET(*psock, &read_fds)) action_flags |= NETWORK_PEER_ACTION_FLAGS_READ; if(FD_ISSET(*psock, &write_fds)) action_flags |= NETWORK_PEER_ACTION_FLAGS_WRITE; if(FD_ISSET(*psock, &exception_fds)) action_flags |= NETWORK_PEER_ACTION_FLAGS_EXCEPTION; } network_peer_update(*ppeer, action_flags); if(network_peer_disconnected(*ppeer) == 1) { vector_add(&disconnected_peers, (uintptr_t)*ppeer); } JLN(ppeer, nm->peer_list, index); } size_t num_disconnected = vector_count(&disconnected_peers); for(size_t i = 0; i < num_disconnected; i++) { struct network_peer* peer = (struct network_peer*)vector_get(&disconnected_peers, i); stop_peer(nm, peer); } vector_free(&disconnected_peers); }
pcap_dumper_t * dump_open(const char *dumpfile) { capture_info_t *capinfo; if (vector_count(capture_cfg.sources) == 1) { capinfo = vector_first(capture_cfg.sources); return pcap_dump_open(capinfo->handle, dumpfile); } return NULL; }
int main(int argc, char** argv) { if (argc < 2) { helper(); return PARAM_E; } int i; Vector *v = vector_create(2); if (v == NULL) { return MEMORY_E; } for (i = 1; i < argc; i++) { switch (argv[i][0]) { case 'f': printe(v, vector_find(v, atoi(argv[++i]))); break; case 'c': printf("%d\n", vector_count(v, atoi(argv[++i]))); break; case 'e': printe(v, atoi(argv[++i])); break; case 'p': if (!strcmp(argv[i], "print")) { printv(v); } break; case 's': vector_sort(v, atoi(argv[++i])); break; case '-': vector_remove(v, -atoi(argv[i])); break; default: vector_insert(v, atoi(argv[i]), atoi(argv[i+1])); i++; break; } } vector_destroy(v); return SUCCESS; }
void Allocator_Destructor(Allocator *allocator){ size_t i; if (allocator->flag_using_lock_memory) { for (i = 0; i < vector_count(allocator->blocks); i++) { vector_delete(allocator->blocks,i); } } else { /*for (i = 0; i < vector_size(allocator->blocks); i++) { vector_delete(allocator->blocks,i); //DELETE block[i] pass kela }*/ } }
int main () { int *x1=(int*)malloc(sizeof(int)); int *x2=(int*)malloc(sizeof(int)); *x1=8; *x2=9; vector vec=vector_init(3); vector_set(vec,x1); vector_set(vec,x2); printf("size:%d\n",vector_count(vec)); int * find=vector_lookup(vec,1); printf("value:%d\n",*find); return 0; }