static inline void set_next_node(node_t *current_node, byte_t byte, node_t *next_node) { Word_t *pnode; JLI(pnode, current_node->PJLarray_branches, (Word_t)byte); *pnode = (Word_t)next_node; }
void pq_scope_set(pq_scope *scope, pq_symbol sym, pq_value *val) { Word_t *pvalue; JLI(pvalue, scope->table, sym); if(pvalue != PJERR) { *pvalue = (Word_t) val; } }
void *jtableL_insert(jtableL *table, unsigned long key, void *data) { PWord_t PValue; JLI(PValue, table->t, key); *PValue = (Word_t)data; return data; }
int main(int argc, char **argv) { const glist *seg = NULL; u32 key = 0; Pvoid_t entries = NULL; uint dc = 0; uint ic = 0; const char *stats = NULL; dub_init(); stats = pparm_common_name("istats"); open_serializer(); while ((seg = pull_head(&key))){ uint i; u32 lang = lang_code(seg); do{ for (i = 0; i < seg->len; i++){ Word_t *e = NULL; Word_t idx = seg->lst[i]; struct istat_entry *ent = NULL; JLI(e, entries, idx); if (!*e){ *e = (Word_t)xmalloc( sizeof(struct istat_entry)); ent = (struct istat_entry*)*e; memset(ent, 0, sizeof(struct istat_entry)); ent->xid = idx; } ent = (struct istat_entry*)*e; ++ent->freq; if (!ent->lang_code) ent->lang_code = lang; } }while ((seg = pull_segment())); ++dc; } ic = write_istats(stats, entries); dub_msg("Number of documents: %u\n", dc); dub_msg("Number of ixemes: %u\n", ic); return 0; }
static inline Word_t *_pq_overload_variadic_function_pointer(pq_overload *overload, pq_function_metadata *metadata, pq_type *arguments_tuple_type) { Word_t *pvalue; JLI(pvalue, overload->variadic_function_table, metadata->argnum); if(pvalue != PJERR) { pvalue = (Word_t *)JudyLIns((PPvoid_t)pvalue, (Word_t)arguments_tuple_type, PJE0); } return pvalue; }
A_UNUSED static void * gc_alloc_annot(gc_t gc,struct s_cache **sc, unsigned count, unsigned nptrs, char *str) { void *ret = (gc_alloc)(gc,sc,count,nptrs); PWord_t pval; JLI(pval,mem_annotate,(Word_t)ret); *pval = (Word_t)str; return ret; }
int dogma_set_skill_level(dogma_context_t* ctx, dogma_typeid_t skillid, uint8_t level) { uint8_t* value; if(level > DOGMA_MAX_SKILL_LEVEL) level = DOGMA_MAX_SKILL_LEVEL; JLI(value, ctx->skill_levels, skillid); *value = level; return DOGMA_OK; }
/* returns 1 if was present, 0 otherwise */ static inline int insert_prefix(node_t *current_node, unsigned short prefix_key, const value_t value) { Word_t *pvalue; int rc; JLI(pvalue, current_node->PJLarray_prefixes, (Word_t)prefix_key); rc = (*pvalue) ? 1 : 0; *pvalue = (Word_t)value; return rc; }
static switch_status_t switch_l3_insert_into_lpm_trie( switch_handle_t vrf, switch_ip_addr_t *ip_addr, switch_l3_hash_t *hash_entry) { switch_lpm_trie_t *lpm_trie = NULL; void *temp = NULL; size_t key_width_bytes; char *prefix; uint32_t v4addr; if (ip_addr->type == SWITCH_API_IP_ADDR_V4) { JLG(temp, switch_vrf_v4_lpm_tries, vrf); key_width_bytes = 4; v4addr = htonl(ip_addr->ip.v4addr); prefix = (char *)(&v4addr); } else { JLG(temp, switch_vrf_v6_lpm_tries, vrf); key_width_bytes = 16; prefix = (char *)(ip_addr->ip.v6addr); } if (!temp) { lpm_trie = switch_lpm_trie_create(key_width_bytes, TRUE); if (!lpm_trie) { SWITCH_API_ERROR("%s:%d: No memory!", __FUNCTION__, __LINE__); return SWITCH_STATUS_NO_MEMORY; } if (ip_addr->type == SWITCH_API_IP_ADDR_V4) { JLI(temp, switch_vrf_v4_lpm_tries, vrf); } else { JLI(temp, switch_vrf_v6_lpm_tries, vrf); } *(unsigned long *)temp = (unsigned long)(lpm_trie); } lpm_trie = (switch_lpm_trie_t *)(*(unsigned long *)temp); switch_lpm_trie_insert( lpm_trie, prefix, ip_addr->prefix_len, (unsigned long)hash_entry); return SWITCH_STATUS_SUCCESS; }
static bool record_board(cube_t *cube) { void **hbitsp = NULL; bool retval = 0; JLI(hbitsp, seen, cube->low); J1S(retval, *hbitsp, cube->pos); return retval; }
static int pointless_export_set_seen(pointless_export_state_t* state, PyObject* py_object, uint32_t handle) { PWord_t value = 0; JLI(value, state->objects_used, (Word_t)py_object); if (value == 0) return 0; *value = (Word_t)handle; return 1; }
static dbus_bool_t add_watch(DBusWatch* watch, void *userdata) { struct dbus* dbus = (struct dbus*)userdata; Word_t index = (Word_t)dbus->next_watch_id; DBusWatch** pwatch; JLI(pwatch, dbus->watches, index); assert_pointer(pwatch); *pwatch = watch; dbus->next_watch_id += 1; return TRUE; }
int dogma_add_drone(dogma_context_t* ctx, dogma_typeid_t droneid, unsigned int quantity) { dogma_env_t** value1; dogma_drone_context_t** value2; dogma_drone_context_t* drone_ctx; dogma_env_t* drone_env; dogma_key_t index = DOGMA_SAFE_CHAR_INDEXES; int ret; if(quantity == 0) return DOGMA_OK; JLG(value2, ctx->drone_map, droneid); if(value2 != NULL) { /* Already have drones of the same type, just add the quantity */ drone_ctx = *value2; drone_ctx->quantity += quantity; return DOGMA_OK; } drone_ctx = malloc(sizeof(dogma_drone_context_t)); drone_env = malloc(sizeof(dogma_env_t)); /* Two calls to malloc * are necessary here, * since the env will be * freed in * dogma_free_env(). */ JLFE(ret, ctx->character->children, index); JLI(value1, ctx->character->children, index); *value1 = drone_env; DOGMA_INIT_ENV(drone_env, droneid, ctx->character, index, ctx); JLI(value2, ctx->drone_map, droneid); *value2 = drone_ctx; drone_ctx->drone = drone_env; drone_ctx->quantity = quantity; return dogma_set_env_state(ctx, drone_env, DOGMA_STATE_Active); }
void set_p(my_int j,my_int k,int v) { // using encoding: 10 -> 2, 01 -> 1, 00 -> 2 int Rc_int; JLI(pval,judy,j); if (v&1==1){ J1S(Rc_int,*pval,2*k);} else J1U(Rc_int,*pval,2*k); int v2=v>>1; if (v2&1==1){ J1S(Rc_int,*pval,2*k+1);} else J1U(Rc_int,*pval,2*k+1); }
static switch_status_t switch_l3_insert_into_vrf_list( switch_l3_hash_t *hash_entry) { switch_vrf_route_list_t *vrf_route_list = NULL; void *temp = NULL; switch_ip_addr_t ip_addr; switch_handle_t vrf_handle = 0; memset(&ip_addr, 0, sizeof(switch_ip_addr_t)); switch_l3_hash_key_decode(hash_entry, &vrf_handle, &ip_addr); if (ip_addr.type == SWITCH_API_IP_ADDR_V4) { JLG(temp, switch_vrf_v4_routes, vrf_handle); } else { JLG(temp, switch_vrf_v6_routes, vrf_handle); } if (!temp) { vrf_route_list = switch_malloc(sizeof(switch_vrf_route_list_t), 1); if (!vrf_route_list) { SWITCH_API_ERROR("%s:%d: No memory!", __FUNCTION__, __LINE__); return SWITCH_STATUS_NO_MEMORY; } tommy_list_init(&(vrf_route_list->routes)); vrf_route_list->num_entries = 0; if (ip_addr.type == SWITCH_API_IP_ADDR_V4) { JLI(temp, switch_vrf_v4_routes, vrf_handle); } else { JLI(temp, switch_vrf_v6_routes, vrf_handle); } *(unsigned long *)temp = (unsigned long)(vrf_route_list); } vrf_route_list = (switch_vrf_route_list_t *)(*(unsigned long *)temp); tommy_list_insert_tail( &(vrf_route_list->routes), &(hash_entry->vrf_route_node), hash_entry); vrf_route_list->num_entries++; return SWITCH_STATUS_SUCCESS; }
static int start_peer(struct network_manager* nm) { // Randomly pick an address to connect to struct network_address address; int have_address = 0; for(int i = 0; i < 50; i++) { if(database_get_random_peer_address(chaind_database(nm->chaind), &address) <= 0) { // Error, or no address to select break; } // Apply default port if sin_port == 0 if(address.sin_port == 0) address.sin_port = htons(NETWORK_DEFAULT_PORT); if(network_manager_get_peer_by_address(nm, &address) == NULL) { have_address = 1; break; } } if(!have_address) return -1; struct network_peer* peer = network_peer_create(nm); struct network_peer** ppeer = NULL; Word_t peer_id = nm->next_peer_id; JLI(ppeer, nm->peer_list, peer_id); *ppeer = peer; nm->next_peer_id += 1; Word_t* pindex; JHSI(pindex, nm->peer_by_address, (uint8_t*)&address, sizeof(struct network_address)); *pindex = peer_id; int res = network_peer_connect(peer, &address, blockchain_link_height(chaind_best_blockchain_link(nm->chaind))); if(res != 0) { int rc; JLD(rc, nm->peer_list, peer_id); JHSD(rc, nm->peer_by_address, (uint8_t*)&address, sizeof(struct network_address)); network_peer_destroy(peer); return res; } nm->num_peers += 1; return 0; }
static inline int dogma_add_env_generic(dogma_context_t* ctx, dogma_env_t* location, dogma_context_t* owner, dogma_typeid_t id, dogma_key_t* index, dogma_state_t state) { dogma_env_t* new_env = malloc(sizeof(dogma_env_t)); dogma_env_t** value; int result; JLFE(result, location->children, *index); JLI(value, location->children, *index); *value = new_env; DOGMA_INIT_ENV(new_env, id, location, *index, owner); DOGMA_ASSUME_OK(dogma_set_env_state(ctx, new_env, state)); return DOGMA_OK; }
static void insert (Pvoid_t* PJArray, Word_t Key, Word_t Value) { PWord_t PValue; JLI (PValue, *PJArray, Key); if (PValue == PJERR) { fprintf (stderr, "JHSI-Error: Out of memory...\n"); exit (EXIT_FAILURE); } *PValue = Value; ++_cnt_ins; }
static Pvoid_t read_cache(FILE *f) { Pvoid_t cache = NULL; u32 key = 0; u32 lang = 0; uint cnt = 0; while (fscanf(f, "%u %u\n", &key, &lang) == 2){ Word_t *val = NULL; JLI(val, cache, key); *val = lang; ++cnt; } dub_msg("Succesfully read %u language entries from the cache.", cnt); return cache; }
Pvoid_t reverse_ixicon() { Pvoid_t rixicon = NULL; uint i; for (i = 0; i < ixi_cnt; i++){ Word_t *ix = NULL; u32 xid = *(u32*)&ixi_body[ixi_idx[i + 1] - 4]; /* skip all entries containing a dot (i.e. site names) */ /* if (index(&ixi_body[ixi_idx[i]], '.')) continue; */ JLI(ix, rixicon, xid); *ix = (Word_t)&ixi_body[ixi_idx[i]]; } return rixicon; }
int network_manager_register_peer_for_polling(struct network_manager* nm, int sock, struct network_peer* peer) { uintptr_t p = (uintptr_t)peer; int* psock = NULL; JLG(psock, nm->poll_socket_by_peer, p); assert(psock == NULL); // duplicate insert JLI(psock, nm->poll_socket_by_peer, p); if(psock == NULL) { // memory failure return -1; } *psock = sock; FD_SET(sock, &nm->poll_read_fds); FD_SET(sock, &nm->poll_write_fds); FD_SET(sock, &nm->poll_exception_fds); vector_add(&nm->poll_fds, (uintptr_t)sock); if(sock > nm->poll_max_fd) nm->poll_max_fd = sock; return 0; }
l3obj* string_set(l3obj* obj, long i, const qqchar& key) { assert(obj->_type == t_str || obj->_type == t_lit); assert(obj->_mytag); char* keydup = obj->_mytag->strdup_qq(key); LIVEO(obj); char** ps = 0; PWord_t PValue = 0; JLI(PValue, obj->_judyL, i); ps = (char**)(PValue); if (*ps != 0) { // replace the old first void* found = obj->_mytag->atom_remove(*ps); assert(found); ::free(found); } *ps = keydup; return obj; }
int dogma_init_context(dogma_context_t** ctx) { dogma_context_t* new_ctx = malloc(sizeof(dogma_context_t)); dogma_env_t** value; dogma_key_t index = 0; const dogma_type_t** type; new_ctx->gang = malloc(sizeof(dogma_env_t)); new_ctx->character = malloc(sizeof(dogma_env_t)); new_ctx->ship = malloc(sizeof(dogma_env_t)); new_ctx->area = NULL; new_ctx->fleet = NULL; DOGMA_INIT_ENV(new_ctx->gang, 0, NULL, 0, new_ctx); DOGMA_INIT_ENV(new_ctx->character, 0, NULL, 0, new_ctx); DOGMA_INIT_ENV(new_ctx->ship, 0, new_ctx->character, 0, new_ctx); JLI(value, new_ctx->character->children, 0); *value = new_ctx->ship; new_ctx->default_skill_level = DOGMA_MAX_SKILL_LEVEL; new_ctx->skill_levels = (dogma_array_t)NULL; new_ctx->drone_map = (dogma_array_t)NULL; *ctx = new_ctx; /* Inject all skills. This is somewhat costly, maybe there is a * way to do it lazily? */ JLF(type, types_by_id, index); while(type != NULL) { if((*type)->categoryid == CAT_Skill) { dogma_inject_skill(*ctx, (*type)->id); } JLN(type, types_by_id, index); } return DOGMA_OK; }
switch_status_t switch_int_transit_enable(switch_device_t device, int32_t switch_id, int32_t enable) { switch_status_t status = SWITCH_STATUS_SUCCESS; #ifdef P4_INT_TRANSIT_ENABLE p4_pd_entry_hdl_t entry_hdl; if (enable) { void *temp = NULL; // check if already created if (switch_int_entry_hdl_get(device, &entry_hdl)) { return SWITCH_STATUS_ITEM_ALREADY_EXISTS; } // use the lowest priority entry for transit status = switch_pd_int_transit_enable(device, switch_id, 1, &entry_hdl); if (status == SWITCH_STATUS_SUCCESS) { JLI(temp, switch_int_proto_entry_handles, device); if (!temp) { return SWITCH_STATUS_NO_MEMORY; } *(p4_pd_entry_hdl_t *)temp = entry_hdl; } } else { // disable int rc; if (!switch_int_entry_hdl_get(device, &entry_hdl)) { return SWITCH_STATUS_ITEM_NOT_FOUND; } status = switch_pd_int_transit_disable(device, entry_hdl); if (status == SWITCH_STATUS_SUCCESS) { JLD(rc, switch_int_proto_entry_handles, device); } } #else (void)device, (void)switch_id, (void)enable; #endif return status; }
int main(int argc, char **argv) { const char *ixicon_file = NULL; const char *istats_file = NULL; const char *qexp_file = NULL; Pvoid_t rixicon = NULL; Pvoid_t lemmas = NULL; Pvoid_t qexp = NULL; uint i; uint nl = 0; uint ul = 0; u32 xid = 0; u32 freq_xid = 0; uint not_qexp = 0; dub_init(); ixicon_file = pparm_common_name("ixi"); istats_file = pparm_common_name("istats"); qexp_file = pparm_common_name("qexp"); PPARM_INT(max_len, MAX_LEMMA_LEN); if (getenv("LOCALE")){ if(!setlocale(LC_ALL, getenv("LOCALE"))) dub_sysdie("Couldn't set locale %s", getenv("LOCALE")); else dub_msg("Locale set to %s", getenv("LOCALE")); } /* qexp_file might not exist; no problem */ not_qexp = load_qexp(qexp_file, 1); load_ixicon(ixicon_file); rixicon = reverse_ixicon(); load_istats(istats_file); snowball_init(); for (i = 0; i < istats_len; i++){ Word_t idx = (Word_t)istats[i].xid; Word_t *val = NULL; const char *lemma = NULL; const char *token = NULL; u32 lid = 0; if (idx > XID_TOKEN_L) continue; JLG(val, rixicon, idx); if (!val) dub_die("Ixicon doesn't contain xid %u", istats[i].xid); token = (const char*)*val; if (istats[i].lang_code < XID_META_LANG_F || istats[i].lang_code > XID_META_LANG_L) continue; if (!lemmatizable(token)){ dub_dbg("Not lemmatizable: %s", token); continue; } lemma = snowball_lemmatize(token, istats[i].lang_code); /* no lemma found (unknown language etc.) */ if (!lemma) continue; JSLI(val, lemmas, lemma); if (!*val){ if (strlen(lemma) > longest_lemma) longest_lemma = strlen(lemma); glist *lst = xmalloc(sizeof(glist) + 4); lst->len = 1; /* unseen lemma */ /* if a token is frequent, its lemma is also */ if (idx < XID_TOKEN_FREQUENT_L){ lid = XID_META_FREQUENT_F + freq_xid++; if (freq_xid >= XID_META_FREQUENT_L) /* freq_ixicon has failed somehow */ dub_die("Too many frequent ixemes"); }else lid = XID_META_FREQUENT_L + xid++; if (lid > XID_META_LEMMA_L) dub_die("Lemma ID range exhausted."); lst->lst[0] = lid; *val = (Word_t)lst; ++ul; } lid = *val; JLI(val, qexp, idx); *val = lid; ++nl; } if (not_qexp){ dub_msg("Qexp file %s doesn't exist. " "Creating qexp from the scratch.", qexp_file); }else{ dub_msg("Qexp file %s exists. Lemmas will be merged to it.", qexp_file); qexp = qexp_merge(qexp); close_qexp(); } create_qexp(qexp_file, qexp); if (getenv("OUTPUT_LEMMAS")) output_lemmas(lemmas); dub_msg("%u / %u ixemes were lemmatized", nl, istats_len); dub_msg("%u different lemmas were found", ul); return 0; }
int main() { Pvoid_t JArray[HASHSIZE] = { NULL }; // Declare static hash table int * PValue; FILE *arq; long int count, i, j, k; float *x,*y,*z; float a,b,c; float d; int n_uniq, sum_uniq; int sum; float *f_dist; int *i_dist; //entrada de dados arq=fopen("cells","r"); count=0; while(fscanf(arq,"%f %f %f", &a, &b, &c)!=EOF) count++; // alocando memória x=(float *)memalign (16, count*sizeof(float)); y=(float *)memalign (16, count*sizeof(float)); z=(float *)memalign (16, count*sizeof(float)); rewind(arq); for(i=0;i<count;i++) fscanf(arq,"%f %f %f", &x[i], &y[i], &z[i]); fclose(arq); //fim entrada de dados for(i = 0; i < HASHSIZE; i++) JArray[i] = NULL; for(i=0;i<count;i++) { for(j=i+1;j<count;j++) { a=x[i]-x[j]; b=y[i]-y[j]; c=z[i]-z[j]; d=sqrt(a*a+b*b+c*c); i_dist = (int*) &d; JLI(PValue, JArray[(int)d % HASHSIZE], *i_dist); *PValue += 1; } } n_uniq = 0; sum_uniq = 0; for(j=0;j<HASHSIZE;j++) { if (JArray[j] == NULL) continue; i = 0; JLF(PValue, JArray[j], i); while(PValue != NULL){ n_uniq++; f_dist = (float*) &i; printf("%.14f \t %i\n", *f_dist, *PValue ); sum_uniq=sum_uniq+*PValue; JLN(PValue, JArray[j],i); } } printf("n_uniq %d\n", n_uniq); printf("sum_uniq %d\n", sum_uniq); free(x); free(y); free(z); return 0; }
switch_status_t switch_packet_hostif_create(switch_device_t device, switch_hostif_info_t *hostif_info) { int intf_fd = 0; struct ifreq ifr; int sock_flags = 0; char *intf_name = NULL; void *temp = NULL; switch_api_capability_t api_switch_info; switch_mac_addr_t mac; switch_api_capability_get(device, &api_switch_info); if ((intf_fd = open("/dev/net/tun", O_RDWR)) < 0) { return SWITCH_STATUS_FAILURE; } memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TAP | IFF_NO_PI; intf_name = hostif_info->hostif.intf_name; strncpy(ifr.ifr_name, intf_name, IFNAMSIZ); if ((ioctl(intf_fd, TUNSETIFF, (void *)&ifr)) < 0) { perror("tunsetiff failed"); close(intf_fd); return SWITCH_STATUS_FAILURE; } // set connection to be non-blocking sock_flags = fcntl(intf_fd, F_GETFL, 0); if ((fcntl(intf_fd, F_SETFL, sock_flags | O_NONBLOCK)) < 0) { perror("f_setfl failed"); close(intf_fd); return SWITCH_STATUS_FAILURE; } memset(&mac, 0, sizeof(switch_mac_addr_t)); if (memcmp(&api_switch_info.switch_mac, &mac, ETH_LEN) != 0) { // set the mac address memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, intf_name, IFNAMSIZ); ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; memcpy(ifr.ifr_addr.sa_data, &api_switch_info.switch_mac, ETH_LEN); if ((ioctl(intf_fd, SIOCSIFHWADDR, (void *)&ifr)) < 0) { perror("ioctl failed"); close(intf_fd); return SWITCH_STATUS_FAILURE; } } // bring the interface up memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, intf_name, IFNAMSIZ); if ((ioctl(cpu_sock_fd, SIOCGIFFLAGS, (void *)&ifr)) < 0) { perror("ioctl get failed"); // close(intf_fd); // return SWITCH_STATUS_FAILURE; } else { ifr.ifr_flags |= IFF_UP; if ((ioctl(cpu_sock_fd, SIOCSIFFLAGS, (void *)&ifr)) < 0) { perror("ioctl set failed"); // close(intf_fd); // return SWITCH_STATUS_FAILURE; } } hostif_info->intf_fd = intf_fd; JLG(temp, switch_intf_fd_array, intf_fd); if (!temp) { JLI(temp, switch_intf_fd_array, intf_fd); *(unsigned long *)temp = (unsigned long)(hostif_info); } switch_packet_write_to_pipe(); return SWITCH_STATUS_SUCCESS; }
int main(int argc, char *argv[]) { if (argc != 8) { fprintf(stderr, "Usage: ./merge_N [out_dir_name 1] [item_#_file 2] [length_#_file 3] [count_dist_file 4] [number_of_temp_prefix 5] [from_temp_n] [to_temp_n]\n"); // /root/cx_src/src/merge_N /tmp/data /tmp/result/ino.txt /tmp/result/lno.txt /tmp/result/cdo.txt 3 0 1 return -1; } if ((itf = fopen(argv[2], "a")) == NULL) { fprintf(stderr, "Failed to open file \"%s\" for writing item numbers\n", argv[2]); return -1; } // fixing each temp file fprintf(stdout, "Start fixing temp files\n"); for (i = atoi(argv[6]); i <= atoi(argv[7]); ++i) { //for (i = 0; i < TEMP_N; ++i) { // fix the temp file i int temp_prefix_num = 0;// num of concurrent threads while (temp_prefix_num < atoi(argv[5])){ sprintf(buffer, "%s/%s%d-%d.txt", argv[1], TEMP_PREFIX, i, temp_prefix_num); fprintf(stdout, "\rWorking on temp file: \"%s\" \n", buffer); //fflush(stdout); if ((tsf[i] = fopen(buffer, "r")) == NULL) { fprintf(stderr, "Failed to open file \"%s\" for reading temp strings\n", buffer); //continue; //break; return -1; } while (fscanf(tsf[i], "%"PRId64"\t", &itemn) != EOF) { fgets(Index, BUFFER_SIZE, tsf[i]); for (Len = strlen(Index) - 1; Index[Len] == '\n' || Index[Len] == '\r'; Len--) Index[Len] = 0; ++Len; JSLI(PValNgramS, PJSLNgram, (uint8_t *)Index); if (PValNgramS == PJERR) { fprintf(stderr, "Malloc failed for \"PJSLNgram\"\n"); //return -1; } (*PValNgramS) += itemn; JLI(PValTotC, PJLTotCount, Len); if (PValTotC == PJERR) { fprintf(stderr, "Malloc failed for \"PJLTotCount\"\n"); //return -1; } *PValTotC += itemn; if (*PValNgramS == itemn) { JLI(PValNgramC, PJLNgramCount, Len); if (PValNgramC == PJERR) { fprintf(stderr, "Malloc failed for \"PJLNgramCount\"\n"); //return -1; } ++*PValNgramC; } } sprintf(buffer, "rm %s/%s%d-%d.txt", argv[1], TEMP_PREFIX, i, temp_prefix_num++); if (system(buffer) == -1) { fprintf(stderr, "Failed to execute command: \"%s\"\n", buffer); //return -1; } fclose(tsf[i]); } // write the final temp file sprintf(buffer, "%s/%s%d.txt", argv[1], TEMP_PREFIX, i); if ((tsf[i] = fopen(buffer, "w")) == NULL) { fprintf(stderr, "Failed to open file \"%s\" for writing temp strings\n", buffer); return -1; } Index[0] = '\0'; JSLF(PValNgramS, PJSLNgram, (uint8_t *)Index); while (PValNgramS != NULL) { fprintf(tsf[i], "%lu\t%s\n", *PValNgramS, Index); Count = *PValNgramS; JLI(PValCountC, PJLCountCount, Count); if (PValCountC == PJERR) { fprintf(stderr, "Malloc failed for \"PJLCountCount\"\n"); return -1; } ++*PValCountC; JSLN(PValNgramS, PJSLNgram, (uint8_t *)Index); } JSLFA(Bytes, PJSLNgram); fflush(tsf[i]); fclose(tsf[i]); fprintf(itf, "Temp file \"%s/%s%d\" uses %lu Bytes of memory\n", argv[1], TEMP_PREFIX, i, Bytes); fflush(itf); } fclose(itf); if ((lef = fopen(argv[3], "a")) == NULL) { fprintf(stderr, "Failed to open file \"%s\" for writing length number\n", argv[3]); return -1; } Total = NgramN = 0; JLF(PValTotC, PJLTotCount, Total); JLF(PValNgramC, PJLNgramCount, NgramN); while (PValTotC != NULL) { fprintf(lef, "%lu\t%lu\t%lu\n", Total, *PValNgramC, *PValTotC); JLN(PValTotC, PJLTotCount, Total); JLN(PValNgramC, PJLNgramCount, NgramN); } JLFA(Bytes, PJLTotCount); JLFA(Bytes, PJLNgramCount); fflush(lef); fclose(lef); if ((cdf = fopen(argv[4], "a")) == NULL) { fprintf(stderr, "Failed to open file \"%s\" for writing count distribuction\n", argv[4]); return -1; } Count = 0; JLF(PValCountC, PJLCountCount, Count); while (PValCountC != NULL) { fprintf(cdf, "%lu\t%lu\n", Count, *PValCountC); JLN(PValCountC, PJLCountCount, Count); } JLFA(Bytes, PJLCountCount); fflush(cdf); fclose(cdf); return 0; }
void network_manager_handle_inv(struct network_manager* nm, struct network_peer* peer, struct inv const* inv) { // Determine if we know about this item and if so, ignore it bytes_to_hexstring(inv->hash, INV_HASH_SIZE, s, 1); printf("got inv %s %s", inv->type == INV_TYPE_TX ? "tx" : "block", s); // Check if it's in mempool struct memory_pool* memory_pool = chaind_memory_pool(nm->chaind); if(memory_pool_has_inv(memory_pool, inv)) { printf(" (mempool)\n"); return; } // Check the list of invs we care about. We include the inv->type field // in the index in case a block and tx have the same hash, despite it // being astronomically unlikely. Word_t* pindex = NULL; JHSG(pindex, nm->wanted_invs_by_inv, (uint8_t*)inv, sizeof(struct inv)); if(pindex != NULL) { printf(" (invpool)\n"); return; } // Check if it's in the database struct database* database = chaind_database(nm->chaind); if(database_has_inv(database, inv) != 0) { printf(" (db)\n"); return; } // We don't know this inv, so we take note in the appropriate list struct inv* ninv = (struct inv*)malloc(sizeof(struct inv)); memcpy(ninv, inv, sizeof(struct inv)); // Add to the list of items we need to get Word_t index = 0; struct inv** pinv = NULL; switch(inv->type) { case INV_TYPE_BLOCK: index = nm->head_block_inv_id; JLI(pinv, nm->block_inv_list, index); assert(pinv != NULL); *pinv = ninv; nm->head_block_inv_id += 1; break; case INV_TYPE_TX: index = nm->head_tx_inv_id; JLI(pinv, nm->tx_inv_list, index); assert(pinv != NULL); *pinv = ninv; nm->head_tx_inv_id += 1; break; case INV_TYPE_ERROR: // TODO break; } if(inv->type != INV_TYPE_ERROR) { JHSI(pindex, nm->wanted_invs_by_inv, (uint8_t*)inv, sizeof(struct inv)); assert(pindex != NULL); *pindex = index; } printf("\n"); }
static void remove_from_wanted_invs(struct network_manager* nm, struct inv const* invs, size_t num_invs) { Word_t swap_index; struct inv** pinv1; struct inv** pinv2; struct inv* inv2; int rc; Word_t* pindex1; Word_t* pindex2; for(size_t i = 0; i < num_invs; i++) { struct inv const* inv = &invs[i]; JHSG(pindex1, nm->wanted_invs_by_inv, (uint8_t*)inv, sizeof(struct inv)); assert(pindex1 != NULL); Word_t* tail = NULL; Word_t* head = NULL; void** list = NULL; switch(inv->type) { case INV_TYPE_BLOCK: head = &nm->head_block_inv_id; tail = &nm->tail_block_inv_id; list = &nm->block_inv_list; break; case INV_TYPE_TX: head = &nm->head_tx_inv_id; tail = &nm->tail_tx_inv_id; list = &nm->tx_inv_list; break; case INV_TYPE_ERROR: assert(0); break; } assert(*head > *tail); swap_index = *head - 1; if(*pindex1 == *tail) { JLG(pinv1, *list, *pindex1); assert(memcmp(*pinv1, inv, sizeof(struct inv)) == 0); free(*pinv1); JLD(rc, *list, *pindex1); (*tail) += 1; } else if(*pindex1 < swap_index) { JLG(pinv1, *list, *pindex1); assert(pinv1 != NULL); JLG(pinv2, *list, swap_index); assert(pinv2 != NULL); assert(memcmp(*pinv1, inv, sizeof(struct inv)) == 0); assert(memcmp(*pinv1, *pinv2, sizeof(struct inv)) != 0); // JLD will kill the pinv2 pointer, so save the inv inv2 = *pinv2; free(*pinv1); JLD(rc, *list, swap_index); // JLD can reorder the array.. JLI(pinv1, *list, *pindex1); *pinv1 = inv2; // Remove from wanted array JHSG(pindex2, nm->wanted_invs_by_inv, (uint8_t*)inv2, sizeof(struct inv)); *pindex2 = *pindex1; (*head) -= 1; } else { assert(*pindex1 == swap_index); JLG(pinv2, *list, swap_index); assert(memcmp(*pinv2, inv, sizeof(struct inv)) == 0); free(*pinv2); JLD(rc, *list, swap_index); (*head) -= 1; } JHSD(rc, nm->wanted_invs_by_inv, (uint8_t*)inv, sizeof(struct inv)); } }