static void mkvar(char **p, char *path_prefix, char *var_dir, char *env_var) { char *buffer; /* Override by environment variables */ buffer = getenv(env_var); #ifdef HAVE_ASPRINTF if (buffer) asprintf(p, "%s", buffer); else asprintf(p, "%s%s%s", path_prefix, DIR_PATHSEP, var_dir); #else /* ~ HAVE_ASPRINTF */ if (buffer){ *p = (char *) tmalloc(strlen(buffer)+1); sprintf(*p,"%s",buffer); /* asprintf(p, "%s", buffer); */ } else{ *p = (char *) tmalloc(strlen(path_prefix) + strlen(DIR_PATHSEP) + strlen(var_dir) + 1); sprintf(*p, "%s%s%s", path_prefix, DIR_PATHSEP, var_dir); /* asprintf(p, "%s%s%s", path_prefix, DIR_PATHSEP, var_dir); */ } #endif /* HAVE_ASPRINTF */ }
char *INPerror(int type) { const char *val; char *ebuf; /*CDHW Lots of things set errMsg but it is never used so let's hack it in CDHW*/ if ( errMsg ) { val = errMsg; errMsg = NULL;} else /*CDHW end of hack CDHW*/ val = SPerror(type); if (!val) return NULL; #ifdef HAVE_ASPRINTF if (errRtn) asprintf(&ebuf, "%s detected in routine \"%s\"\n", val, errRtn); else asprintf(&ebuf, "%s\n", val); #else /* ~ HAVE_ASPRINTF */ if (errRtn){ ebuf = (char *) tmalloc(strlen(val) + strlen(errRtn) + 25); sprintf(ebuf, "%s detected in routine \"%s\"\n", val, errRtn); } else{ ebuf = (char *) tmalloc(strlen(val) + 2); sprintf(ebuf, "%s\n", val); } #endif /* HAVE_ASPRINTF */ FREE(errMsg); /* pn: really needed ? */ return ebuf; }
long memory_count() { static ITEM_LIST *item; static long initialized = 0; long mem; if (!initialized) { /* allocate an item list structure */ item = tmalloc((unsigned)sizeof(*item)+sizeof(long)); item->buffer_address = tmalloc((unsigned)4); /* 1 longword */ item->return_length_address = tmalloc((unsigned)4); /* 1 longword */ *((char*)item+sizeof(*item)) = 0; initialized = 1; } /* get global page count */ item->code = JPI$_GPGCNT; item->buffer_length = 4; if (sys$getjpiw(0, 0, 0, item, 0, 0, 0)!=SS$_NORMAL) return(0); mem = *(item->buffer_address); /* get page count in working set */ item->code = JPI$_PPGCNT; item->buffer_length = 4; if (sys$getjpiw(0, 0, 0, item, 0, 0, 0)!=SS$_NORMAL) return(0); return(mem + *(item->buffer_address)); }
double ming( double (*fn)(), /* pointer to fn to be minimize */ double *x, /* array to return location of minimum in */ double *xlo, /* array of lower limits */ double *xhi, /* array of upper limits */ long *n_steps, /* array of numbers of steps */ long np /* number of parameters */ ) { register double f0; double *dx, *xbest, min_val; long i, *counter; xbest = tmalloc(sizeof(double)*np); dx = tmalloc(sizeof(double)*np); counter = tmalloc(sizeof(long)*np); for (i=0; i<np; i++) { xbest[i] = x[i] = xlo[i]; dx[i] = (xhi[i]-xlo[i])/(n_steps[i]-1); counter[i] = 0; } min_val = (*fn)(xbest); while (advance_values(x, counter, n_steps, xlo, dx, np)) { if ((f0=(*fn)(x))<min_val) { for (i=0; i<np; i++) xbest[i] = x[i]; min_val = f0; } } for (i=0; i<np; i++) x[i] = xbest[i]; return(min_val); }
void *trealloc(void *vp, unsigned newbytes) { void *newp = NULL; /* behavior on corner cases conforms to SUSv2 */ if (vp == NULL) return tmalloc(newbytes); if (newbytes != 0) { CHUNK *oldchunk; unsigned bytes; if ( (newp = tmalloc(newbytes)) == NULL) return NULL; oldchunk = TOCHUNK(vp); bytes = CHUNKSIZE(oldchunk) - sizeof(CHUNK); if (bytes > newbytes) bytes = newbytes; memcpy(newp, vp, bytes); } tfree(vp); return newp; }
long randomWalkMin( double *best_result, double *xReturn, double *lower, double *upper, double *stepSize, long n_dimen, double target, double (*func)(double *x, long *invalid), long nSamples, double (*random_f)(long iseed) ) { double *x, *xBest; double result; long flag, i, best_found = 0; optimFlags = 0; if (random_f==NULL) random_f = random_1; x = tmalloc(sizeof(*x)*n_dimen); xBest = tmalloc(sizeof(*xBest)*n_dimen); for (i=0; i<n_dimen; i++) xBest[i] = xReturn[i]; *best_result = DBL_MAX; while (nSamples--) { for (i=0; i<n_dimen; i++) { x[i] = xBest[i] + 2*stepSize[i]*(0.5-random_f(0)); if (lower && x[i]<lower[i]) x[i] = lower[i]; if (upper && x[i]>upper[i]) x[i] = upper[i]; } result = (*func)(x, &flag); if (flag==0 && result<*best_result) { *best_result = result; for (i=0; i<n_dimen; i++) xBest[i] = x[i]; best_found = 1; if (result<target) break; } if (optimFlags&OPTIM_ABORT) break; } if (best_found) { for (i=0; i<n_dimen; i++) xReturn[i] = xBest[i]; } free(x); free(xBest); return(best_found); }
void setupOutputFile(SDDS_DATASET *OutputTable, int32_t *xIndex, int32_t *yIndex, int32_t *fitIndex, int32_t *residualIndex, char *output, long fullOutput, SDDS_DATASET *InputTable, char *xName, char *yName) { char *name, *yUnits, *description, *xUnits, *inverse_xUnits; int32_t typeValue = SDDS_DOUBLE; static char *residualNamePart = "Residual"; static char *residualDescriptionPart = "Residual of sinusoidal fit to "; if (!SDDS_InitializeOutput(OutputTable, SDDS_BINARY, 0, NULL, "sddsexpfit output", output) || !SDDS_TransferColumnDefinition(OutputTable, InputTable, xName, NULL) || !SDDS_ChangeColumnInformation(OutputTable, "type", &typeValue, SDDS_BY_NAME, xName) || (*xIndex=SDDS_GetColumnIndex(OutputTable, xName))<0 || !SDDS_GetColumnInformation(InputTable, "units", &xUnits, SDDS_BY_NAME, xName) || !SDDS_GetColumnInformation(InputTable, "units", &yUnits, SDDS_BY_NAME, yName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); name = tmalloc(sizeof(*name)*(strlen(yName)+strlen(residualNamePart)+1)); description = tmalloc(sizeof(*name)*(strlen(yName)+strlen(residualDescriptionPart)+1)); if (fullOutput) { if (!SDDS_TransferColumnDefinition(OutputTable, InputTable, yName, NULL) || !SDDS_ChangeColumnInformation(OutputTable, "type", &typeValue, SDDS_BY_NAME, yName) || (*yIndex=SDDS_GetColumnIndex(OutputTable, yName))<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); sprintf(name, "%s%s", yName, residualNamePart); sprintf(description, "%s%s", yName, residualDescriptionPart); if ((*residualIndex=SDDS_DefineColumn(OutputTable, name, NULL, yUnits, description, NULL, SDDS_DOUBLE, 0))<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } sprintf(name, "%sFit", yName); sprintf(description, "Sinusoidal fit to %s", yName); if ((*fitIndex=SDDS_DefineColumn(OutputTable, name, NULL, yUnits, description, NULL, SDDS_DOUBLE, 0))<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); inverse_xUnits = makeInverseUnits(xUnits); if (SDDS_DefineParameter(OutputTable, "sinefitConstant", NULL, yUnits, "Constant term from sinusoidal fit", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineParameter(OutputTable, "sinefitFactor", NULL, yUnits, "Factor from sinusoidal fit", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineParameter(OutputTable, "sinefitFrequency", NULL, inverse_xUnits, "Frequency from sinusoidal fit", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineParameter(OutputTable, "sinefitPhase", NULL, xUnits, "Phase from sinusoidal fit", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineParameter(OutputTable, "sinefitRmsResidual", NULL, yUnits, "rms residual from sinusoidal fit", NULL, SDDS_DOUBLE, 0)<0 || !SDDS_WriteLayout(OutputTable)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); }
cDict *dict_new(cList *keys, cList *values) { cDict *cnew; Int i, j, size; if (generic_empty_dict && list_length(keys) == 0) return dict_dup(generic_empty_dict); /* Construct a new dictionary. */ cnew = tmalloc(sizeof(cDict)); cnew->keys = list_dup(keys); cnew->values = list_dup(values); /* Calculate initial size of chain and hash table. */ cnew->hashtab_size = HASHTAB_STARTING_SIZE; while (cnew->hashtab_size < keys->len) { if (cnew->hashtab_size > 4096) cnew->hashtab_size += 4096; else cnew->hashtab_size = cnew->hashtab_size * 2 + MALLOC_DELTA; } /* Initialize chain entries and hash table. */ size = sizeof(Int) * cnew->hashtab_size; cnew->links = tmalloc(size); cnew->hashtab = tmalloc(size); memset(cnew->links, -1, size); memset(cnew->hashtab, -1, size); /* Insert the keys into the hash table, eliminating duplicates. */ i = j = 0; while (i < cnew->keys->len) { if (i != j) { cnew->keys->el[j] = cnew->keys->el[i]; cnew->values->el[j] = cnew->values->el[i]; } if (search(cnew, &keys->el[i]) == F_FAILURE) { insert_key(cnew, j++); } else { data_discard(&cnew->keys->el[i]); data_discard(&cnew->values->el[i]); } i++; } cnew->keys->len = cnew->values->len = j; cnew->refs = 1; if (!generic_empty_dict && list_length(keys) == 0) generic_empty_dict = dict_dup(cnew); return cnew; }
static nonlocal_info *nlinfo_alloc(uint np, uint count, uint maxv) { nonlocal_info *info = tmalloc(nonlocal_info,1); info->np = np; info->target = tmalloc(uint,2*np+count); info->nshared = info->target + np; info->sh_ind = info->nshared + np; info->reqs = tmalloc(MPI_Request,2*np); info->buf = tmalloc(real,2*count*maxv); info->maxv = maxv; return info; }
/* va: we should use tmalloc, whith also makes failure test */ int IFnewUid(void *ckt, IFuid * newuid, IFuid olduid, char *suffix, int type, void **nodedata) { char *newname; int error; if (olduid) { #ifdef HAVE_ASPRINTF asprintf(&newname, "%s#%s", (char *) olduid, suffix); #else /* ~ HAVE_ASPRINTF */ newname = (char *) tmalloc(strlen((char *) olduid) + strlen(suffix) + 2); /* 2 = strlen("#\0") */ sprintf(newname, "%s#%s", (char *) olduid, suffix); #endif /* HAVE_ASPRINTF */ } else { #ifdef HAVE_ASPRINTF asprintf(&newname, "%s", suffix); #else /* ~ HAVE_ASPRINTF */ newname = (char *) tmalloc(strlen(suffix) + 1 ); sprintf(newname, "%s", suffix); #endif /* HAVE_ASPRINTF */ } switch (type) { case UID_ANALYSIS: case UID_TASK: case UID_INSTANCE: case UID_OTHER: case UID_MODEL: error = INPinsert(&newname, (INPtables *) ft_curckt->ci_symtab); if (error && error != E_EXISTS) return (error); *newuid = (IFuid) newname; break; case UID_SIGNAL: error = INPmkTerm(ckt, &newname, (INPtables *) ft_curckt->ci_symtab, nodedata); if (error && error != E_EXISTS) return (error); *newuid = (IFuid) newname; break; default: return (E_BADPARM); } return (OK); }
long rpn_create_mem(char *name, short is_string) { long i_mem; int32_t duplicate; MEMORY *newMem; if (is_func(name)!=-1 || find_udf(name)!=-1) { fprintf(stderr, "error: attempt to create rpn memory with reserved name \"%s\"\n", name); return -1; } if (Memory==NULL || n_memories>=max_n_memories) { Memory = trealloc(Memory, sizeof(*Memory)*(max_n_memories+=10)); memoryData = trealloc(memoryData, sizeof(*memoryData)*max_n_memories); str_memoryData = trealloc(str_memoryData, sizeof(*str_memoryData)*max_n_memories); } newMem = tmalloc(sizeof(*newMem)); newMem->name = name; /* temporary copy */ i_mem = binaryInsert((void**)Memory, n_memories, (void*)newMem, compare_mem, &duplicate); if (duplicate) { free(newMem); return Memory[i_mem]->index; } cp_str(&newMem->name, name); newMem->index = n_memories; newMem->is_string = is_string; memoryData[n_memories] = 0; str_memoryData[n_memories] = NULL; n_memories++; memory_added = 1; return Memory[i_mem]->index; }
int CKTmodCrt(CKTcircuit *ckt, int type, GENmodel **modfast, IFuid name) { GENmodel *model = CKTfndMod(ckt, name); if (model) { *modfast = model; return E_EXISTS; } model = (GENmodel *) tmalloc((size_t) *(DEVices[type]->DEVmodSize)); if (!model) return E_NOMEM; model->GENmodType = type; model->GENmodName = name; model->GENnextModel = ckt->CKThead[type]; ckt->CKThead[type] = model; nghash_insert(ckt->MODnameHash, name, model); *modfast = model; return OK; }
char *get_token_t(char *s, char *t) { char *ptr0, *ptr1, *ptr; /* skip all leading characters of s found in string t */ ptr0 = s; while (in_charset(*s, t) && *s) s++; if (*s==0) return(NULL); ptr1 = s; /* skip to next character of s found in t, skipping over quoted */ /* portions */ do { if (*s=='"' && !(s!=ptr0 && *(s-1)=='\\')) { s++; while (*s && !(*s=='"' && *(s-1)!='\\') ) s++; if (*s=='"') s++; } else s++; } while (*s && !in_charset(*s, t)); ptr = tmalloc(sizeof(*ptr)*(s-ptr1+1)); strncpy(ptr, ptr1, s-ptr1); ptr[s-ptr1]=0; strcpy_ss(ptr0, s); interpret_escaped_quotes(ptr); return(ptr); }
char *addOuterParentheses(char *arg) { char *ptr; ptr = tmalloc(sizeof(*ptr)*(strlen(arg)+2)); sprintf(ptr, "(%s)", arg); return ptr; }
void *trealloc( void *old_ptr, unsigned long size_of_block ) { void *ptr; static unsigned long total_bytes = 0; if (size_of_block<=0) size_of_block = 4; if (!old_ptr) return(tmalloc(size_of_block)); if (!(ptr=realloc((void*)old_ptr, (unsigned)(size_of_block)))) { printf("error: memory reallocation failure--%lu bytes requested.\n", size_of_block); printf("trealloc() has reallocated %lu bytes previously\n", total_bytes); abort(); } if (fp_trealloc) { fprintf(fp_trealloc, "d:%lx\na:%lx %lu\n", (unsigned long)old_ptr, (unsigned long)ptr, size_of_block); fflush(fp_trealloc); } total_bytes += size_of_block; return(ptr); }
static inline Tree* insert(Tree* t, char* key, unsigned len, void* tag) { Tree* n = 0; t = splay(t, key); if (t && !key_lt(key, t) && !key_gt(key, t)) { /* already in, so update the record. This could break the tree */ t->key = key; t->end = t->key + (len - 1); t->tag = tag; return t; } n = tmalloc(); n->key = key; n->end = key + (len - 1); n->tag = tag; n->right = n->left = 0; if (t) { if (key_lt(key, t)) { n->left = t->left; n->right = t; t->left = 0; } else { n->right = t->right; n->left = t; t->right = 0; } } return n; }
double *KE_cei(double k, double *buffer) { double a0, b0, c0, a1, b1, c1, K, sum, powerOf2; if (!buffer) buffer = tmalloc(sizeof(*buffer)*2); a0 = 1; b0 = sqrt(1-sqr(k)); c0 = k; sum = sqr(c0); powerOf2 = 1; do { /* do two steps of recurrence per pass in the loop */ a1 = (a0+b0)/2; b1 = sqrt(a0*b0); c1 = (a0-b0)/2; sum += sqr(c1)*(powerOf2 *= 2);; a0 = (a1+b1)/2; b0 = sqrt(a1*b1); c0 = (a1-b1)/2; sum += sqr(c0)*(powerOf2 *= 2); } while (fabs(c0)>ceiAccuracy); buffer[0] = K = PI/(2*a0); buffer[1] = K*(1-sum/2); return buffer; }
/* numeric factorization: L is built row-by-row, using: ( ' indicates transpose ) [ A r ] = [ (I-L) ] [ D^(-1) ] [ (I-L)' -s ] [ r' a ] [ -s' 1 ] [ 1/d ] [ 1 ] = [ A (I-L) D^(-1) (-s) ] [ r' s' D^(-1) s + 1/d ] so, if r' is the next row of A, up to but excluding the diagonal, then the next row of L, s', obeys r = - (I-L) D^(-1) s let y = (I-L)^(-1) (-r) then s = D y, and d = 1/(s' y) */ static void factor_numeric(uint n, const uint *Arp, const uint *Aj, const real *A, sparse_cholesky_data *out, uint *visit, real *y) { const uint *Lrp=out->Lrp, *Lj=out->Lj; real *D, *L; uint i; D=out->D=tmalloc(real,n+Lrp[n]); L=out->L=D+n; for(i=0;i<n;++i) { uint p,pe; real a=0; visit[i]=n; for(p=Lrp[i],pe=Lrp[i+1];p!=pe;++p) { uint j=Lj[p]; y[j]=0, visit[j]=i; } for(p=Arp[i],pe=Arp[i+1];p!=pe;++p) { uint j=Aj[p]; if(j>=i) { if(j==i) a=A[p]; break; } y[j]=-A[p]; } for(p=Lrp[i],pe=Lrp[i+1];p!=pe;++p) { uint q,qe,j=Lj[p]; real lij,yj=y[j]; for(q=Lrp[j],qe=Lrp[j+1];q!=qe;++q) { uint k=Lj[q]; if(visit[k]==i) yj+=L[q]*y[k]; } y[j]=yj; L[p]=lij=D[j]*yj; a-=yj*lij; } D[i]=1/a; } }
void store_fitpoint_ctwiss_parameters(MARK *fpt, char *name, long occurence, double betax1, double betax2, double betay1, double betay2, double etax, double etay, double tilt) { long i; double data[7]; static char *suffix[7] = { "betax1", "betax2", "betay1", "betay2", "cetax", "cetay", "tilt" }; static char s[200]; data[0] = betax1; data[1] = betax2; data[2] = betay1; data[3] = betay2; data[4] = etax; data[5] = etay; data[6] = tilt; if (!(fpt->init_flags&16)) { fpt->ctwiss_mem = tmalloc(sizeof(*(fpt->ctwiss_mem))*12); fpt->init_flags |= 16; for (i=0; i<7; i++) { sprintf(s, "%s#%ld.%s", name, occurence, suffix[i]); fpt->ctwiss_mem[i] = rpn_create_mem(s, 0); } } for (i=0; i<7; i++) rpn_store(data[i], NULL, fpt->ctwiss_mem[i]); }
void lincopy(struct dvec *ov, double *newscale, int newlen, struct dvec *oldscale) { struct dvec *v; double *nd; if (!isreal(ov)) { fprintf(cp_err, "Warning: %s is not real\n", ov->v_name); return; } if (ov->v_length < oldscale->v_length) { fprintf(cp_err, "Warning: %s is too short\n", ov->v_name); return; } v = alloc(struct dvec); v->v_name = copy(ov->v_name); v->v_type = ov->v_type; v->v_flags = ov->v_flags; v->v_flags |= VF_PERMANENT; v->v_length = newlen; nd = (double *) tmalloc(newlen * sizeof (double)); if (!ft_interpolate(ov->v_realdata, nd, oldscale->v_realdata, oldscale->v_length, newscale, newlen, 1)) { fprintf(cp_err, "Error: can't interpolate %s\n", ov->v_name); return; } v->v_realdata = nd; vec_new(v); return; }
long randomSampleMin( double *best_result, double *xReturn, double *lower, double *upper, long n_dimen, double target, double (*func)(double *x, long *invalid), long nSamples, double (*random_f)(long iseed) ) { double *x, *xBest; double result; long flag, i, best_found = 0; optimFlags = 0; if (random_f==NULL) random_f = random_1; x = tmalloc(sizeof(*x)*n_dimen); xBest = tmalloc(sizeof(*xBest)*n_dimen); for (i=0; i<n_dimen; i++) xBest[i] = xReturn[i]; *best_result = DBL_MAX; while (nSamples--) { for (i=0; i<n_dimen; i++) x[i] = lower[i] + (upper[i]-lower[i])*(*random_f)(0); if ((result=(*func)(x, &flag)) < *best_result && flag==0) { *best_result = result; for (i=0; i<n_dimen; i++) xBest[i] = x[i]; best_found = 1; if (result<target) break; } if (optimFlags&OPTIM_ABORT) break; } if (best_found) { for (i=0; i<n_dimen; i++) xReturn[i] = xBest[i]; } free(x); free(xBest); return(best_found); }
void *array_1d(long size, long lower_index, long upper_index) { char *ptr; if (!(ptr=tmalloc((unsigned)size*(upper_index-lower_index+1)))) bomb("unable to allocate array (array_1d)", NULL); ptr -= lower_index*size; return((void*)ptr); }
int initialise_wd_fsa_cos (fsa *wd_fsaptr, srec *alphptr, int maxwdiffs) /* Initialise a word-difference automaton, using *alphptr as base-alphabet. * First state is empty word, which is initial and only accepting state. * In the cosets situation, there can be more than one initial state. */ { int i, **table; fsa_init(wd_fsaptr); wd_fsaptr->states->type = WORDS; tmalloc(wd_fsaptr->states->words,gen *,maxwdiffs+1); wd_fsaptr->states->alphabet_size = alphptr->size; for (i=1;i<=alphptr->size;i++) { tmalloc(wd_fsaptr->states->alphabet[i],char,stringlen(alphptr->names[i])+1); strcpy(wd_fsaptr->states->alphabet[i],alphptr->names[i]); } wd_fsaptr->states->size=1; /* Set up first state, which is the empty word. */ tmalloc(wd_fsaptr->states->words[1],gen,1); wd_fsaptr->states->words[1][0] = 0; wd_fsaptr->alphabet->type = PRODUCT; wd_fsaptr->alphabet->size = (alphptr->size+1)*(alphptr->size+1) - 1; wd_fsaptr->alphabet->arity = 2; wd_fsaptr->alphabet->padding = '_'; tmalloc(wd_fsaptr->alphabet->base,srec,1); srec_copy(wd_fsaptr->alphabet->base,alphptr); wd_fsaptr->num_accepting = 1; tmalloc(wd_fsaptr->accepting,int,2); wd_fsaptr->accepting[1] = 1; wd_fsaptr->flags[TRIM] = TRUE; wd_fsaptr->flags[MIDFA] = TRUE; fsa_table_init(wd_fsaptr->table,maxwdiffs,wd_fsaptr->alphabet->size); table = wd_fsaptr->table->table_data_ptr; for (i=1;i<=wd_fsaptr->alphabet->size;i++) set_dense_target(table,i,1,0); wd_fsaptr->table->printing_format = SPARSE; if (fsa_table_dptr_init(wd_fsaptr)==-1) return -1; return 0; }
/* This function will parse a tconfig "module" block, and attempt * to load the module */ struct tmodule *tmodule_from_tconfig(struct tconfig_block *tcfg, struct tconfig_block *global_cfg) { lt_dlhandle ltmodule = NULL; struct tmodule *module = NULL; int (*tmodule_load)(struct tconfig_block *, struct tconfig_block *); if (strcmp(tcfg->key, "module")) return NULL; /* Search modules directory */ lt_dlsetsearchpath("modules"); ltmodule = lt_dlopenext(tcfg->value); if (ltmodule == NULL) { troll_debug(LOG_WARN, "Could not open module \"%s\" (Reason: %s)", tcfg->value, lt_dlerror()); return NULL; } troll_debug(LOG_DEBUG, "Loaded module \"%s\"", tcfg->value); if ((tmodule_load = lt_dlsym(ltmodule, "tmodule_load")) == NULL) { troll_debug(LOG_WARN, "Could not find symbol tmodule_load in module \"%s\"", tcfg->value); lt_dlclose(ltmodule); } else { /* We praise our magnificient overlords */ if (!(*tmodule_load)(tcfg->child, global_cfg)) { troll_debug(LOG_WARN, "Init function for module \"%s\" failed", tcfg->value); lt_dlclose(ltmodule); return NULL; } } module = tmalloc(sizeof(struct tmodule)); module->name = tstrdup(tcfg->value); module->handle = ltmodule; /* Loading mechanism */ module->tmodule_load = lt_dlsym(ltmodule, "tmodule_load"); module->tmodule_unload = lt_dlsym(ltmodule, "tmodule_unload"); /* Socket system */ module->tmodule_get_tsockets = lt_dlsym(ltmodule, "tmodule_get_tsockets"); /* Messaging system */ module->tmodule_get_messages = lt_dlsym(ltmodule, "tmodule_get_messages"); module->tmodule_handle_messages = lt_dlsym(ltmodule, "tmodule_handle_messages"); return module; }
void udn_int_print_val(PRINT_VAL_ARGS) { int *int_struct = STRUCT_PTR; /* Allocate space for the printed value */ PRINT_VAL = tmalloc(30); /* Print the value into the string */ sprintf(PRINT_VAL, "%8d", *int_struct); }
/* Constructor */ struct http_data *http_data_new(void) { struct http_data *local; local = tmalloc(sizeof(struct http_data)); local->txt_packet = NULL; return local; }
cDict *dict_prep(cDict *dict) { cDict *cnew; if (dict->refs == 1) return dict; /* Duplicate the old dictionary. */ cnew = tmalloc(sizeof(cDict)); cnew->keys = list_dup(dict->keys); cnew->values = list_dup(dict->values); cnew->hashtab_size = dict->hashtab_size; cnew->links = tmalloc(sizeof(Int) * cnew->hashtab_size); cnew->hashtab = tmalloc(sizeof(Int) * cnew->hashtab_size); MEMCPY(cnew->links, dict->links, cnew->hashtab_size); MEMCPY(cnew->hashtab, dict->hashtab, cnew->hashtab_size); dict->refs--; cnew->refs = 1; return cnew; }
char *makeFrequencyUnits(SDDS_DATASET *SDDSin, char *indepName) { char *timeUnits; char *units; long reciprocal = 0, end; if (SDDS_GetColumnInformation(SDDSin, "units", &timeUnits, SDDS_GET_BY_NAME, indepName)!=SDDS_STRING) return 0; if (timeUnits) { while (1) { end = strlen(timeUnits) - 1; if (timeUnits[0]=='(' && timeUnits[end]==')') { timeUnits[end] = 0; strslide(timeUnits, 1); } else if (timeUnits[0]=='1' && timeUnits[1]=='/' && timeUnits[2]=='(' && timeUnits[end]==')') { timeUnits[end] = 0; strslide(timeUnits, 3); reciprocal = !reciprocal; } else break; } } if (!timeUnits || SDDS_StringIsBlank(timeUnits)) { units = tmalloc(sizeof(*units)*1); units[0] = 0; return units; } if (reciprocal) { if (!SDDS_CopyString(&units, timeUnits)) return NULL; return units; } units = tmalloc(sizeof(*units)*(strlen(timeUnits)+5)); if (strchr(timeUnits, ' ')) sprintf(units, "1/(%s)", timeUnits); else sprintf(units, "1/%s", timeUnits); return units; }
/* @builtin-bind { 'd', builtin_defun }, */ int builtin_defun(interpreter* interp) { string name, body; byte n1; long_command* curr; if (!stack_pop_strings(interp, 2, &body, &name)) UNDERFLOW; /* If name is one character, it is a short name; if it is more than one * character, it is a long name. An empty string is an error. */ if (!name->len) { print_error("Empty command name"); goto error; } if (name->len == 1) { n1 = string_data(name)[0]; /* Both pointers are in the same spot, so check either */ if (interp->commands[n1].cmd.native) { print_error_s("Short command already exists", name); goto error; } else { /* OK */ interp->commands[n1].is_native = 0; interp->commands[n1].cmd.user = body; free(name); } } else { /* Long command name. * First, check to see if it already exists. */ for (curr = interp->long_commands; curr; curr = curr->next) { if (string_equals(name, curr->name)) { print_error_s("Long command already exists", name); goto error; } } /* OK, add it */ curr = tmalloc(sizeof(long_command)); curr->name = name; curr->cmd.is_native = 0; curr->cmd.cmd.user = body; curr->next = interp->long_commands; interp->long_commands = curr; } return 1; error: /* Restore the stack and return failure */ stack_push(interp, name); stack_push(interp, body); return 0; }
void *tcalloc(unsigned nelem, unsigned elsize) { void *vp; unsigned nbytes; nbytes = nelem * elsize; if ( (vp = tmalloc(nbytes)) == NULL) return NULL; memset(vp, '\0', nbytes); return vp; }