void mx_init_daemon() { struct linger ling = {0, 0}; struct sockaddr_in addr; int flags = 1; mx_daemon->log_fd = fopen(mx_daemon->log_file, "a+"); if (!mx_daemon->log_file) { fprintf(stderr, "[failed] failed to open log file\n"); exit(-1); } mx_daemon->fd = socket(AF_INET, SOCK_STREAM, 0); if (mx_daemon->fd == -1) { mx_write_log(mx_log_error, "Unable create listening server socket"); exit(-1); } if (mx_set_nonblocking(mx_daemon->fd) == -1) { mx_write_log(mx_log_error, "Unable set socket to non-blocking"); exit(-1); } setsockopt(mx_daemon->fd, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags)); setsockopt(mx_daemon->fd, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof(flags)); setsockopt(mx_daemon->fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling)); #if !defined(TCP_NOPUSH) setsockopt(mx_daemon->fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags)); #endif addr.sin_family = AF_INET; addr.sin_port = htons(mx_daemon->port); addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(mx_daemon->fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) { mx_write_log(mx_log_error, "Unable bind socket"); close(mx_daemon->fd); exit(-1); } if (listen(mx_daemon->fd, 1024) == -1) { mx_write_log(mx_log_error, "Unable listen socket"); close(mx_daemon->fd); exit(-1); } mx_daemon->event = aeCreateEventLoop(); if (!mx_daemon->event) { mx_write_log(mx_log_error, "Unable create EventLoop"); exit(-1); } mx_daemon->table = hash_alloc(32); if (!mx_daemon->table) { mx_write_log(mx_log_error, "Unable create HashTable"); exit(-1); } mx_daemon->delay_queue = mx_queue_create("__delay__", sizeof("__delay__") - 1); if (!mx_daemon->table) { mx_write_log(mx_log_error, "Unable create delay queue"); exit(-1); } mx_daemon->recycle = mx_recycle_create(); if (!mx_daemon->recycle) { mx_write_log(mx_log_error, "Unable create recycle"); exit(-1); } if (aeCreateFileEvent(mx_daemon->event, mx_daemon->fd, AE_READABLE, mx_accept_connection, NULL) == -1) { mx_write_log(mx_log_error, "Unable create accpet file event"); exit(-1); } aeCreateTimeEvent(mx_daemon->event, 1, mx_core_timer, NULL, NULL); time(&mx_current_time); return; }
void config_schema_item_set_required_children_on_value(config_schema_item_type * item , const char * value , stringlist_type * child_list) { if (item->required_children_value == NULL) item->required_children_value = hash_alloc(); hash_insert_hash_owned_ref( item->required_children_value , value , stringlist_alloc_deep_copy(child_list) , stringlist_free__); }
static int module_init(void) { uint32_t x, bsize = ALLOC_DEFAULT_BSIZE; struct pl opt; int err = 0; restund_stun_register_handler(&stun); restund_cmd_subscribe(&cmd_turn); restund_cmd_subscribe(&cmd_turnstats); /* turn_external_addr */ if (!conf_get(restund_conf(), "turn_relay_addr", &opt)) err = sa_set(&turnd.rel_addr, &opt, 0); else sa_init(&turnd.rel_addr, AF_UNSPEC); if (err) { restund_error("turn: bad turn_relay_addr: '%r'\n", &opt); goto out; } /* turn_external_addr6 */ if (!conf_get(restund_conf(), "turn_relay_addr6", &opt)) err = sa_set(&turnd.rel_addr6, &opt, 0); else sa_init(&turnd.rel_addr6, AF_UNSPEC); if (err) { restund_error("turn: bad turn_relay_addr6: '%r'\n", &opt); goto out; } if (!sa_isset(&turnd.rel_addr, SA_ADDR) && !sa_isset(&turnd.rel_addr6, SA_ADDR)) { restund_error("turn: no relay address configured\n"); err = EINVAL; goto out; } /* turn_max_lifetime, turn_max_allocations, udp_sockbuf_size */ turnd.lifetime_max = TURN_DEFAULT_LIFETIME; conf_get_u32(restund_conf(), "turn_max_lifetime", &turnd.lifetime_max); conf_get_u32(restund_conf(), "turn_max_allocations", &bsize); conf_get_u32(restund_conf(), "udp_sockbuf_size", &turnd.udp_sockbuf_size); for (x=2; (uint32_t)1<<x<bsize; x++); bsize = 1<<x; err = hash_alloc(&turnd.ht_alloc, bsize); if (err) { restund_error("turnd hash alloc error: %m\n", err); goto out; } restund_debug("turn: lifetime=%u ext=%j ext6=%j bsz=%u\n", turnd.lifetime_max, &turnd.rel_addr, &turnd.rel_addr6, bsize); out: return err; }
ext_joblist_type * ext_joblist_alloc( ) { ext_joblist_type * joblist = util_malloc( sizeof * joblist ); joblist->jobs = hash_alloc(); return joblist; }
ranking_table_type * ranking_table_alloc( int ens_size ) { ranking_table_type * table = util_malloc( sizeof * table ); table->ranking_table = hash_alloc(); return table; }
struct vsf_client_launch vsf_standalone_main(void) { struct vsf_sysutil_sockaddr* p_sockaddr = 0; struct vsf_sysutil_ipv4addr listen_ipaddr; int listen_sock = vsf_sysutil_get_ipv4_sock(); int retval; s_p_ip_count_hash = hash_alloc(256, sizeof(struct vsf_sysutil_ipv4addr), sizeof(unsigned int), hash_ip); s_p_pid_ip_hash = hash_alloc(256, sizeof(int), sizeof(struct vsf_sysutil_ipv4addr), hash_pid); if (tunable_setproctitle_enable) { vsf_sysutil_setproctitle("LISTENER"); } vsf_sysutil_install_sighandler(kVSFSysUtilSigCHLD, handle_sigchld, 0); vsf_sysutil_install_async_sighandler(kVSFSysUtilSigHUP, handle_sighup); vsf_sysutil_activate_reuseaddr(listen_sock); vsf_sysutil_sockaddr_alloc_ipv4(&p_sockaddr); vsf_sysutil_sockaddr_set_port( p_sockaddr, vsf_sysutil_ipv4port_from_int(tunable_listen_port)); if (!tunable_listen_address || vsf_sysutil_inet_aton(tunable_listen_address, &listen_ipaddr) == 0) { listen_ipaddr = vsf_sysutil_sockaddr_get_any(); } vsf_sysutil_sockaddr_set_ipaddr(p_sockaddr, listen_ipaddr); retval = vsf_sysutil_bind(listen_sock, p_sockaddr); vsf_sysutil_free(p_sockaddr); if (vsf_sysutil_retval_is_error(retval)) { die("could not bind listening socket"); } vsf_sysutil_listen(listen_sock, VSFTP_LISTEN_BACKLOG); while (1) { struct vsf_client_launch child_info; static struct vsf_sysutil_sockaddr* p_accept_addr; int new_child; struct vsf_sysutil_ipv4addr ip_addr; /* NOTE - wake up every 10 seconds to make sure we notice child exit * in a timely manner (the sync signal framework race) */ int new_client_sock = vsf_sysutil_accept_timeout( listen_sock, &p_accept_addr, 10); if (s_reload_needed) { s_reload_needed = 0; do_reload(); } if (vsf_sysutil_retval_is_error(new_client_sock)) { continue; } ip_addr = vsf_sysutil_sockaddr_get_ipaddr(p_accept_addr); ++s_children; child_info.num_children = s_children; child_info.num_this_ip = handle_ip_count(&ip_addr); new_child = vsf_sysutil_fork_failok(); if (new_child != 0) { /* Parent context */ vsf_sysutil_close(new_client_sock); if (new_child > 0) { hash_add_entry(s_p_pid_ip_hash, (void*)&new_child, (void*)&ip_addr); } else { /* fork() failed, clear up! */ --s_children; drop_ip_count(&ip_addr); } /* Fall through to while() loop and accept() again */ } else { /* Child context */ vsf_sysutil_close(listen_sock); prepare_child(new_client_sock); /* By returning here we "launch" the child process with the same * contract as xinetd would provide. */ return child_info; } } }
void output_run_line( const output_type * output , ensemble_type * ensemble) { const int data_columns = vector_get_size( output->keys ); const int data_rows = time_t_vector_size( ensemble->interp_time ); double ** data; int row_nr, column_nr; data = util_calloc( data_rows , sizeof * data ); /* time-direction, i.e. the row index is the first index and the column number (i.e. the different keys) is the second index. */ for (row_nr=0; row_nr < data_rows; row_nr++) data[row_nr] = util_calloc( data_columns , sizeof * data[row_nr] ); printf("Creating output file: %s \n",output->file ); /* Go through all the cases and check that they have this key; exit if missing. Could also ignore the missing keys and just continue; and even defer the checking to the inner loop. */ for (column_nr = 0; column_nr < vector_get_size( output->keys ); column_nr++) { const quant_key_type * qkey = vector_iget( output->keys , column_nr ); { bool OK = true; for (int iens = 0; iens < vector_get_size( ensemble->data ); iens++) { const sum_case_type * sum_case = vector_iget_const( ensemble->data , iens ); if (!ecl_sum_has_general_var(sum_case->ecl_sum , qkey->sum_key)) { OK = false; fprintf(stderr,"** Sorry: the case:%s does not have the summary key:%s \n", ecl_sum_get_case( sum_case->ecl_sum ), qkey->sum_key); } } if (!OK) util_exit("Exiting due to missing summary vector(s).\n"); } } /* The main loop - outer loop is running over time. */ { /** In the quite typical case that we are asking for several quantiles of the quantity, i.e. WWCT:OP_1:0.10 WWCT:OP_1:0.50 WWCT:OP_1:0.90 the interp_data_cache construction will ensure that the underlying ecl_sum object is only queried once; and also the sorting will be performed once. */ hash_type * interp_data_cache = hash_alloc(); for (row_nr = 0; row_nr < data_rows; row_nr++) { time_t interp_time = time_t_vector_iget( ensemble->interp_time , row_nr); for (column_nr = 0; column_nr < vector_get_size( output->keys ); column_nr++) { const quant_key_type * qkey = vector_iget( output->keys , column_nr ); double_vector_type * interp_data; /* Check if we have the vector in the cache table - if not create it. */ if (!hash_has_key( interp_data_cache , qkey->sum_key)) { interp_data = double_vector_alloc(0 , 0); hash_insert_hash_owned_ref( interp_data_cache , qkey->sum_key , interp_data , double_vector_free__); } interp_data = hash_get( interp_data_cache , qkey->sum_key ); /* Check if the vector has data - if not initialize it. */ if (double_vector_size( interp_data ) == 0) { for (int iens = 0; iens < vector_get_size( ensemble->data ); iens++) { const sum_case_type * sum_case = vector_iget_const( ensemble->data , iens ); if ((interp_time >= sum_case->start_time) && (interp_time <= sum_case->end_time)) /* We allow the different simulations to have differing length */ double_vector_append( interp_data , ecl_sum_get_general_var_from_sim_time( sum_case->ecl_sum , interp_time , qkey->sum_key)) ; double_vector_sort( interp_data ); } } data[row_nr][column_nr] = statistics_empirical_quantile__( interp_data , qkey->quantile ); } hash_apply( interp_data_cache , double_vector_reset__ ); } hash_free( interp_data_cache ); } output_save( output , ensemble , (const double **) data); for (row_nr=0; row_nr < data_rows; row_nr++) free( data[row_nr] ); free( data ); }
/** * Decode a SIP message * * @param msgp Pointer to allocated SIP Message * @param mb Buffer containing SIP Message * * @return 0 if success, otherwise errorcode */ int sip_msg_decode(struct sip_msg **msgp, struct mbuf *mb) { struct pl x, y, z, e, name; const char *p, *v, *cv; struct sip_msg *msg; bool comsep, quote; enum sip_hdrid id = SIP_HDR_NONE; uint32_t ws, lf; size_t l; int err; if (!msgp || !mb) return EINVAL; p = (const char *)mbuf_buf(mb); l = mbuf_get_left(mb); if (re_regex(p, l, "[^ \t\r\n]+ [^ \t\r\n]+ [^\r\n]*[\r]*[\n]1", &x, &y, &z, NULL, &e) || x.p != (char *)mbuf_buf(mb)) return (l > STARTLINE_MAX) ? EBADMSG : ENODATA; msg = mem_zalloc(sizeof(*msg), destructor); if (!msg) return ENOMEM; err = hash_alloc(&msg->hdrht, HDR_HASH_SIZE); if (err) goto out; msg->tag = rand_u64(); msg->mb = mem_ref(mb); msg->req = (0 == pl_strcmp(&z, "SIP/2.0")); if (msg->req) { msg->met = x; msg->ruri = y; msg->ver = z; if (uri_decode(&msg->uri, &y)) { err = EBADMSG; goto out; } } else { msg->ver = x; msg->scode = pl_u32(&y); msg->reason = z; if (!msg->scode) { err = EBADMSG; goto out; } } l -= e.p + e.l - p; p = e.p + e.l; name.p = v = cv = NULL; name.l = ws = lf = 0; comsep = false; quote = false; for (; l > 0; p++, l--) { switch (*p) { case ' ': case '\t': lf = 0; /* folding */ ++ws; break; case '\r': ++ws; break; case '\n': ++ws; if (!lf++) break; ++p; --l; /* eoh */ /*@fallthrough@*/ default: if (lf || (*p == ',' && comsep && !quote)) { if (!name.l) { err = EBADMSG; goto out; } err = hdr_add(msg, &name, id, cv ? cv : p, cv ? p - cv - ws : 0, true, cv == v && lf); if (err) goto out; if (!lf) { /* comma separated */ cv = NULL; break; } if (cv != v) { err = hdr_add(msg, &name, id, v ? v : p, v ? p - v - ws : 0, false, true); if (err) goto out; } if (lf > 1) { /* eoh */ err = 0; goto out; } comsep = false; name.p = NULL; cv = v = NULL; lf = 0; } if (!name.p) { name.p = p; name.l = 0; ws = 0; } if (!name.l) { if (*p != ':') { ws = 0; break; } name.l = MAX((int)(p - name.p - ws), 0); if (!name.l) { err = EBADMSG; goto out; } id = hdr_hash(&name); comsep = hdr_comma_separated(id); break; } if (!cv) { quote = false; cv = p; } if (!v) { v = p; } if (*p == '"') quote = !quote; ws = 0; break; } } err = ENODATA; out: if (err) mem_deref(msg); else { *msgp = msg; mb->pos = mb->end - l; } return err; }
int main( int argc, char ** argv) { if (argc == 1) util_exit("block_node node1 node2 node3:2 \n"); /* Initialize lsf environment */ util_setenv( "LSF_BINDIR" , "/prog/LSF/8.0/linux2.6-glibc2.3-x86_64/bin" ); util_setenv( "LSF_LINDIR" , "/prog/LSF/8.0/linux2.6-glibc2.3-x86_64/lib" ); util_setenv( "XLSF_UIDDIR" , "/prog/LSF/8.0/linux2.6-glibc2.3-x86_64/lib/uid" ); util_setenv( "LSF_SERVERDIR" , "/prog/LSF/8.0/linux2.6-glibc2.3-x86_64/etc"); util_setenv( "LSF_ENVDIR" , "/prog/LSF/conf"); util_update_path_var( "PATH" , "/prog/LSF/8.0/linux2.6-glibc2.3-x86_64/bin" , false); util_update_path_var( "LD_LIBRARY_PATH" , "/prog/LSF/8.0/linux2.6-glibc2.3-x86_64/lib" , false); lsf_driver = lsf_driver_alloc(); if (lsf_driver_get_submit_method( lsf_driver ) != LSF_SUBMIT_INTERNAL) util_exit("Sorry - the block_node program must be invoked on a proper LSF node \n"); { int iarg; int total_blocked_target = 0; nodes = hash_alloc(); for (iarg = 1; iarg < argc; iarg++) { char *node_name; int num_slots; { char * num_slots_string; util_binary_split_string( argv[iarg] , ":" , true , &node_name , &num_slots_string); if (num_slots_string) util_sscanf_int( num_slots_string , &num_slots); else num_slots = 1; } if (!hash_has_key( nodes , node_name)) hash_insert_hash_owned_ref( nodes , node_name , count_pair_alloc() , free); { count_pair_type * pair = hash_get( nodes , node_name); pair->target += num_slots; } total_blocked_target += num_slots; } signal(SIGINT , block_node_exit ); { const int sleep_time = 5; const int chunk_size = 10; /* We submit this many at a time. */ const int max_pool_size = 1000; /* The absolute total maximum of jobs we will submit. */ bool cont = true; int pending = 0; bool all_blocked; job_pool = vector_alloc_new(); while (cont) { printf("[Ctrl-C to give up] "); fflush( stdout ); if (cont) sleep( sleep_time ); if (pending == 0) { if (vector_get_size( job_pool ) < max_pool_size) add_jobs( chunk_size ); } update_pool_status( &all_blocked , &pending); print_status(); if (all_blocked) cont = false; } if (!all_blocked) printf("Sorry - failed to block all the nodes \n"); block_node_exit( 0 ); hash_free( nodes ); } } }
struct vsf_client_launch vsf_standalone_main(void) { struct vsf_sysutil_sockaddr* p_accept_addr = 0; int listen_sock = -1; int retval; s_ipaddr_size = vsf_sysutil_get_ipaddr_size(); if (tunable_listen && tunable_listen_ipv6) { die("run two copies of vsftpd for IPv4 and IPv6"); } if (tunable_background) { int forkret = vsf_sysutil_fork(); if (forkret > 0) { /* Parent, just exit */ vsf_sysutil_exit(0); } vsf_sysutil_make_session_leader(); } if (tunable_listen) { listen_sock = vsf_sysutil_get_ipv4_sock(); } else { listen_sock = vsf_sysutil_get_ipv6_sock(); } vsf_sysutil_activate_reuseaddr(listen_sock); s_p_ip_count_hash = hash_alloc(256, s_ipaddr_size, sizeof(unsigned int), hash_ip); s_p_pid_ip_hash = hash_alloc(256, sizeof(int), s_ipaddr_size, hash_pid); if (tunable_setproctitle_enable) { vsf_sysutil_setproctitle("LISTENER"); } vsf_sysutil_install_async_sighandler(kVSFSysUtilSigCHLD, handle_sigchld); vsf_sysutil_install_async_sighandler(kVSFSysUtilSigHUP, handle_sighup); if (tunable_listen) { struct vsf_sysutil_sockaddr* p_sockaddr = 0; vsf_sysutil_sockaddr_alloc_ipv4(&p_sockaddr); vsf_sysutil_sockaddr_set_port(p_sockaddr, tunable_listen_port); if (!tunable_listen_address) { vsf_sysutil_sockaddr_set_any(p_sockaddr); } else { if (!vsf_sysutil_inet_aton(tunable_listen_address, p_sockaddr)) { die2("bad listen_address: ", tunable_listen_address); } } retval = vsf_sysutil_bind(listen_sock, p_sockaddr); vsf_sysutil_free(p_sockaddr); if (vsf_sysutil_retval_is_error(retval)) { die("could not bind listening IPv4 socket"); } } else { struct vsf_sysutil_sockaddr* p_sockaddr = 0; vsf_sysutil_sockaddr_alloc_ipv6(&p_sockaddr); vsf_sysutil_sockaddr_set_port(p_sockaddr, tunable_listen_port); if (!tunable_listen_address6) { vsf_sysutil_sockaddr_set_any(p_sockaddr); } else { struct mystr addr_str = INIT_MYSTR; const unsigned char* p_raw_addr; str_alloc_text(&addr_str, tunable_listen_address6); p_raw_addr = vsf_sysutil_parse_ipv6(&addr_str); str_free(&addr_str); if (!p_raw_addr) { die2("bad listen_address6: ", tunable_listen_address6); } vsf_sysutil_sockaddr_set_ipv6addr(p_sockaddr, p_raw_addr); } retval = vsf_sysutil_bind(listen_sock, p_sockaddr); vsf_sysutil_free(p_sockaddr); if (vsf_sysutil_retval_is_error(retval)) { die("could not bind listening IPv6 socket"); } } vsf_sysutil_listen(listen_sock, VSFTP_LISTEN_BACKLOG); vsf_sysutil_sockaddr_alloc(&p_accept_addr); while (1) { struct vsf_client_launch child_info; void* p_raw_addr; int new_child; int new_client_sock; vsf_sysutil_unblock_sig(kVSFSysUtilSigCHLD); vsf_sysutil_unblock_sig(kVSFSysUtilSigHUP); new_client_sock = vsf_sysutil_accept_timeout( listen_sock, p_accept_addr, 0); vsf_sysutil_block_sig(kVSFSysUtilSigCHLD); vsf_sysutil_block_sig(kVSFSysUtilSigHUP); if (vsf_sysutil_retval_is_error(new_client_sock)) { continue; } ++s_children; child_info.num_children = s_children; child_info.num_this_ip = 0; p_raw_addr = vsf_sysutil_sockaddr_get_raw_addr(p_accept_addr); child_info.num_this_ip = handle_ip_count(p_raw_addr); new_child = vsf_sysutil_fork_failok(); if (new_child != 0) { /* Parent context */ vsf_sysutil_close(new_client_sock); if (new_child > 0) { hash_add_entry(s_p_pid_ip_hash, (void*)&new_child, p_raw_addr); } else { /* fork() failed, clear up! */ --s_children; drop_ip_count(p_raw_addr); } /* Fall through to while() loop and accept() again */ } else { /* Child context */ vsf_sysutil_close(listen_sock); prepare_child(new_client_sock); /* By returning here we "launch" the child process with the same * contract as xinetd would provide. */ return child_info; } } }
static EcBool hash_expand( ec_hash table ) { EcUInt newCapacity = 0, newPrimeIndex; EcUInt i; ec_hash new_table = NULL; ec_hash_entry entry; EcAny invkey; EcAny invval; ENTERF; #ifdef EC_DEBUG if (debTable) { EcUInt lf; if (H_CAPACITY(table)) { lf = (H_ENTRIES(table) << SHIFTAMOUNT); lf /= H_CAPACITY(table); } else lf = 0; #if EC_HAS_VARARGS_MACRO VADEBMESSAGE( debTable, "**************** hash_expand ******************" ); VADEBMESSAGE( debTable, "Current load factor: %ld [LO %ld, HI %ld]", (long)lf, (long)H_LOLOAD(table), (long)H_HILOAD(table) ); #endif } #endif ASSERT( table ); ASSERT( !new_table ); ASSERT( table != new_table ); #if CHECK_PRIME ASSERT( (H_CAPACITY(table) == 0) || isprime(H_CAPACITY(table)) ); #endif invkey = H_INVKEY(table); invval = H_INVVAL(table); if (H_PRIMEINDEX(table) == 0) { newPrimeIndex = FIRSTPRIME; ASSERT(newPrimeIndex < nprimes); newCapacity = primes[newPrimeIndex]; #if CHECK_PRIME ASSERT( isprime( newCapacity ) ); #endif for (i = 1; i < nprimes; i++) { if (primes[i] >= H_MINSIZE(table)) { newPrimeIndex = i; newCapacity = primes[i]; H_MINSIZE(table) = primes[i]; break; } } ASSERT(newCapacity >= H_MINSIZE(table)); ASSERT(H_MINSIZE(table) > 2); #if CHECK_PRIME ASSERT( isprime( newCapacity ) ); #endif #if EC_DEBUG && EC_HAS_VARARGS_MACRO VADEBMESSAGE( debTable, "*1*" ); #endif } else { EcUInt curLoad; newPrimeIndex = H_PRIMEINDEX(table); if (H_CAPACITY(table)) { curLoad = H_ENTRIES(table) << SHIFTAMOUNT; curLoad /= H_CAPACITY(table); } else curLoad = H_HILOAD(table) + 1; /* capacity null => expand ! */ if (curLoad >= H_HILOAD(table)) { do { newPrimeIndex++; ASSERT(newPrimeIndex < nprimes); newCapacity = primes[newPrimeIndex]; ASSERT(newCapacity); #if CHECK_PRIME ASSERT( isprime( newCapacity ) ); #endif curLoad = H_ENTRIES(table) << SHIFTAMOUNT; curLoad /= newCapacity; } while (curLoad > H_LOLOAD(table)); } #if EC_DEBUG && EC_HAS_VARARGS_MACRO VADEBMESSAGE( debTable, "*2*" ); #endif } ASSERT(newCapacity >= H_MINSIZE(table)); #if CHECK_PRIME ASSERT( isprime( newCapacity ) ); #endif if (! H_ENTRYBASE(table)) { H_ENTRYBASE(table) = (ec_hash_entry) ec_malloc( sizeof(struct ec_hash_entry_struct) * newCapacity ); if (! H_ENTRYBASE(table)) return FALSE; H_CAPACITY(table) = newCapacity; H_PRIMEINDEX(table) = newPrimeIndex; /* IMPORTANT: initialize new slots */ for (i = 0; i < newCapacity; i++) { EC_HASH_ENTRY_KEY(H_ENTRY(table, i)) = invkey; EC_HASH_ENTRY_VALUE(H_ENTRY(table, i)) = invval; } #if CHECK_PRIME ASSERT( isprime(H_CAPACITY(table)) ); #endif return TRUE; } else { EcBool old_xfer; #if EC_DEBUG && EC_HAS_VARARGS_MACRO VADEBMESSAGE( debTable, "Expanding table to %ld elements", (long)newCapacity ); #endif ASSERT( !new_table ); new_table = hash_alloc(); ASSERT( !H_ENTRYBASE(new_table) ); H_DEF(new_table) = H_DEF(table); H_ENTRIES(new_table) = 0; H_CAPACITY(new_table) = 0; H_PRIMEINDEX(new_table) = 0; H_ENTRYBASE(new_table) = 0; /* new_table.loLoad = (1 << SHIFTAMOUNT); */ /* BUG CORRECTED */ /* new_table.hiLoad = (1 << SHIFTAMOUNT); */ H_MINSIZE(new_table) = H_MINSIZE(table); H_LOLOAD(new_table) = H_LOLOAD(table); H_HILOAD(new_table) = H_HILOAD(table); H_ENTRYBASE(new_table) = (ec_hash_entry) ec_malloc( sizeof(struct ec_hash_entry_struct) * newCapacity ); if (! H_ENTRYBASE(new_table)) return FALSE; H_ENTRIES(new_table) = 0; H_CAPACITY(new_table) = newCapacity; H_PRIMEINDEX(new_table) = newPrimeIndex; #if EC_DEBUG && EC_HAS_VARARGS_MACRO VADEBMESSAGE( debTable, "newCapacity: %ld, newPrimeIndex: %ld", (long)newCapacity, (long)newPrimeIndex ); #endif /* IMPORTANT: initialize new slots */ for (i = 0; i < newCapacity; i++) { EC_HASH_ENTRY_KEY(H_ENTRY(new_table, i)) = invkey; EC_HASH_ENTRY_VALUE(H_ENTRY(new_table, i)) = invval; } #if EC_DEBUG && EC_HAS_VARARGS_MACRO VADEBMESSAGE( debTable, "oldTable capacity: %ld entries: %ld\n", (long)H_CAPACITY(table), (long)H_ENTRIES(table) ); #endif /* fill new table with old values */ ASSERT( table != new_table ); old_xfer = xfer_only; xfer_only = TRUE; for (i = 0; i < H_CAPACITY(table); i++) { entry = H_ENTRY(table, i); if (EC_HASH_ENTRY_KEY(entry) != invkey) { ec_hash_set( new_table, EC_HASH_ENTRY_KEY(entry), EC_HASH_ENTRY_VALUE(entry) ); } } xfer_only = old_xfer; #if EC_DEBUG && EC_HAS_VARARGS_MACRO VADEBMESSAGE( debTable, "newTable capacity: %ld entries: %ld\n", (long)H_CAPACITY(new_table), (long)H_ENTRIES(new_table) ); #endif ASSERT( H_ENTRIES(table) == H_ENTRIES(new_table) ); /* free old table contents */ hash_cleanup( table ); /* move new table to old one */ memcpy( table, new_table, sizeof(struct ec_hash_struct) ); /* free new table space (not contents !) */ hash_free( new_table ); new_table = NULL; #if EC_DEBUG && EC_HAS_VARARGS_MACRO VADEBMESSAGE( debTable, "Table capacity: %ld entries: %ld\n", (long)H_CAPACITY(table), (long)H_ENTRIES(table) ); #endif #if CHECK_PRIME ASSERT( isprime(H_CAPACITY(table)) ); #endif return TRUE; } return FALSE; }
int main(void) { daemon(1, 0); parseconf_load_file(MINIFTPD_CONF); if (getuid() != 0) { fprintf(stderr, "must be started by root.\n"); exit(EXIT_FAILURE); } int listenfd = tcp_server(tunable_listen_address, tunable_listen_port); int conn; session_t sess = { //控制连接 0, -1, "", "", "", // ftp协议进程与nobody进程通信 -1, -1, //限速 0, 0, 0, 0, // 数据连接 NULL, -1, -1, 0, // ftp协议控制 0, 0, NULL, 0, //客户端连接数控制 0, 0 }; p_sess = &sess; sess.upload_speed_max = tunable_upload_max_rate; sess.download_speed_max = tunable_download_max_rate; signal(SIGCHLD, handle_sigchld); struct sockaddr_in peeraddr; s_ip_count_hash = hash_alloc(256, hash_func); s_pid_ip_hash = hash_alloc(256, hash_func); while(1) { if ((conn = accept_timeout(listenfd, &peeraddr, 0)) < 0) ERR_EXIT("accept_timeout"); unsigned int ip = (unsigned int)peeraddr.sin_addr.s_addr; sess.num_this_ip = handle_ip_count(&ip); ++cur_childrens; sess.num_clients = cur_childrens; pid_t pid = fork(); if (pid == -1) { --cur_childrens; ERR_EXIT("fork"); } if (pid == 0) { sess.ctrl_fd = conn; close(listenfd); check_clients_limit(&sess); signal(SIGCHLD, SIG_IGN); begin_session(&sess); } else if (pid > 0) { hash_add_entry(s_pid_ip_hash, &pid, sizeof(pid), &ip, sizeof(unsigned int)); close(conn); } } hash_free(s_ip_count_hash); hash_free(s_pid_ip_hash); return 0; }
void ensemble_config_init_FIELD( ensemble_config_type * ensemble_config , const config_content_type * config , ecl_grid_type * grid) { if (config_content_has_item(config , FIELD_KEY)) { const config_content_item_type * item = config_content_get_item( config , FIELD_KEY ); int i; for (i=0; i < config_content_item_get_size( item ); i++) { const config_content_node_type * node = config_content_item_iget_node( item , i ); const char * key = config_content_node_iget( node , 0 ); const char * var_type_string = config_content_node_iget( node , 1 ); enkf_config_node_type * config_node; { hash_type * options = hash_alloc(); int truncation = TRUNCATE_NONE; double value_min = -1; double value_max = -1; config_content_node_init_opt_hash( node , options , 2 ); if (hash_has_key( options , MIN_KEY)) { truncation |= TRUNCATE_MIN; value_min = atof(hash_get( options , MIN_KEY)); } if (hash_has_key( options , MAX_KEY)) { truncation |= TRUNCATE_MAX; value_max = atof(hash_get( options , MAX_KEY)); } if (strcmp(var_type_string , DYNAMIC_KEY) == 0) { config_node = ensemble_config_add_field( ensemble_config , key , grid , false); enkf_config_node_update_state_field( config_node , truncation , value_min , value_max ); } else if (strcmp(var_type_string , PARAMETER_KEY) == 0) { const char * ecl_file = config_content_node_iget( node , 2 ); const char * init_file_fmt = hash_safe_get( options , INIT_FILES_KEY ); const char * init_transform = hash_safe_get( options , INIT_TRANSFORM_KEY ); const char * output_transform = hash_safe_get( options , OUTPUT_TRANSFORM_KEY ); const char * min_std_file = hash_safe_get( options , MIN_STD_KEY ); const char * forward_string = hash_safe_get( options , FORWARD_INIT_KEY ); bool forward_init = false; if (forward_string) { if (!util_sscanf_bool( forward_string , &forward_init)) fprintf(stderr,"** Warning: parsing %s as bool failed - using FALSE \n",forward_string); } config_node = ensemble_config_add_field( ensemble_config , key , grid , forward_init); enkf_config_node_update_parameter_field( config_node, ecl_file , init_file_fmt , min_std_file , truncation , value_min , value_max , init_transform , output_transform ); } else if (strcmp(var_type_string , GENERAL_KEY) == 0) { /* General - not really interesting .. */ const char * ecl_file = config_content_node_iget( node , 2 ); const char * enkf_infile = config_content_node_iget( node , 3 ); const char * init_file_fmt = hash_safe_get( options , INIT_FILES_KEY ); const char * init_transform = hash_safe_get( options , INIT_TRANSFORM_KEY ); const char * output_transform = hash_safe_get( options , OUTPUT_TRANSFORM_KEY ); const char * input_transform = hash_safe_get( options , INPUT_TRANSFORM_KEY ); const char * min_std_file = hash_safe_get( options , MIN_STD_KEY ); const char * forward_string = hash_safe_get( options , FORWARD_INIT_KEY ); bool forward_init = false; if (forward_string) { if (!util_sscanf_bool( forward_string , &forward_init)) fprintf(stderr,"** Warning: parsing %s as bool failed - using FALSE \n",forward_string); } config_node = ensemble_config_add_field( ensemble_config , key , grid , forward_init); enkf_config_node_update_general_field( config_node, ecl_file , enkf_infile , init_file_fmt , min_std_file , truncation , value_min , value_max , init_transform , input_transform , output_transform); } else util_abort("%s: field type: %s is not recognized\n",__func__ , var_type_string); hash_free( options ); } } } }
int sip_transp_init(struct sip *sip, uint32_t sz) { return hash_alloc(&sip->ht_conn, sz); }
int main() { stu_t stu_arr[]={ {"1","A",20}, {"2","B",21}, {"3","C",22} }; hash_t *hash=hash_alloc(256,hash_function); int size = sizeof(stu_arr)/sizeof(stu_arr[0]); /*用字符串作为关键码 int i=0; for(;i<size;++i) { hash_add_entry(hash,stu_arr[i].num,strlen(stu_arr[i].num),&stu_arr[i],sizeof(stu_arr[i])); } stu_t *s = (stu_t*)hash_lookuo_enty(hash,"1",1); if(s != NULL) { printf("%s %s %d\n",s->num,s->name,s->age); } else { printf("not found\n"); } hash_free_entry(hash,"1",1); s = (stu_t*)hash_lookuo_enty(hash,"1",1); if(s != NULL) { printf("%s %s %d\n",s->num,s->name,s->age); } else { printf("not found\n"); } */ //用年龄作为关键码 (用整数作为关键码) int i=0; for(;i<size;++i) { hash_add_entry(hash,&stu_arr[i].age,sizeof(stu_arr[i].age),&stu_arr[i],sizeof(stu_arr[i])); } int key=20; stu_t *s = (stu_t*)hash_lookuo_enty(hash,&key,4); if(s != NULL) { printf("%s %s %d\n",s->num,s->name,s->age); } else { printf("not found\n"); } hash_free_entry(hash,&key,4); s = (stu_t*)hash_lookuo_enty(hash,&key,4); if(s != NULL) { printf("%s %s %d\n",s->num,s->name,s->age); } else { printf("not found\n"); } hash_dealloc(hash,256); return 0; }
void init_hash() { ip_to_clients = hash_alloc(256, hash_func); pid_to_ip = hash_alloc(256, hash_func); }