/* * Create a new thread-safe queue of size sz. */ thrqueue_t * thrqueue_new(size_t sz) { thrqueue_t *queue; if (!(queue = umalloc(sizeof(thrqueue_t)))) goto out0; if (!(queue->data = umalloc(sz * sizeof(void*)))) goto out1; if (0 != pthread_mutex_init(&queue->mutex, NULL)) goto out2; if (0 != pthread_cond_init(&queue->notempty, NULL)) goto out3; if (0 != pthread_cond_init(&queue->notfull, NULL)) goto out4; queue->sz = sz; queue->n = 0; queue->in = 0; queue->out = 0; queue->block_enqueue = 1; queue->block_dequeue = 1; return queue; out4: pthread_cond_destroy(&queue->notempty); out3: pthread_mutex_destroy(&queue->mutex); out2: free(queue->data); out1: free(queue); out0: return NULL; }
/*--------------------------------------------------------------------*\ | FUNCTION : djulian_ | COMMENT : | PARAMETERS : | RETURN VALUE : | RESTRICTIONS : \*--------------------------------------------------------------------*/ double djulian_ (char *jwhen, char *jtype, ftnlen jwhenlen, ftnlen jtypelen) { char *when, *type; double retval; /* * copy strings and terminate */ when = (char *) umalloc(jwhenlen + 1); strncpy(when, jwhen, jwhenlen); when[jwhenlen] = '\0'; type = (char *) umalloc(jtypelen + 1); strncpy(type, jtype, jtypelen); type[jtypelen] = '\0'; /* * call C version of djulian() */ retval = djulian(when, type); /* * free up arrays */ //ufree(when); //ufree(type); return retval; }
static GIFmetafile *init_meta(mf_cgmo *cgmo, int sx, int sy) { GIFmetafile *newmeta = (GIFmetafile *)umalloc(sizeof(GIFmetafile)); assert(newmeta); newmeta->image = gdImageCreate(sx, sy); newmeta->sx = sx; newmeta->sy = sy; newmeta->colors = (GIFcolor *)umalloc(sizeof(GIFcolor) * cgmo->ws->wscolour); newmeta->lineIndex = 1; newmeta->lineWidth = 1; newmeta->fillIndex = 1; newmeta->styleIndex = 1; newmeta->fillStyleIndex = 1; newmeta->style = 0; newmeta->resize = 1; /* OK to resize */ newmeta->ws = cgmo->ws; init_colors(newmeta, cgmo->ws->wscolour); /* Allocate colors -- background white, foreground black */ allocate_color(newmeta, 0, 1, 1, 1); allocate_color(newmeta, 1, 0, 0, 0); return newmeta; }
void draw_help_button(si32 n, ui32 background) { static si32 first_call = TRUE; static char **button_label; char *label_str; int i; gframe_t *frame; if (Glob->debug) { fprintf(stderr, "** draw_help_button **\n"); } frame = Glob->help_button_frame[n]; /* * if first call, load up the label strings */ if (first_call) { button_label = (char **) umalloc (N_HELP_BUTTONS * sizeof(char *)); label_str = (char *) umalloc((ui32) (strlen(HELP_BUTTON_LABELS) + 1)); strcpy(label_str, HELP_BUTTON_LABELS); for (i = 0; i < N_HELP_BUTTONS; i++) { if (i == 0) button_label[i] = strtok(label_str, " "); else button_label[i] = strtok((char *) NULL, " "); } first_call = FALSE; } /* * clear window and set background */ XSetWindowBackground(Glob->rdisplay, frame->x->drawable, background); safe_XClearWindow(Glob->rdisplay, frame->x->drawable); /* * write the label to the window */ GDrawString(XDEV, frame, frame->x->gc, frame->x->font, frame->psgc, XJ_CENTER, YJ_CENTER, (double) 0.5, (double) 0.5, button_label[n]); safe_XFlush(Glob->rdisplay); }
type *type_uptree_find_or_new( type *to, enum type_kind idx, int (*eq)(type *, void *), void (*init)(type *, void *), void *ctx) { struct type_tree_ent **ent; to = type_skip_wheres(to); if(!to->uptree) to->uptree = umalloc(sizeof *to->uptree); for(ent = &to->uptree->ups[idx]; *ent; ent = &(*ent)->next){ type *candidate = (*ent)->t; assert(candidate->type == idx); if(eq(candidate, ctx)) return candidate; } { type *new_t = type_new(idx, to); if(init) init(new_t, ctx); *ent = umalloc(sizeof **ent); (*ent)->t = new_t; return new_t; } }
void alloc_morphology_grids(void) { static int first_call = TRUE; static si32 nbytes_alloc; si32 nbytes_needed; nbytes_needed = Glob->nx * Glob->ny * sizeof(ui08); if (first_call) { Refl_margin_grid = (ui08 *) umalloc ((ui32) nbytes_needed); Morphology_grid = (ui08 *) umalloc ((ui32) nbytes_needed); Edm_grid = (ui08 *) umalloc ((ui32) nbytes_needed); Eroded_grid = (ui08 *) umalloc ((ui32) nbytes_needed); nbytes_alloc = nbytes_needed; first_call = FALSE; } else { /* * adjust grid allocation as necessary */ if (nbytes_needed < nbytes_alloc) { Refl_margin_grid = (ui08 *) urealloc ((char *) Refl_margin_grid, (ui32) nbytes_needed); Morphology_grid = (ui08 *) urealloc ((char *) Morphology_grid, (ui32) nbytes_needed); Edm_grid = (ui08 *) urealloc ((char *) Edm_grid, (ui32) nbytes_needed); Eroded_grid = (ui08 *) urealloc ((char *) Eroded_grid, (ui32) nbytes_needed); nbytes_alloc = nbytes_needed; } /* if (nbytes_needed < nbytes_alloc) */ } /* if (first_call) */ /* * zero out grids */ memset((void *) Refl_margin_grid, 0, (int) nbytes_alloc); memset((void *) Morphology_grid, 0, (int) nbytes_alloc); memset((void *) Edm_grid, 0, (int) nbytes_alloc); memset((void *) Eroded_grid, 0, (int) nbytes_alloc); return; }
/* * new_hscprc * * create and init a new hsc process */ HSCPRC *new_hscprc(void) { HSCPRC *hp = NULL; hp = (HSCPRC *) umalloc(sizeof(HSCPRC)); if (hp) { memset(hp, 0, sizeof(HSCPRC)); /* init lists */ hp->defent = init_dllist(del_entity); hp->deftag = init_hsctree(free_tag_node, cmp_tag_node, ubi_trOVERWRITE); hp->defattr = init_dllist(del_hscattr); hp->deflazy = init_hsctree(free_tag_node, cmp_tag_node, ubi_trOVERWRITE); hp->defstyle = init_hsctree(free_style_node, cmp_style_node, ubi_trOVERWRITE); hp->container_stack = init_dllist(del_hsctag); hp->content_stack = init_dllist(del_string_node); hp->inpf_stack = init_dllist(del_inpf_stack_node); hp->project = NULL; hp->idrefs = init_dllist(del_idref); hp->select_stack = init_dllist(del_select_stack_node); hp->tag_styles = init_dllist(&del_styleattr); hp->include_dirs = init_strlist(); /* precompile some regular expressions */ hp->re_uri = hscregcomp(hp, REGEXP_URI, TRUE, TRUE); /* init strings */ hp->destdir = init_estr(0); hp->reldir = init_estr(0); hp->iconbase = init_estr(0); hp->server_dir = init_estr(0); hp->if_stack = init_estr(0); hp->tag_name_str = init_estr(128); hp->tag_attr_str = init_estr(128); hp->tag_close_str = init_estr(0); hp->tmpstr = init_estr(0); hp->curr_msg = init_estr(64); hp->curr_ref = init_estr(64); hp->whtspc = init_estr(0); /* allocate message arrays */ hp->msg_ignore = (HSCIGN *) umalloc((MAX_MSGID + 1) * sizeof(HSCIGN)); hp->msg_class = (HSCMSG_CLASS *) umalloc((MAX_MSGID + 1) * sizeof(HSCMSG_CLASS)); /* allocate image buffer */ hp->image_buffer = (unsigned char *) umalloc(IMAGE_BUFFER_SIZE); reset_hscprc(hp); } return (hp); }
/* * new_hscprc * * create and init a new hsc process */ HSCPRC *new_hscprc(void) { HSCPRC *hp = NULL; hp = (HSCPRC *) umalloc(sizeof(HSCPRC)); if (hp) { memset(hp, 0, sizeof(HSCPRC)); /* init lists */ hp->defent = init_dllist(del_entity); hp->deftag = init_dllist(del_hsctag); hp->defattr = init_dllist(del_hscattr); hp->container_stack = init_dllist(del_hsctag); hp->content_stack = init_dllist(del_string_node); hp->inpf_stack = init_dllist(del_inpf_stack_node); hp->project = NULL; hp->idrefs = init_dllist(del_idref); hp->select_stack = init_dllist(del_select_stack_node); hp->include_dirs = init_strlist(); /* init strings */ hp->destdir = init_estr(0); hp->reldir = init_estr(0); hp->iconbase = init_estr(0); hp->server_dir = init_estr(0); hp->if_stack = init_estr(0); hp->tag_name_str = init_estr(128); hp->tag_attr_str = init_estr(128); hp->tag_close_str = init_estr(0); hp->tmpstr = init_estr(0); hp->curr_msg = init_estr(64); hp->curr_ref = init_estr(64); hp->whtspc = init_estr(0); #if 0 /* TODO:remove */ hp->filename_project = NULL; hp->filename_document = NULL; #endif /* alloc message arrays */ hp->msg_ignore = (BOOL *) umalloc((MAX_MSGID + 1) * sizeof(BOOL)); hp->msg_class = (HSCMSG_CLASS *) umalloc((MAX_MSGID + 1) * sizeof(HSCMSG_CLASS)); reset_hscprc(hp); } return (hp); }
int CClp_init(CClp **lp) { /* INITIALIZES the LP. */ CClp_free(lp); (*lp) = umalloc(sizeof(CClp)); (*lp)->lp = NULL; return 0; }
struct list *list_new(void *d) { struct list *l = umalloc(sizeof(*l)); l->data = d; l->next = l->prev = NULL; return l; }
int b_read(int fd, char* buf, int len) { if (buf_load==0) { buf_load = umalloc( BUFFER_FOR_READ_SIZE ); if (buf_load==0) return 0; } int loaded=0, now=1; while (now && loaded<len) { now = len-loaded; if ( now > BUFFER_FOR_READ_SIZE ) now = BUFFER_FOR_READ_SIZE; now = read(fd,buf_load,now); memcpy(buf+loaded, buf_load, now); loaded+=now; } return loaded; }
char *out_label_goto(char *func, char *lbl) { int len = strlen(func) + strlen(lbl) + 6; char *ret = umalloc(len); SNPRINTF(ret, len, ASM_PLBL_PRE "%s.%s", func, lbl); return ret; }
/* * Open an output GIF file. */ int GIFmoOpen(WS_STATE_PTR ws) { /* Output file not opened or written to until flushed */ Gint status = 1; initLogFlag(); assert(ws != NULL); assert(ws->wscolour <= gdMaxColors); if ((ws->mf.cgmo = (mf_cgmo*)umalloc(sizeof(mf_cgmo))) != NULL) { mf_cgmo *cgmo = ws->mf.cgmo; cgmo->ws = ws; if (find_meta(cgmo) != 0){ msgWarn("GIFmoOpen:metafile already open\n"); } else { create_meta(cgmo, DefaultSize, DefaultSize); cgmo->ws = ws; cgmo->type = MF_GIF; unlink(ws->conn); status = OK; } } return status; }
/* ** new_hscvar ** ** alloc & init a new variable */ HSCVAR *new_hscvar( STRPTR newname ) { HSCVAR *newvar = (HSCVAR*) umalloc( sizeof(HSCVAR) ); if (newvar) { /* init new varument item */ newvar->vartype = VT_NONE; newvar->name = strclone( newname ); newvar->deftext = NULL; newvar->text = NULL; newvar->enumstr = NULL; newvar->macro_id = 0; newvar->varflag = 0; newvar->quote = EOF; } if ( !( newvar->name ) ) { del_var( (APTR) newvar ); newvar = NULL; } return (newvar); }
tab *tab_new(window *win) { tab *t = umalloc(sizeof *t); t->win = win; t->next = NULL; return t; }
char *str_quote(char *quoteme, int free_in) { int len; const char *s; char *ret, *p; len = 3; /* ""\0 */ for(s = quoteme; *s; s++, len++) switch(*s){ case '"': case '\\': len++; } p = ret = umalloc(len); *p++ = '"'; for(s = quoteme; *s; s++){ switch(*s){ case '"': case '\\': *p++ = '\\'; } *p++ = *s; } strcpy(p, "\""); if(free_in) free(quoteme); return ret; }
char *out_label_case(enum out_label_type lbltype, int val) { int len; char *ret = umalloc(len = 15 + 32); switch(lbltype){ case CASE_DEF: SNPRINTF(ret, len, ASM_PLBL_PRE "case_%d_default", switch_last); break; case CASE_CASE: case CASE_RANGE: { const char *extra = ""; if(val < 0){ val = -val; extra = "m"; } SNPRINTF(ret, len, ASM_PLBL_PRE "case%s_%d_%s%d", lbltype == CASE_RANGE ? "_rng" : "", switch_last, extra, val); break; } } switch_last++; return ret; }
static GIFmetafile *copy_meta(mf_cgmo *cgmo, GIFmetafile *old, int sx, int sy) { GIFmetafile *newmeta = init_meta(cgmo, sx, sy); int i; /* Copy various stuff -- don't copy resize! */ newmeta->lineIndex = old->lineIndex; newmeta->lineWidth = old->lineWidth; newmeta->fillIndex = old->fillIndex; newmeta->styleIndex = old->styleIndex; newmeta->fillStyleIndex = old->fillStyleIndex; newmeta->style = (int *)umalloc(sizeof(int) * MaxLineStyleLength); memcpy(newmeta->style, old->style, sizeof(int)*MaxLineStyleLength); newmeta->ws = old->ws; /* Copy the color info */ for (i=0; i < old->numcolors; ++i){ GIFcolor *color = &old->colors[i]; if (old->colors[i].index == -1) break; allocate_color(newmeta, i, color->r, color->g, color->b); } /* Destroy the old metafile */ destroy_meta(cgmo, old); /* Add this one */ add_meta(cgmo, newmeta); return newmeta; }
static void set_lineStyle(GIFmetafile *meta, Gint attr, Gasf type) { if (type != xgks_state.gks_lnattr.type) return; if (type == GBUNDLED){ attr = meta->ws->lnbundl_table[attr].type; } msgInfo("set_lineStyle: setting style to %d\n", attr); if (attr <= 0 || attr > 4) attr = 1; meta->styleIndex = attr; { int currColor = meta->lineIndex; int i, length; char *cp = LineStyles[attr]; length = strlen(cp); assert(currColor != -1); if (meta->style) free(meta->style); meta->style = (int *)umalloc(sizeof(int) * MaxLineStyleLength); length = strlen(LineStyles[attr]); for (i=0; i < length; ++i){ if (cp[i] == '-'){ meta->style[i] = currColor; } else { meta->style[i] = gdTransparent; } } gdImageSetStyle(meta->image, meta->style, length); } }
char *str_quote(const char *quoteme) { int len; const char *s; char *ret, *p; len = 3; /* ""\0 */ for(s = quoteme; *s; s++, len++) if(*s == '"') len++; p = ret = umalloc(len); *p++ = '"'; for(s = quoteme; *s; s++){ if(*s == '"') *p++ = '\\'; *p++ = *s; } strcpy(p, "\""); return ret; }
void escape_string(char *old_str, int *plen) { const int old_len = *plen; char *new_str = umalloc(*plen); int new_i, i; /* "parse" into another string */ for(i = new_i = 0; i < old_len; i++){ char add; if(old_str[i] == '\\'){ int len; i++; if(escape_multi_char(old_str + i, &add, &len)){ i += len - 1; }else{ add = escape_char(old_str[i]); if(add == -1) DIE_AT(NULL, "unknown escape char '\\%c'", add); } }else{ add = old_str[i]; } new_str[new_i++] = add; } memcpy(old_str, new_str, new_i); *plen = new_i; free(new_str); }
tree_vertex_t *alloc_vertices(si32 n_vertices) { static int n_alloc = 0; static tree_vertex_t *vertices; if (n_vertices > n_alloc) { if (vertices == NULL) { vertices = (tree_vertex_t *) umalloc ((ui32) (n_vertices * sizeof(*vertices))); } else { vertices = (tree_vertex_t *) urealloc ((char *) vertices, (ui32) (n_vertices * sizeof(*vertices))); } n_alloc = n_vertices; } return (vertices); }
//------------------------------------------------------------------- void conf_save() { static const long t=CONF_MAGICK_VALUE; register int i; int fd; char *buf=umalloc(sizeof(t)+CONF_NUM*(sizeof(conf_info[0].id)+sizeof(conf_info[0].size))+sizeof(conf)); char *p=buf; fd = open(CONF_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0777); if (fd>=0){ memcpy(p, &t, sizeof(t)); p+=sizeof(t); for (i=0; i<CONF_NUM; ++i) { memcpy(p, &(conf_info[i].id), sizeof(conf_info[i].id)); p+=sizeof(conf_info[i].id); memcpy(p, &(conf_info[i].size), sizeof(conf_info[i].size)); p+=sizeof(conf_info[i].size); memcpy(p, conf_info[i].var, conf_info[i].size); p+=conf_info[i].size; } write(fd, buf, p-buf); close(fd); } ufree(buf); }
char *out_label_flow(const char *fmt) { int len = 16 + strlen(fmt); char *ret = umalloc(len); SNPRINTF(ret, len, ASM_PLBL_PRE "flow_%s_%d", fmt, flow_last++); return ret; }
static type *type_new(enum type_kind t, type *of) { type *r = umalloc(sizeof *r); r->type = t; r->ref = of; return r; }
/* * new_hsctag * * alloc & init a new hsctag */ HSCTAG *new_hsctag(STRPTR newid) { HSCTAG *newtag = (HSCTAG*) umalloc(sizeof(HSCTAG)); if (newtag) { /* init new tag item */ newtag->name = upstr(strclone(newid)); /* set id */ newtag->option = 0; newtag->o_handle = NULL; /* no handle functions */ newtag->c_handle = NULL; newtag->occured = FALSE; newtag->op_text = NULL; newtag->cl_text = NULL; newtag->attr = init_dllist(del_hscattr); newtag->mbi = NULL; newtag->naw = NULL; newtag->uri_size = NULL; newtag->uri_stripext = NULL; newtag->start_fpos = NULL; newtag->end_fpos = NULL; if (!(newtag->name && newtag->attr)) { del_hsctag(newtag); newtag = NULL; } } return (newtag); }
INV *inv_create(int m, int max_upd) { INV *inv; int k; if (m < 1) fault("inv_create: m = %d; invalid parameter", m); if (max_upd < 0) fault("inv_create: max_upd = %d; invalid parameter", max_upd); inv = umalloc(sizeof(INV)); inv->m = m; inv->valid = 1; inv->luf = luf_create(m, 0); inv->hh_max = max_upd; inv->hh_nfs = 0; inv->hh_ndx = ucalloc(1+max_upd, sizeof(int)); inv->hh_ptr = ucalloc(1+max_upd, sizeof(int)); inv->hh_len = ucalloc(1+max_upd, sizeof(int)); inv->p0_row = ucalloc(1+m, sizeof(int)); inv->p0_col = ucalloc(1+m, sizeof(int)); for (k = 1; k <= m; k++) inv->p0_row[k] = inv->p0_col[k] = k; inv->cc_len = -1; inv->cc_ndx = ucalloc(1+m, sizeof(int)); inv->cc_val = ucalloc(1+m, sizeof(double)); #if 0 inv->upd_tol = 1e-12; #else inv->upd_tol = 1e-6; #endif inv->nnz_h = 0; return inv; }
int CClp_sread_warmstart(CC_SFILE *file, CClp_warmstart **warm) { /* READS warmstart information from the file. */ int i, j, ccount, rcount; char name[5]; CClp_free_warmstart(warm); for (i = 0; i < 4; i++) if (CCutil_sread_char(file, &name[i])) goto fail; name[4] = '\0'; if (strncmp(name, SOLVER, 4)) { print("CClp_sread_warmstart: warmstart for another solver (%s)" " ignored", name); return 0; } if (CCutil_sread_int(file, &ccount)) goto fail; if (CCutil_sread_int(file, &rcount)) goto fail; (*warm) = umalloc(sizeof(CClp_warmstart)); (*warm)->ncols = 0; (*warm)->nrows = 0; (*warm)->cstat = NULL; (*warm)->rstat = NULL; (*warm)->cstat = ucalloc(ccount, sizeof(int)); (*warm)->rstat = ucalloc(rcount, sizeof(int)); for (j = 0; j < ccount; j++) if (CCutil_sread_bits(file, &(((*warm)->cstat)[j]), 2)) goto fail; for (i = 0; i < rcount; i++) if (CCutil_sread_bits(file, &(((*warm)->rstat)[i]), 1)) goto fail; (*warm)->ncols = ccount; (*warm)->nrows = rcount; return 0; fail: CClp_free_warmstart(warm); return 1; }
LPP *lpp_create_wksp(void) { LPP *lpp; lpp = umalloc(sizeof(LPP)); lpp->orig_m = 0; lpp->orig_n = 0; lpp->orig_nnz = 0; lpp->orig_dir = LPX_MIN; lpp->nrows = 0; lpp->ncols = 0; lpp->row_pool = dmp_create_pool(sizeof(LPPROW)); lpp->col_pool = dmp_create_pool(sizeof(LPPCOL)); lpp->aij_pool = dmp_create_pool(sizeof(LPPAIJ)); lpp->row_ptr = NULL; lpp->col_ptr = NULL; lpp->row_que = NULL; lpp->col_que = NULL; lpp->c0 = 0.0; lpp->tqe_pool = dmp_create_pool(0); lpp->tqe_list = NULL; lpp->m = 0; lpp->n = 0; lpp->nnz = 0; lpp->row_ref = NULL; lpp->col_ref = NULL; lpp->row_stat = NULL; lpp->row_prim = NULL; lpp->row_dual = NULL; lpp->col_stat = NULL; lpp->col_prim = NULL; lpp->col_dual = NULL; return lpp; }
symtable *symtab_new(symtable *parent) { symtable *p = umalloc(sizeof *p); if(parent) symtab_set_parent(p, parent); return p; }