static char *get(char *type, char *line, int itype, int n) { int l; l = hempty(line) + my_strlen(type) + hempty(line + hempty(line) + my_strlen(type)); if (line[l] != '"') lerror(INVALID(itype), n); if (!IN(line + l + 1, '"')) lerror(INVALID(itype), n); return (my_strndup(line + l + 1, my_strilen(line + l + 1, '"'))); }
static int _openTcpClient( UThread* ut, struct sockaddr* addr, socklen_t addrlen ) { SOCKET fd; #ifdef __APPLE__ int yes = 1; #endif fd = socket( AF_INET, SOCK_STREAM, 0 ); if( INVALID(fd) ) { ur_error( ut, UR_ERR_ACCESS, "socket %s", SOCKET_ERR ); return -1; } #ifdef __APPLE__ if( setsockopt( fd, SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof(int) ) != 0 ) { closesocket( fd ); ur_error( ut, UR_ERR_ACCESS, "setsockopt %s", SOCKET_ERR ); return -1; } #endif if( connect( fd, addr, addrlen ) < 0 ) { closesocket( fd ); ur_error( ut, UR_ERR_ACCESS, "connect %s", SOCKET_ERR ); return -1; } return fd; }
/* \param ext If non-zero, then bind socket to ext->addr. */ static int _openUdpSocket( UThread* ut, SocketExt* ext, int nowait ) { SOCKET fd; fd = socket( AF_INET, SOCK_DGRAM, 0 ); if( INVALID(fd) ) { ur_error( ut, UR_ERR_ACCESS, "socket %s", SOCKET_ERR ); return -1; } if( nowait ) { #ifdef _WIN32 u_long flags = 1; ioctlsocket( fd, FIONBIO, &flags ); #else fcntl( fd, F_SETFL, fcntl( fd, F_GETFL, 0 ) | O_NONBLOCK ); #endif } if( ext ) { if( bind( fd, &ext->addr, ext->addrlen ) < 0 ) { closesocket( fd ); ur_error( ut, UR_ERR_ACCESS, "bind %s", SOCKET_ERR ); return -1; } } return fd; }
static int _openTcpServer( UThread* ut, struct sockaddr* addr, socklen_t addrlen, int backlog ) { SOCKET fd; int yes = 1; fd = socket( AF_INET, SOCK_STREAM, 0 ); if( INVALID(fd) ) { ur_error( ut, UR_ERR_ACCESS, "socket %s", SOCKET_ERR ); return -1; } if( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int) ) != 0 ) { closesocket( fd ); ur_error( ut, UR_ERR_ACCESS, "setsockopt %s", SOCKET_ERR ); return -1; } if( bind( fd, addr, addrlen ) != 0 ) { closesocket( fd ); ur_error( ut, UR_ERR_ACCESS, "bind %s", SOCKET_ERR ); return -1; } if( listen( fd, backlog ) != 0 ) { closesocket( fd ); ur_error( ut, UR_ERR_ACCESS, "listen %s", SOCKET_ERR ); return -1; } return fd; }
kernel void KERNEL_NAME( int32_t nq, // number of q values const int32_t pd_start, // where we are in the polydispersity loop const int32_t pd_stop, // where we are stopping in the polydispersity loop global const ProblemDetails *details, global const double *values, global const double *q, // nq q values, with padding to boundary global double *result, // nq+3 return values, again with padding const double cutoff // cutoff in the polydispersity weight product ) { // Storage for the current parameter values. These will be updated as we // walk the polydispersity cube. ParameterBlock local_values; // current parameter values double *pvec = (double *)(&local_values); // Alias named parameters with a vector // Fill in the initial variables #ifdef USE_OPENMP #pragma omp parallel for #endif for (int k=0; k < NPARS; k++) { pvec[k] = values[k+2]; } // Monodisperse computation if (details->num_active == 0) { double norm, scale, background; #ifdef INVALID if (INVALID(local_values)) { return; } #endif norm = CALL_VOLUME(local_values); scale = values[0]; background = values[1]; #ifdef USE_OPENMP #pragma omp parallel for #endif for (int q_index=0; q_index < nq; q_index++) { double scattering = CALL_IQ(q, q_index, local_values); result[q_index] = (norm>0. ? scale*scattering/norm + background : background); } return; } #if MAX_PD > 0 const double *pd_value = values+2+NPARS; const double *pd_weight = pd_value+details->pd_sum; // need product of weights at every Iq calc, so keep product of // weights from the outer loops so that weight = partial_weight * fast_weight double pd_norm; double partial_weight; // product of weight w4*w3*w2 but not w1 double spherical_correction; // cosine correction for latitude variation double weight; // product of partial_weight*w1*spherical_correction // Number of elements in the longest polydispersity loop const int p0_par = details->pd_par[0]; const int p0_length = details->pd_length[0]; const int p0_offset = details->pd_offset[0]; const int p0_is_theta = (p0_par == details->theta_par); int p0_index; // Trigger the reset behaviour that happens at the end the fast loop // by setting the initial index >= weight vector length. p0_index = p0_length; // Default the spherical correction to 1.0 in case it is not otherwise set spherical_correction = 1.0; // Since we are no longer looping over the entire polydispersity hypercube // for each q, we need to track the result and normalization values between // calls. This means initializing them to 0 at the start and accumulating // them between calls. pd_norm = (pd_start == 0 ? 0.0 : result[nq]); if (pd_start == 0) { #ifdef USE_OPENMP #pragma omp parallel for #endif for (int q_index=0; q_index < nq; q_index++) { result[q_index] = 0.0; } } // Loop over the weights then loop over q, accumulating values for (int loop_index=pd_start; loop_index < pd_stop; loop_index++) { // check if fast loop needs to be reset if (p0_index == p0_length) { // Compute position in polydispersity hypercube and partial weight partial_weight = 1.0; for (int k=1; k < details->num_active; k++) { int pk = details->pd_par[k]; int index = details->pd_offset[k] + (loop_index/details->pd_stride[k])%details->pd_length[k]; pvec[pk] = pd_value[index]; partial_weight *= pd_weight[index]; if (pk == details->theta_par) { spherical_correction = fmax(fabs(cos(M_PI_180*pvec[pk])), 1.e-6); } } p0_index = loop_index%p0_length; } // Update parameter p0 weight = partial_weight*pd_weight[p0_offset + p0_index]; pvec[p0_par] = pd_value[p0_offset + p0_index]; if (p0_is_theta) { spherical_correction = fmax(fabs(cos(M_PI_180*pvec[p0_par])), 1.e-6); } p0_index++; #ifdef INVALID if (INVALID(local_values)) continue; #endif // Accumulate I(q) // Note: weight==0 must always be excluded if (weight > cutoff) { // spherical correction has some nasty effects when theta is +90 or -90 // where it becomes zero. If the entirety of the correction weight *= spherical_correction; pd_norm += weight * CALL_VOLUME(local_values); #ifdef USE_OPENMP #pragma omp parallel for #endif for (int q_index=0; q_index < nq; q_index++) { const double scattering = CALL_IQ(q, q_index, local_values); result[q_index] += weight*scattering; } } } if (pd_stop >= details->pd_prod) { // End of the PD loop we can normalize double scale, background; scale = values[0]; background = values[1]; #ifdef USE_OPENMP #pragma omp parallel for #endif for (int q_index=0; q_index < nq; q_index++) { result[q_index] = (pd_norm>0. ? scale*result[q_index]/pd_norm + background : background); } } // Remember the updated norm. result[nq] = pd_norm; #endif // MAX_PD > 0 }
static int internal_coalesce(void) { BlockP block; BlockP previous; BlockP tail; #ifndef BLOCKS_GUARDED BlockP bin_copy[NBINS+2]; #endif size_t size; /* where size is used to specify an element of an array it should really be * called index, but to generate better code I got rid of the index variable */ F0("!!internal_coalesce..."); #ifdef STATS statsP->stats.coalesces++; #endif lookInBins = FALSE; totalFree = 0; /* set bins and overflow lists to empty */ for (size = 0; size <= NBINS+1; size++) { bin[size] = NULL; #ifndef BLOCKS_GUARDED bin_copy[size] = NULL; #endif } block = heapLow; /* NULL indicates previous doesn't point to start of free block */ previous = NULL; tail = NULL; while (block <= heapHigh) { if (INVALID(block)) return CORRUPT; if (FREE(block)) { /* free block */ if (previous == NULL) previous = block; } else if (previous != NULL) { size = PTRDIFF(block, previous) - OVERHEAD; /* set flags to Free */ totalFree += size; previous->size = (size | FREEBIT); if (size <= MAXBINSIZE) { /* return to bin */ size /= BINRANGE; if (bin[size] == NULL) bin[size] = previous; else { /* if not BLOCKS_GUARDED use guard word of first block in bin to hold * a pointer to the last block in the list for this bin otherwise * use the bin_copy array. This allows me to keep the list in * ascending address order. Remember to put back the guard words at * the end of coalescing if BLOCKS_GUARDED. */ #ifdef BLOCKS_GUARDED ((BlockP) bin[size]->guard)->next = previous; #else (bin_copy[size])->next = previous; #endif } #ifdef BLOCKS_GUARDED bin[size]->guard = (int) previous; #else bin_copy[size] = previous; #endif } else { /* put block on overflow list */ if (bin[0] == NULL) {bin[0] = previous; previous->previous = NULL;} else {tail->next = previous; previous->previous = tail;} tail = previous; } previous = NULL; } ADDBYTESTO(block, SIZE(block) + OVERHEAD); } /* replace the guard words at the start of the bins lists */ for (size = 1; size <= NBINS; size++) { if (bin[size] != NULL) { lookInBins = TRUE; #ifdef BLOCKS_GUARDED ((BlockP) bin[size]->guard)->next = NULL; bin[size]->guard = GUARDCONSTANT; #else (bin_copy[size])->next = NULL; #endif } } /* do both ends of overflow list */ if (bin[0] != NULL) { tail->next = NULL; bin[NBINS+1] = tail; } else { bin[NBINS+1] = NULL; } lastFreeBlockOnHeap = bin[NBINS+1]; F0(" ... complete\n"); return OK; }
/** * Function that prepares buffer for n-grammification: * runs of invalid characters are collapsed to a single * underscore. * * Function is implemented as a finite state machine. */ static char *prepbuffer( const char *src, size_t bufsize ) { const char *p = src; char *dest = (char *)wg_malloc( bufsize + 3 ); char *w = dest; char *wlimit = dest + bufsize + 1; if ( INVALID(*p) ) { goto SPACE; } else if ( *p == '\0' ) { goto END; } *w++ = '_'; if ( w == wlimit ) { goto STOP; } goto WORD; SPACE: /*** Inside string of invalid characters ***/ p++; if ( INVALID(*p) ) { goto SPACE; } else if ( *p == '\0' ) { goto END; } *w++ = '_'; if ( w == wlimit ) { goto STOP; } goto WORD; WORD: /*** Inside string of valid characters ***/ *w++ = *p++; if ( w == wlimit ) { goto END; } else if ( INVALID(*p) ) { goto SPACE; } else if ( *p == '\0' ) { goto STOP; } goto WORD; END: *w++ = '_'; STOP: *w++ = '\0'; /*** Docs that are too small for a fingerprint, are refused ***/ if ( w - dest < MINDOCSIZE ) { wg_free(dest); return NULL; } return dest; }
int check_basic_conf(server_configuration * config) { #define INVALID(msg) \ { \ fprintf(stderr, "%s\n", msg); \ return FAIL; \ } if(!config->log_file_name) INVALID("Log file name missing."); if(!config->db_server_address || !strlen(config->db_server_address)) INVALID("Server address missing."); if(!config->db_user_name || !strlen(config->db_user_name)) INVALID("User name missing."); if(!config->query_discover || !strlen(config->query_discover)) INVALID("SQL DHCPDISCOVER query missing."); if(!config->query_history || !strlen(config->query_history)) INVALID("SQL DHCPHISTORY query missing."); if((config->db_server_port < MIN_TCP_PORT) || (config->db_server_port > MAX_TCP_PORT) ) INVALID("Invalid database port value."); if(!config->db_clients_count) config->db_clients_count = DEFAULT_DBCL_CNT; else if((config->db_clients_count < MIN_DBCLIENTS) || (config->db_clients_count > MAX_DBCLIENTS)) INVALID("Invalid database clients count value."); if(!config->max_qps_host) config->max_qps_host = DEFAULT_MAXQPS_HOST; else if(config->max_qps_host < 1) INVALID("Invalid maximum QPS for host value."); if(!config->max_qps_total) config->max_qps_total = DEFAULT_MAXQPS_TOTAL; else if(config->max_qps_total < config->max_qps_host) INVALID("Invalid maximum total QPS value."); if(!config->dhcp_server_port) { struct servent * srv = getservbyname("bootps", NULL); if(!srv) INVALID("Can't get server port value from system DB"); config->dhcp_server_port = ntohs(srv->s_port); } else if((config->dhcp_server_port < MIN_UDP_PORT) || (config->dhcp_server_port > MAX_UDP_PORT)) INVALID("Invalid DHCP server port."); if(!config->dhcp_client_port) { struct servent * srv = getservbyname("bootpc", NULL); if(!srv) INVALID("Can't get client port value from system DB"); config->dhcp_client_port = ntohs(srv->s_port); } else if((config->dhcp_client_port < MIN_UDP_PORT) || (config->dhcp_client_port > MAX_UDP_PORT)) INVALID("Invalid DHCP client port."); /*if(!config->db_type) INVALID("DBType is not set.");*/ if(!config->db_name) INVALID("DBName is not set."); if(!config->db_server_address) INVALID("DBServerAddress is not set."); if(!config->db_user_name) INVALID("DBUserName is not set."); #undef INVALID return OK; }