/// Deallocate dvar_vector memory. void dvar_vector::deallocate() { if (shape) { #ifdef DIAG cout << " Deallocating dvar_vector with ptr_address\n " << &ptr << " pointing at " << (ptr+indexmin()) << "\n"; #endif if (shape->ncopies > 0) { --(shape->ncopies); } else { va = (double_and_int*) shape->trueptr; *(arr_link**) va = link_ptr; arr_free(va); delete shape; } allocate(); } #ifdef DEBUG else { cerr << "Warning -- Unable to deallocate an unallocated dvar_vector.\n"; } #endif }
void id_item_default_release(id_item_t *item) { assert(item != NULL); switch (item->type) { case ID_VAR: if (item->var != NULL) { var_clear(item->var); ufree(item->var); item->var = NULL; } break; case ID_ARR: if (item->arr != NULL) { arr_free(item->arr); item->arr = NULL; } break; case ID_UNKNOWN: default: error(1, "unknown id type\n"); break; } }
int test_uarr(){ int ret = 0; write_cstr(1, ">>> test_uarr >>>\n"); int l; /* test insert & append */ struct arr *msg = arr_alloc(16); struct uarr *p = uarr_alloc(4, sizeof(int)); int iarr[] = {1,2,3,4}; uarr_insert(p, uarr_count(p), &iarr[0], 1); uarr_insert(p, uarr_count(p), &iarr[1], 1); uarr_append(p, &iarr[2]); uarr_append(p, &iarr[3]); int *ip = (int *)p->v; l = uarr_count(p); while(l--){ arr_append_int_str(msg, *ip); arr_insert(msg, msg->c, ",", 1); ip++; } ret |= test_print_eq(msg, "1,2,3,4,", "uarr 1"); arr_free(msg); uarr_free(p); write(1, "\n", 1); /* test append */ struct arr *msg2 = arr_alloc(16); struct uarr *p2 = uarr_alloc(4, sizeof(int)); int iarr2[] = {1,2,3,4}; uarr_append(p2, &iarr2[0]); uarr_append(p2, &iarr2[1]); uarr_append(p2, &iarr2[2]); uarr_append(p2, &iarr2[3]); int *ip2 = (int *)p2->v; l = uarr_count(p2); while(l--){ arr_append_int_str(msg2, *ip2); arr_insert(msg2, msg2->c, ",", 1); ip2++; } ret |= test_print_eq(msg2, "1,2,3,4,", "uarr 2"); arr_free(msg2); uarr_free(p2); write(1, "\n", 1); ret |= test_uarr_ptr(); return ret; }
void compile_object_simple(compiler_wrapper *cw, ast_object_decl_node *node) { int ct = 0; ast_node *list = node->payload; arraylist names = arr_create(20); for(; list; list = list->next) { arr_append(&names, ((ast_value_node *)list)->value.s); list = list->next; compile(cw, list); ct++; } if(node->obj) compile(cw, node->obj); else append_op(cw, LI_PUSH_NEW_OBJECT, node->lineno); unsigned char buf[4]; int_to_byte_array(buf, ct); append_op(cw, LI_MAKE_OBJECT, node->lineno); append_op(cw, buf[0], node->lineno); append_op(cw, buf[1], node->lineno); append_op(cw, buf[2], node->lineno); append_op(cw, buf[3], node->lineno); int i; for(i = ct - 1; i >= 0; i--) { char *sid = arr_get(&names, i); char *nsid = malloc(strlen(sid) + 1); strcpy(nsid, sid); int i = find_prev_name(cw, nsid); if(i < 0) { i = (int)cw->rnames.count; arr_append(&cw->rnames, nsid); } unsigned char buf[4]; int_to_byte_array(buf, i); append_op(cw, buf[0], node->lineno); append_op(cw, buf[1], node->lineno); append_op(cw, buf[2], node->lineno); append_op(cw, buf[3], node->lineno); } arr_free(&names); }
inline optional init_sym() { table *t; optional opt; if ((t = malloc(sizeof(table))) == null) { opt.e = false; opt.err = malloc_fail; return opt; } opt = arr_init(32); if (!opt.e) { free(t); return opt; } t->syms = opt.val; opt = arr_init(32); if (!opt.e) { arr_free(t->syms); free(t); return opt; } t->lits = opt.val; opt = arr_init(32); if (!opt.e) { arr_free(t->syms); arr_free(t->lits); free(t); return opt; } t->tmps = opt.val; opt.val = t; return opt; }
void vs_pop(VarScopeStack *vs) { VarScope *s = vs->scope; if(!s) return; vs->scope = s->next; if(s->scope == SCOPE) { if(vs->warnunused) hst_for_each(&s->table, vs_run_warnings, NULL); hst_free(&s->table); arr_free(&s->deferments); } free(s); }
int test_uarr_ptr(){ int ret = 0, l, ip; struct arr *msg = arr_alloc(16); struct uarr *p = uarr_alloc(2, sizeof(char *)); char *carr[] = {"hi","there","alpha","beta"}; uarr_append(p, &carr[0]); uarr_append(p, &carr[1]); uarr_append(p, &carr[2]); uarr_append(p, &carr[3]); char **cp = (char **)p->v; l = uarr_count(p); while(l--){ arr_insert(msg, msg->c, *cp, strlen(*cp)); arr_insert(msg, msg->c, ",", 1); cp++; } ret |= test_print_eq(msg, "hi,there,alpha,beta,", "uarr ptr 1"); arr_free(msg); uarr_free(p); write(1, "\n", 1); return ret; }
/** * Description not yet available. * \param */ void dvar_vector::deallocate() { if (shape) { #ifdef DIAG cout << " Deallocating dvar_vector with ptr_address\n " << &ptr << " pointing at " << (ptr+indexmin()) << "\n"; #endif if (shape->ncopies) { (shape->ncopies)--; } else { va = (double_and_int*) shape->trueptr; * (arr_link **) va = link_ptr; arr_free(va); delete shape; } va=NULL; shape=NULL; } }
void list_mempool_free(void *datum) { Arraylist *list = datum; arr_free(list); }
void compile_object(compiler_wrapper *cw, ast_node *n) { ast_object_decl_node *node = (ast_object_decl_node *)n; if(!node->refname) return compile_object_simple(cw, node); arraylist name_list = cw->used_names; int i; for(i = 0; i < name_list.count; i++) { name_wrapper *w = arr_get(&cw->used_names, i); if(strcmp(w->name, node->refname)) continue; switch_to_close(cw, node->refname, w->idx); } int ct = 0; ast_node *list = node->payload; arraylist names = arr_create(20); for(; list; list = list->next) { arr_append(&names, ((ast_value_node *)list)->value.s); list = list->next; compile(cw, list); ct++; } char *nsid = malloc(strlen(node->refname) + 1); strcpy(nsid, node->refname); i = find_prev_name(cw, nsid); if(i < 0) { i = (int)cw->rnames.count; arr_append(&cw->rnames, nsid); } append_op(cw, LI_LOAD_CLOSE, node->lineno); unsigned char buf[4]; int_to_byte_array(buf, i); append_op(cw, buf[0], node->lineno); append_op(cw, buf[1], node->lineno); append_op(cw, buf[2], node->lineno); append_op(cw, buf[3], node->lineno); int_to_byte_array(buf, ct); append_op(cw, LI_MAKE_OBJECT, node->lineno); append_op(cw, buf[0], node->lineno); append_op(cw, buf[1], node->lineno); append_op(cw, buf[2], node->lineno); append_op(cw, buf[3], node->lineno); for(i = ct - 1; i >= 0; i--) { char *sid = arr_get(&names, i); char *nsid = malloc(strlen(sid) + 1); strcpy(nsid, sid); int i = find_prev_name(cw, nsid); if(i < 0) { i = (int)cw->rnames.count; arr_append(&cw->rnames, nsid); } unsigned char buf[4]; int_to_byte_array(buf, i); append_op(cw, buf[0], node->lineno); append_op(cw, buf[1], node->lineno); append_op(cw, buf[2], node->lineno); append_op(cw, buf[3], node->lineno); } arr_free(&names); }
/* markdown • parses the input buffer and renders it into the output buffer */ void markdown(struct buf *ob, struct buf *ib, const struct mkd_renderer *rndrer) { struct link_ref *lr; struct buf *text = bufnew(TEXT_UNIT); size_t i, beg, end; struct render rndr; /* filling the render structure */ if (!rndrer) return; rndr.make = *rndrer; if (rndr.make.max_work_stack < 1) rndr.make.max_work_stack = 1; arr_init(&rndr.refs, sizeof (struct link_ref)); parr_init(&rndr.work); for (i = 0; i < 256; i += 1) rndr.active_char[i] = 0; if ((rndr.make.emphasis || rndr.make.double_emphasis || rndr.make.triple_emphasis) && rndr.make.emph_chars) for (i = 0; rndr.make.emph_chars[i]; i += 1) rndr.active_char[(unsigned char)rndr.make.emph_chars[i]] = char_emphasis; if (rndr.make.codespan) rndr.active_char['`'] = char_codespan; if (rndr.make.linebreak) rndr.active_char['\n'] = char_linebreak; if (rndr.make.image || rndr.make.link) rndr.active_char['['] = char_link; rndr.active_char['<'] = char_langle_tag; rndr.active_char['\\'] = char_escape; rndr.active_char['&'] = char_entity; /* first pass: looking for references, copying everything else */ beg = 0; while (beg < ib->size) /* iterating over lines */ if (is_ref(ib->data, beg, ib->size, &end, &rndr.refs)) beg = end; else { /* skipping to the next line */ end = beg; while (end < ib->size && ib->data[end] != '\n' && ib->data[end] != '\r') end += 1; /* adding the line body if present */ if (end > beg) bufput(text, ib->data + beg, end - beg); while (end < ib->size && (ib->data[end] == '\n' || ib->data[end] == '\r')) { /* add one \n per newline */ if (ib->data[end] == '\n' || (end + 1 < ib->size && ib->data[end + 1] != '\n')) bufputc(text, '\n'); end += 1; } beg = end; } /* sorting the reference array */ if (rndr.refs.size) qsort(rndr.refs.base, rndr.refs.size, rndr.refs.unit, cmp_link_ref_sort); /* adding a final newline if not already present */ if (text->size && text->data[text->size - 1] != '\n' && text->data[text->size - 1] != '\r') bufputc(text, '\n'); /* second pass: actual rendering */ if (rndr.make.prolog) rndr.make.prolog(ob, rndr.make.opaque); parse_block(ob, &rndr, text->data, text->size); if (rndr.make.epilog) rndr.make.epilog(ob, rndr.make.opaque); /* clean-up */ bufrelease(text); lr = rndr.refs.base; for (i = 0; i < rndr.refs.size; i += 1) { bufrelease(lr[i].id); bufrelease(lr[i].link); bufrelease(lr[i].title); } arr_free(&rndr.refs); assert(rndr.work.size == 0); for (i = 0; i < rndr.work.asize; i += 1) bufrelease(rndr.work.item[i]); parr_free(&rndr.work); }
/* markdown • parses the input buffer and renders it into the output buffer */ void ups_markdown(struct buf *ob, struct buf *ib, const struct mkd_renderer *rndrer, unsigned int extensions) { struct link_ref *lr; struct buf *text; size_t i, beg, end; struct render rndr; /* filling the render structure */ if (!rndrer) return; text = bufnew(TEXT_UNIT); if (!text) return; rndr.make = *rndrer; arr_init(&rndr.refs, sizeof (struct link_ref)); parr_init(&rndr.work); for (i = 0; i < 256; i += 1) rndr.active_char[i] = 0; if (rndr.make.emphasis || rndr.make.double_emphasis || rndr.make.triple_emphasis) { rndr.active_char['*'] = char_emphasis; rndr.active_char['_'] = char_emphasis; if (extensions & MKDEXT_STRIKETHROUGH) rndr.active_char['~'] = char_emphasis; } if (rndr.make.codespan) rndr.active_char['`'] = char_codespan; if (rndr.make.linebreak) rndr.active_char['\n'] = char_linebreak; if (rndr.make.image || rndr.make.link) rndr.active_char['['] = char_link; rndr.active_char['<'] = char_langle_tag; rndr.active_char['\\'] = char_escape; rndr.active_char['&'] = char_entity; if (extensions & MKDEXT_AUTOLINK) { rndr.active_char['h'] = char_autolink; // http, https rndr.active_char['H'] = char_autolink; rndr.active_char['f'] = char_autolink; // ftp rndr.active_char['F'] = char_autolink; rndr.active_char['m'] = char_autolink; // mailto rndr.active_char['M'] = char_autolink; } /* Extension data */ rndr.ext_flags = extensions; rndr.max_nesting = 16; /* first pass: looking for references, copying everything else */ beg = 0; while (beg < ib->size) /* iterating over lines */ if (is_ref(ib->data, beg, ib->size, &end, &rndr.refs)) beg = end; else { /* skipping to the next line */ end = beg; while (end < ib->size && ib->data[end] != '\n' && ib->data[end] != '\r') end += 1; /* adding the line body if present */ if (end > beg) expand_tabs(text, ib->data + beg, end - beg); while (end < ib->size && (ib->data[end] == '\n' || ib->data[end] == '\r')) { /* add one \n per newline */ if (ib->data[end] == '\n' || (end + 1 < ib->size && ib->data[end + 1] != '\n')) bufputc(text, '\n'); end += 1; } beg = end; } /* sorting the reference array */ if (rndr.refs.size) qsort(rndr.refs.base, rndr.refs.size, rndr.refs.unit, cmp_link_ref_sort); /* adding a final newline if not already present */ if (!text->size) goto cleanup; if (text->data[text->size - 1] != '\n' && text->data[text->size - 1] != '\r') bufputc(text, '\n'); /* second pass: actual rendering */ if (rndr.make.doc_header) rndr.make.doc_header(ob, rndr.make.opaque); parse_block(ob, &rndr, text->data, text->size); if (rndr.make.doc_footer) rndr.make.doc_footer(ob, rndr.make.opaque); /* clean-up */ cleanup: bufrelease(text); lr = rndr.refs.base; for (i = 0; i < (size_t)rndr.refs.size; i += 1) { bufrelease(lr[i].id); bufrelease(lr[i].link); bufrelease(lr[i].title); } arr_free(&rndr.refs); assert(rndr.work.size == 0); for (i = 0; i < (size_t)rndr.work.asize; i += 1) bufrelease(rndr.work.item[i]); parr_free(&rndr.work); }