/* ** open parts that may cause memory-allocation errors */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); UNUSED(ud); stack_init(L, L); /* init stack */ sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ luaT_init(L); luaX_init(L); luaS_fix(luaS_newliteral(L, MEMERRMSG)); g->GCthreshold = 4*g->totalbytes; }
/* ** open parts of the state that may cause memory-allocation errors. ** ('g->version' != NULL flags that the state was completely build) */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); UNUSED(ud); stack_init(L, L); /* init stack */ init_registry(L, g); luaS_init(L); luaT_init(L); luaX_init(L); g->gcrunning = 1; /* allow gc */ g->version = lua_version(NULL); luai_userstateopen(L); }
int main(void) { tlsf_create_with_pool(_tlsf_heap, sizeof(_tlsf_heap)); printf("%s started\n", APPLICATION_NAME); xtimer_init(); msg_init_queue(main_msg_queue, MAIN_MSG_QUEUE_SIZE); netdev_init(); stack_init(); exp_run(); printf("%s stopped\n", APPLICATION_NAME); return 0; }
int main(int argc, const char **argv) { int opt; #if 0 if (argc != 2) { fprintf(stderr, "Usage: motlle `smottle`\n"); exit(2); } sscanf(argv[1], "%p", &load_address); #endif for (;;) switch (getopt(argc, argv, "+d")) { case 'd': debug_lvl = 2; break; case '?': break; case -1: goto done; } done: signal(SIGALRM, silly_sig); garbage_init(); interpret_init(); stack_init(); runtime_init(); call_init(); parser_init(); compile_init(); mcompile_init(); context_init(); ports_init(); if (optind < argc) make_global_state(argc - optind, argv + optind); else make_global_state(0, NULL); mudio_init(); print_init(); if (optind < argc) mload(argv[optind]); else push_repl(); for (;;) motlle_run1(); }
uint32_t *ets_terms_copy_non_recursive_N(term_t *terms, int num, uint32_t *htop, t_proc_bin_t **pbs) { uint32_t cradle[256]; stack_t st; stack_init(&st, WSIZE(ets_deferred_copy_t), cradle, 256); uint32_t *last_htop = terms_copy(&st, terms, num, htop, pbs); stack_done(&st); return last_htop; }
static stack *get_saved_pos(void) { static stack st; static stack *s; if (s == NULL) { s = &st; stack_init(s, NULL); } return s; }
int bst_postOrder(const BisTree *pTree, BNode *pStartNode, Queue *qPostorder) { BNode *pNode; BNode *pLeftChild, *pRightChild; Stack stNodesA, stNodesB; if (pStartNode == 0 || qPostorder == 0) return -1; pNode = 0; pLeftChild = pRightChild = 0; stack_init(&stNodesA, 0); stack_init(&stNodesB, 0); stack_push(&stNodesA, (const void *) pStartNode); while (stack_size(&stNodesA) > 0) { stack_pop(&stNodesA, (void **) &pNode); stack_push(&stNodesB, (const void *) pNode); pLeftChild = bst_leftChild((const BNode *) pNode); pRightChild = bst_rightChild((const BNode *) pNode); if (pLeftChild != 0) { stack_push(&stNodesA, (const void *) pLeftChild); } if (pRightChild != 0) { stack_push(&stNodesA, (const void *) pRightChild); } } while (stack_size(&stNodesB) > 0) { stack_pop(&stNodesB, (void **) &pNode); queue_enqueue(qPostorder, (const void *) pNode); } stack_destroy(&stNodesA); stack_destroy(&stNodesB); return 0; }
int main(){ Stack pe, *pe1; PilaFloat pilaFloat, *pFloat1; init(&pilaFloat); stack_init(&pe); stack_push(&pe, (void*)10); stack_push(&pe, (void*)9); stack_push(&pe, (void*)8); stack_push(&pe, (void*)7); stack_push(&pe, (void*)6); stack_push(&pe, (void*)5); stack_push(&pe, (void*)4); stack_push(&pe, (void*)3); stack_push(&pe, (void*)2); stack_push(&pe, (void*)1); stack_push(&pe, (void*)0); stack_push(&pe, (void*)-1); stack_push(&pe, (void*)-2); stack_push(&pe, (void*)-3); stack_push(&pe, (void*)-4); push(&pilaFloat, 10.0); push(&pilaFloat, 9.0); push(&pilaFloat, 8.0); push(&pilaFloat, 7.0); push(&pilaFloat, 6.0); push(&pilaFloat, 5.0); push(&pilaFloat, 4.0); push(&pilaFloat, 3.0); push(&pilaFloat, 2.0); push(&pilaFloat, 1.0); push(&pilaFloat, 0.0); push(&pilaFloat, -1.0); push(&pilaFloat, -2.0); push(&pilaFloat, -3.0); push(&pilaFloat, -4.0); pe1 = &pe; pFloat1 = &pilaFloat; mulPilaPila(&pe, pe1); mulPilaPilaF(&pilaFloat, pFloat1); while(!stack_empty(&pe)){ printf("%d %f\n", (int)stack_top(&pe), tope(&pilaFloat)); //printf("OK\n"); pop(&pilaFloat); stack_pop(&pe, 0); } return 0; }
/** * \ingroup time * TM_AddOneSecond zählt die aktuelle Zeit weiter * und entscheidet auf weiter durchzuführende Aktionen */ void TM_AddOneSecond(void) { if (++TM_ss == 60) { TM_ss = 0; if (++TM_mm == 60) { TM_mm = 0; // add one hour if (++TM_hh == 24) { TM_hh = 0; TM_AddOneDay(); } // Start der Sommerzeit: März, 2:00:00 ? if ((TM_MM==3) && (TM_DOW==0) && (TM_hh==2)) { if (TM_DD >= 25 ){ // letzte 7 Tage im Monat ? TM_hh++; // 2:00 -> 3:00 } } // Ende der Sommerzeit: Oktober, 03:00 if ((TM_MM==10) && (TM_DOW==0) && (TM_hh==3) && (TM_DS==0)){ if (TM_DD >= 25){ // letzte 7 Tage im Monat ? TM_hh--; // 3:00 -> 2:00 TM_DS=1; // Marker, dass schon umgestellt ist } } } #if USE_SCHEDULER // Timer check every 10 minutes if ((TM_mm % 10) == 0){ machineStatus.regeln = 1; } #endif } if (--timerT == 0) { // Temperaturen messen machineStatus.Tlesen = 1; timerT = TIME_TEMP; } if((time_watchdog++) > WTT) { time_watchdog = 0; stack_init(); } #if USE_NTP ntp_timer--; #endif //USE_NTP }
float parse_postfix(const char *postfix) { char str[INPUT_SIZE], result[ENTRY_SIZE]; char *temp1, *temp2, *res; char *operand, op; float a, b, r; stack_t stack; stack_init(&stack); strcpy(str, postfix); operand = strtok(str, " "); while (operand != NULL) { if (is_number(operand[0])) { stack_push(&stack, operand); } else { op = operand[0]; stack_pop(&stack, &temp1); stack_pop(&stack, &temp2); sscanf(temp1, "%f", &b); sscanf(temp2, "%f", &a); switch (op) { case '+': r = a + b; break; case '-': r = a - b; break; case '*': r = a * b; break; case '/': r = a / b; break; } sprintf(result, "%f", r); stack_push(&stack, result); } operand = strtok(NULL, " "); } stack_pop(&stack, &res); r = atof(res); return r; }
int main(void) { A* a = malloc(sizeof(A)), *b = malloc(sizeof(A)), *c = NULL; List* l = list_init(); Stack* s = stack_init(); Queue* q = queue_init(); DoubleLinkedList* d = dll_init(); printf("a: %d\nB: %d\nc: %d\n", (int)a, (int)b, (int)c); a->a1 = 1; a->a2 = 'c'; b->a1 = 2; b->a2 = 'a'; printf("\n=== LIST TEST ===\n"); list_add(l, a, 0); list_append(l, b); list_remove(l, 0); list_print(l); c = list_get(l, 0); printf("c: %d\n", (int)c); list_delete(l); printf("\n=== STACK TEST ===\n"); stack_push(s, b); stack_push(s, a); stack_pop(s); stack_print(s); c = stack_peek(s); printf("c: %d\n", (int)c); stack_delete(s); printf("\n=== QUEUE TEST ===\n"); queue_push(q, a); queue_push(q, b); queue_pop(q); queue_print(q); c = queue_peek(q); printf("c: %d\n", (int)c); queue_delete(q); printf("\n=== DOUBLE LINKED LIST TEST ===\n"); dll_add(d, b, 0); dll_prepend(d, a); dll_remove(d, 1); dll_print(d); c = dll_get(d, 0); printf("c: %d\n", (int)c); dll_delete(d); free(a); free(b); return 0; }
int eval(linkedlist *expr, double *val) { int i; stack_t stk; /* operator stack */ stack_init(&stk); for(i = 0; i < linkedlist_size(expr); i++) { token_t *t = linkedlist_get(expr, i); token_t *n2, *n1, *res; /* if number, push to stack: */ if (t->type == TOKEN_NUMBER) { stack_push(&stk, t); continue; } /* all operators in the postfix expression are binary. */ if (stack_size(&stk) < 2) { while(!stack_isEmpty(&stk)) free(stack_pop(&stk)); /* free the remaining tokens in expr: */ while(i < linkedlist_size(expr)) free(linkedlist_get(expr, i++)); return -1; /* error. */ } n2 = stack_pop(&stk); n1 = stack_pop(&stk); res = malloc(sizeof(token_t)); res->type = TOKEN_NUMBER; res->op = 0; res->num = do_op(n1->num, n2->num, t->op); stack_push(&stk, res); free(n2); free(n1); free(t); } if (stack_size(&stk) != 1) { while(!stack_isEmpty(&stk)) { free(stack_pop(&stk)); } return -1; /* error. */ } *val = ((token_t *) stack_peek(&stk))->num; free(stack_pop(&stk)); return 0; }
EntryType eval_expr(char * expr) { char op, c = expr[0]; int a, b; StackPtr OPTR, OPND; stack_init(OPTR); stack_init(OPND); stack_push(OPTR, '#'); while(c != '#' || OPTR->top->entry != '#') { if (!is_operand(c)){ // 如果c是操作数 stack_push(OPND, c); // 对c进行入栈操作 expr++; c = *expr; } else{ switch(prec_compare(OPTR->top->entry, c) ){ case '<': // 栈顶元素优先级低于当前运算符 push(OPTR, c); // 将当前运算符入栈 expr++; c = *expr; // 读入下一个字符 break; case '=': // 处理括号 pop(OPTR, &op); // 弹出左括号 expr++; c = *expr; // 读入下一个字符 break; case '>': // 栈顶元素优先级高于当前运算符 pop(OPTR, &op); // 运算符退栈 pop(OPND, &a); pop(OPND, &b); push(OPND, calc(a, b, op)); // 将运算结果入栈 break; } // end switch } }// end while return OPND->top->entry; }
int main(void) { int i; stack_t s; s = stack_init(); for (i = 0; i < 2; i++) { test(s); } stack_finalize(s); return 0; }
void test_valid_move_from_stock_to_foundation_stacks() { struct stack *stock, *foundation_stacks[4]; stack_malloc(&stock); stack_init(stock); card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X); for (int i = 0; i < 4; i++) { stack_malloc(&foundation_stacks[i]); stack_init(foundation_stacks[i]); } card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X); card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X); card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X); card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X); for (int i = 0; i < 4; i++) { assert(!valid_move(stock, foundation_stacks[i])); } stack_free(stock); for (int i = 0; i < 4; i++) { stack_free(foundation_stacks[i]); } }
void test_valid_move_from_foundation_stack_to_waste_pile() { struct stack *waste_pile, *foundation_stacks[4]; stack_malloc(&waste_pile); stack_init(waste_pile); card_set(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X); for (int i = 0; i < 4; i++) { stack_malloc(&foundation_stacks[i]); stack_init(foundation_stacks[i]); } card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X); card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X); card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X); card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X); for (int i = 0; i < 4; i++) { assert(!valid_move(foundation_stacks[i], waste_pile)); } stack_free(waste_pile); for (int i = 0; i < 4; i++) { stack_free(foundation_stacks[i]); } }
static void test_init(){ Stack *stack = (Stack *)malloc(sizeof(Stack)); stack_init(stack, NULL); assert(0 == stack_size(stack)); assert(NULL == stack_peek(stack)); /* int *d = (int *)malloc(sizeof(int)); stack_pop(stack, (void **) &d); assert(NULL == d );*/ }
lua_State *luaE_newthread (lua_State *L) { lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State))); luaC_link(L, obj2gco(L1), LUA_TTHREAD); preinit_state(L1, G(L)); stack_init(L1, L); /* init stack */ setobj2n(L, gt(L1), gt(L)); /* share table of globals */ L1->hookmask = L->hookmask; L1->basehookcount = L->basehookcount; L1->hook = L->hook; resethookcount(L1); lua_assert(iswhite(obj2gco(L1))); return L1; }
/* ** open parts of the state that may cause memory-allocation errors */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); //UNUSED(ud); stack_init(L, L); /* init stack */ //init_registry(L, g); //luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ //luaT_init(L); //luaX_init(L); ///* pre-create memory-error message */ //g->memerrmsg = luaS_newliteral(L, MEMERRMSG); //luaS_fix(g->memerrmsg); /* it should never be collected */ //g->gcrunning = 1; /* allow gc */ }
int ar_init (ar * warehouse) { /* initialize an ar table */ if ((*warehouse = (ar) malloc (sizeof (struct ActRecTable))) == NULL) { FI_errno = FI_DEFERR; return 0; } if (!stack_init (&((*warehouse) -> entries))) { free (*warehouse); FI_errno = FI_DEFERR; return 0; } return 1; }
void stk_test_print(void) { stack_t* s = stack_init(stk_compare, stk_print, stk_clone, stk_destroy); CU_ASSERT (s != NULL); CU_ASSERT (stack_push(s, stk_test_s1) != NULL);; CU_ASSERT (stack_push(s, stk_test_s2) != NULL);; CU_ASSERT (stack_push(s, stk_test_s3) != NULL);; CU_ASSERT (stack_push(s, stk_test_s4) != NULL);; stack_print(s, stdout); CU_ASSERT (stack_destroy(s) == 0); }
void stk_test_push(void) { stack_t* s = stack_init(stk_compare, stk_print, stk_clone, stk_destroy); CU_ASSERT (stack_push(NULL, stk_test_s1) == NULL); CU_ASSERT (stack_push(s, NULL) == NULL); CU_ASSERT (stack_push(s, stk_test_s1) == stk_test_s1); CU_ASSERT (stack_push(s, stk_test_s2) == stk_test_s2); CU_ASSERT (stack_push(s, stk_test_s3) == stk_test_s3); CU_ASSERT (stack_push(s, stk_test_s4) == stk_test_s4); CU_ASSERT (stack_destroy(s) == 0); }
/* ** open parts of the state that may cause memory-allocation errors */ static void f_killaopen (killa_State *L, void *ud) { killa_GlobalState *g = KILLA_G(L); KILLA_UNUSED(ud); stack_init(L, L); /* init stack */ init_registry(L, g); killaS_resize(L, KILLA_MINSTRTABSIZE); /* initial size of string table */ killaT_init(L); killaX_init(L); /* pre-create memory-error message */ g->memerrmsg = killaS_newliteral(L, MEMERRMSG); killaS_fix(g->memerrmsg); /* it should never be collected */ g->gcrunning = 1; /* allow gc */ }
int test_2() { int size = 10000; int max = 50000; int i, j; float aux; float val[size]; struct stack st; printf("Test 2: %d floats...", size); for (i = 0; i < size; i++) { val[i] = (float)((rand() % max) + 1); } stack_init(&st, sizeof(float)); for (i = 0; i < size; i++) { if (stack_push(&st, &val[i]) < 0) { printf("error: push\n"); return -1; } } for (j = size - 1; j >= 0; j--) { if (stack_get_size(&st) == 0) { printf("error: size should not be 0\n"); return -1; } if (stack_pop(&st, &aux) < 0) { printf("error: pop\n"); return -1; } if (aux != val[j]) { printf("error: aux (%f) != val[%d] (%f)\n", aux, j, val[j]); return -1; } } if (stack_get_size(&st) != 0) { printf("size %d\n", stack_get_size(&st)); printf("error: size should be 0\n"); return -1; } printf(" OK!\n"); return 0; }
void test_move_card_from_non_stack_empty_stack_to_stack_empty_stack() { struct stack *origin, *destination; struct card *card[6]; for (int i = 0; i < 6; i++) { card_malloc(&card[i]); card_init(card[i]); card_set(card[i], TWO + i, i % 5, i % 2, 99, 99); } stack_malloc(&origin); stack_malloc(&destination); stack_init(origin); stack_init(destination); for (int i = 0; i < 6; i++) { stack_push(&origin, card[i]); } move_card(&origin, &destination); assert(stack_length(origin) == 5); assert(stack_length(destination) == 1); assert(cards_equal(destination->card, card[5])); move_card(&origin, &destination); assert(stack_length(origin) == 4); assert(stack_length(destination) == 2); assert(cards_equal(destination->card, card[4])); move_card(&origin, &destination); assert(stack_length(origin) == 3); assert(stack_length(destination) == 3); assert(cards_equal(destination->card, card[3])); stack_free(origin); stack_free(destination); }
/** * create a new stack */ sstack_t *stack_new(void) { sstack_t *stk; stk = calloc(1, sizeof *stk); if (NULL == stk) { ERRF(__FILE__, __LINE__, "ERROR: allocating for a new stack!\n"); exit(1); } stack_init(stk); return stk; }
Drawing create_drawing() { Drawing d = malloc(sizeof(Drawing)); d->stack = stack_init(); d->firstLayer = NULL; d->lastLayer = NULL; if (!d->stack) { free(d); return NULL; } return d; }
static void si_init(void) { // Create stack for linenumbers lineno_stack = dll_init(); // Initialize scope scope_stack = stack_init(); // Add global scope t_scope *tmp = smm_malloc(sizeof(t_scope)); tmp->context = NULL; tmp->depth = 0; tmp->entrypoint = NULL; stack_push(scope_stack, tmp); }
/* create */ int tpool_init(tpool_t * tp, unsigned int cnt, tfunc controller, tfunc handler) { int i; #ifdef DEBUG assert(NULL != tp); assert(NULL != handler); #endif tp->cnt = cnt; tp->pool = malloc(tp->cnt * (sizeof *(tp->pool))); tp->handler = handler; tp->controller = controller; tp->jobcount = 0; /* set up mutex */ pthread_mutex_init(&tp->mutex, NULL); /* get work queue ready */ stack_init(&tp->work); /* set up the control thread */ pthread_create(&tp->boss.th, NULL, tp->controller, NULL); tp->boss.id = 0; tp->boss.job = 0; tp->boss.busy = 0; pthread_mutex_init(&tp->boss.mt, NULL); pthread_mutex_lock(&tp->boss.mt); pthread_detach(tp->boss.th); for (i = 0; i < (signed)tp->cnt; ++i) { if (pthread_create (&tp->pool[i].th, NULL, tp->handler, (void *)i)) { /* trouble... */ ERRF(__FILE__, __LINE__, "error creating thread %d in pool.\n", i); return 0; } /* automatic cleanup */ tp->pool[i].id = i; tp->pool[i].job = 0; tp->pool[i].busy = 0; pthread_mutex_init(&tp->pool[i].mt, NULL); pthread_mutex_lock(&tp->pool[i].mt); pthread_detach(tp->pool[i].th); } return 1; }
static void postfix_infix(const char *postfix) { assert(postfix != 0); printf("Postfix expression: %s\n", postfix); char c; bool valid = true; Stack stack; stack_init(&stack); while ((c = *postfix++) != '\0') { if (isdigit(c)) stack_push_chr(&stack, c); else if (is_operator(c)) { char *rhs = stack_pop(&stack); char *lhs = stack_pop(&stack); size_t l_rhs = strlen(rhs); size_t l_lhs = strlen(lhs); char expr[l_lhs + l_rhs + sizeof("()*()")]; snprintf(expr, sizeof(expr), "%s%s%s%c%s%s%s", (l_lhs > 1) ? "(" : "", lhs, (l_lhs > 1) ? ")" : "", c, (l_rhs > 1) ? "(" : "", rhs, (l_rhs > 1) ? ")" : ""); stack_push_str(&stack, expr); free(lhs); free(rhs); } else if (isspace(c)) continue; else { err_remark("Invalid character %c (0x%.2X) found\n", c, c); valid = false; break; } } if (valid) { if (stack.tos != 1) err_remark("Stack has wrong number of entries (%zu instead of 1)\n", stack.tos); else { char *infix = stack_pop(&stack); printf("Infix expression: %s\n", infix); free(infix); } } stack_free(&stack); }