/** * Frees a compiler object and all associated memory. * compiler: an instance of Compiler. * TODO: a lot of allocations don't have frees yet. These will be added in when * the program structure becomes final. */ void compiler_free(Compiler * compiler) { assert(compiler != NULL); if(compiler->compiledScripts != NULL) { set_free(compiler->compiledScripts); } if(compiler->symTableStk != NULL) { stk_free(compiler->symTableStk); } if(compiler->functionHT != NULL) { HTIter htIterator; ht_iter_get(compiler->functionHT, &htIterator); while(ht_iter_has_next(&htIterator)) { DSValue value; ht_iter_next(&htIterator, NULL, 0, &value, NULL, true); compilerfunc_free(value.pointerVal); } ht_free(compiler->functionHT); } if(compiler->outBuffer != NULL) { buffer_free(compiler->outBuffer); } free(compiler); }
/* conditioned_on must be an array of integer lists; specifically, the ith element must be the list of state numbers on which the ith state is conditioned. */ Unspooler *cm_create_unspooler(int nstates_spooled, List **conditioned_on) { UnspoolNode *n; int i, j; Stack *s; Unspooler *unsp; int *mark; int capacity; unsp = (Unspooler*)smalloc(sizeof(Unspooler)); unsp->nstates_spooled = nstates_spooled; unsp->nstates_unspooled = 0; unsp->spooled_to_unspooled = (UnspoolNode**)smalloc(nstates_spooled * sizeof(UnspoolNode*)); capacity = nstates_spooled * nstates_spooled; unsp->unspooled_to_spooled = (int*)smalloc(capacity * sizeof(int)); mark = (int*)smalloc(nstates_spooled * sizeof(int)); s = stk_new_ptr(nstates_spooled); for (i = 0; i < nstates_spooled; i++) { /* erase marks (used to detect cycles) */ for (j = 0; j < nstates_spooled; j++) mark[j] = 0; unsp->spooled_to_unspooled[i] = cm_new_unspool_node(i); stk_push_ptr(s, unsp->spooled_to_unspooled[i]); while ((n = (UnspoolNode*)stk_pop_ptr(s)) != NULL) { if (conditioned_on[n->oldstate] == NULL || lst_size(conditioned_on[n->oldstate]) == 0) { n->newstate = unsp->nstates_unspooled++; /* mapping to spooled space */ if (n->newstate >= capacity) { capacity *= 2; unsp->unspooled_to_spooled = (int*)srealloc(unsp->unspooled_to_spooled, capacity * sizeof(int)); } unsp->unspooled_to_spooled[n->newstate] = i; } else { for (j = 0; j < lst_size(conditioned_on[n->oldstate]); j++) { int oldstate = lst_get_int(conditioned_on[n->oldstate], j); UnspoolNode *m; if (mark[oldstate] == 1) die("ERROR: cycle in 'conditioned_on' dependencies.\n"); mark[oldstate] = 1; m = cm_new_unspool_node(oldstate); lst_push_ptr(n->children, m); stk_push_ptr(s, m); } } } } stk_free(s); sfree(mark); return unsp; }
/** * Frees a compiler object and all associated memory. * compiler: an instance of Compiler. * TODO: a lot of allocations don't have frees yet. These will be added in when * the program structure becomes final. */ void compiler_free(Compiler * compiler) { assert(compiler != NULL); if(compiler->compiledScripts != NULL) { set_free(compiler->compiledScripts); } if(compiler->symTableStk != NULL) { stk_free(compiler->symTableStk); } free(compiler); }
stk_async_queue_t *stk_async_queue_new() { stk_async_queue_t *queue; stk_pool_t *pool; if ((pool = stk_create_pool()) == NULL) { return NULL; } queue = (stk_async_queue_t *)stk_palloc(pool, sizeof(stk_async_queue_t)); if (queue) { if (__stk_async_queue_init(queue, pool) == STK_ERR) { stk_destroy_pool(pool); stk_free(queue); } } return queue; }
void cm_free_unspooler(Unspooler *unsp) { int i, j; UnspoolNode *n; Stack *s = stk_new_ptr(unsp->nstates_unspooled); /* free all nodes in tree */ for (i = 0; i < unsp->nstates_spooled; i++) stk_push_ptr(s, unsp->spooled_to_unspooled[i]); while ((n = stk_pop_ptr(s)) != NULL) { for (j = 0; j < lst_size(n->children); j++) stk_push_ptr(s, lst_get_ptr(n->children, j)); cm_free_unspool_node(n); } sfree(unsp->spooled_to_unspooled); sfree(unsp->unspooled_to_spooled); stk_free(s); sfree(unsp); }
/* * Deletes the mailbox for the specified user. */ MailboxResult *mailbox_quit_1_svc(MailboxParams *argp, struct svc_req *rqstp) { static MailboxResult result = MailboxResultSuccess; // Check that users hashtable exists. if (g_users_ht == NULL) { result = MailboxResultUserNotExists; return &result; } bool exists; DSValue mailbox; // Try to delete mailbox, and handle any malloc/free errors. if (!ht_put(g_users_ht, argp->user, NULL, &mailbox, &exists)) { result = MailboxResultServerFailure; return &result; } // User Mailbox doesn't exist, return error code. if (!exists) { result = MailboxResultUserNotExists; return &result; } // Delete user's message list if it exists. if (mailbox.pointerVal != NULL) { Stk *mailbox_stk = (Stk*)mailbox.pointerVal; // Free messages. for (int i = 0; i < stk_depth(mailbox_stk); i++) { DSValue value; if (stk_get(mailbox_stk, &value, i) && value.pointerVal != NULL) { free(value.pointerVal); } } stk_free((Stk*)mailbox.pointerVal); } result = MailboxResultSuccess; return &result; }
void impl( ) { stk_t * k = stk_new(); char cur; int lhs; int rhs; int val; int total = 0; while( (cur = fgetc(stdin)) != EOF ) { if(isdigit(cur)) { int digits[MAXDIGITS]; int n_digits = 0; digits[ n_digits++ ] = cur - '0'; while( (cur = fgetc(stdin)) != EOF && isdigit(cur) && n_digits < MAXDIGITS) digits[ n_digits++ ] = cur - '0'; int val = 0; for(int i = 0, j = n_digits - 1; i < n_digits; i ++, j-- ) val += digits[i] * pow(10, j); stk_push(k, val); } switch(cur) { // handle operators case '%': case '+': case '-': case '*': case '/': //printf("got op, %c\n",cur); if(! stk_is_empty(k) ) if( (lhs = stk_pop(k)) == -1 ) { printf("Bad lhs pop\n"); return; } if(! stk_is_empty(k) ) { if( (rhs = stk_pop(k)) == -1 ) { printf("Bad rhs pop\n"); return; } fprintf(stdout, "lhs %d, rhs %d, op %c\n",lhs,rhs,cur); stk_push(k, calc_op(cur, lhs, rhs)); } else { stk_push(k,lhs); stk_push(k,cur); } break; // clear stack with _ underscore case '_': calc_clear(k); break; case '.': calc_print_top(k,stdout); break; case '>': calc_swap_top(k); break; } } stk_dump(k,stdout); stk_free(k); }