// implement a stack with list // list would always have a dummy header // tree in dummy type-casted to store the size of stack (not counting the dummy) // void push(lnode *head, lnode *node) { node->next = head->next; head->next = node; set_stack_size(head, stack_size(head)+1); }
int main(int argc, char *argv[]) { int opt; char *scriptfile, prompt[50]; extern void my_interfaceparse(char *my_string); /* YY_BUFFER_STATE flex_command; */ stack_init(); while ((opt = getopt(argc, argv, "e:f:hl:pqrsv")) != -1) { switch(opt) { case 'e': my_interfaceparse(optarg); break; case 'f': scriptfile = file_to_mem(optarg); if (scriptfile != NULL) { input_is_file = 1; my_interfaceparse(scriptfile); } exit(0); case 'h': print_help(); exit(0); case 'l': scriptfile = file_to_mem(optarg); if (scriptfile != NULL) { input_is_file = 1; my_interfaceparse(scriptfile); xxfree(scriptfile); } break; case 'p': pipe_mode = 1; break; case 'q': quiet_mode = 1; break; case 'r': use_readline = 0; break; case 's': exit(0); case 'v': printf("%s %i.%i.%i%s\n",argv[0],MAJOR_VERSION,MINOR_VERSION,BUILD_VERSION,STATUS_VERSION); exit(0); default: fprintf(stderr, "%s", usagestring); exit(EXIT_FAILURE); } } if (!pipe_mode && !quiet_mode) printf("%s",disclaimer); rl_basic_word_break_characters = " >"; rl_attempted_completion_function = my_completion; for(;;) { if (promptmode == PROMPT_MAIN) sprintf(prompt, "foma[%i]: ",stack_size()); if (promptmode == PROMPT_A && apply_direction == AP_D) sprintf(prompt, "apply down> "); if (promptmode == PROMPT_A && apply_direction == AP_U) sprintf(prompt, "apply up> "); if (promptmode == PROMPT_A && apply_direction == AP_M) sprintf(prompt, "apply med> "); if (pipe_mode || quiet_mode) prompt[0] = '\0'; command = rl_gets(prompt); if (command == NULL && promptmode == PROMPT_MAIN) { printf("\n"); exit(0); } if (command == NULL && promptmode == PROMPT_A) { /* apply_clear(); */ promptmode = PROMPT_MAIN; printf("\n"); continue; } input_is_file = 0; my_interfaceparse(command); } }
int _lru_free_mem(cache_t *c, segment_t *pseg, ex_off_t bytes_to_free) { cache_lru_t *cp = (cache_lru_t *)c->fn.priv; cache_segment_t *s; cache_page_t *p; page_lru_t *lp; Stack_ele_t *ele; apr_thread_mutex_t *plock; ex_off_t total_bytes, pending_bytes; int gotlock, count, bits, err; total_bytes = 0; err = 0; log_printf(15, "START seg=" XIDT " bytes_to_free=" XOT " bytes_used=" XOT " stack_size=%d\n", segment_id(pseg), bytes_to_free, cp->bytes_used, stack_size(cp->stack)); move_to_bottom(cp->stack); ele = get_ptr(cp->stack); while ((total_bytes < bytes_to_free) && (ele != NULL) && (err == 0)) { p = (cache_page_t *)get_stack_ele_data(ele); lp = (page_lru_t *)p->priv; plock = p->seg->lock; gotlock = apr_thread_mutex_trylock(plock); if ((gotlock == APR_SUCCESS) || (p->seg == pseg)) { bits = atomic_get(p->bit_fields); if ((bits & C_TORELEASE) == 0) { //** Skip it if already flagged for removal count = atomic_get(p->access_pending[CACHE_READ]) + atomic_get(p->access_pending[CACHE_WRITE]) + atomic_get(p->access_pending[CACHE_FLUSH]); if (count == 0) { //** No one is using it s = (cache_segment_t *)p->seg->priv; if ((bits & C_ISDIRTY) == 0) { //** Don't have to flush it total_bytes += s->page_size; log_printf(15, "lru_free_mem: freeing page seg=" XIDT " p->offset=" XOT " bits=%d\n", segment_id(p->seg), p->offset, bits); list_remove(s->pages, &(p->offset), p); //** Have to do this here cause p->offset is the key var delete_current(cp->stack, 1, 0); if (p->data[0].ptr) free(p->data[0].ptr); if (p->data[1].ptr) free(p->data[1].ptr); free(lp); } else { //** Got to flush the page first err = 1; } } else { err = 1; } } if (gotlock == APR_SUCCESS) apr_thread_mutex_unlock(plock); } else { err = 1; } if ((total_bytes < bytes_to_free) && (err == 0)) ele = get_ptr(cp->stack); } cp->bytes_used -= total_bytes; pending_bytes = bytes_to_free - total_bytes; log_printf(15, "END seg=" XIDT " bytes_to_free=" XOT " pending_bytes=" XOT " bytes_used=" XOT "\n", segment_id(pseg), bytes_to_free, pending_bytes, cp->bytes_used); return(pending_bytes); }
ex_off_t _lru_attempt_free_mem(cache_t *c, segment_t *page_seg, ex_off_t bytes_to_free) { cache_lru_t *cp = (cache_lru_t *)c->fn.priv; cache_segment_t *s; segment_t *pseg; cache_page_t *p; page_lru_t *lp; Stack_ele_t *ele; op_generic_t *gop; opque_t *q; ex_off_t total_bytes, freed_bytes, pending_bytes, *poff; ex_off_t *segid; ex_off_t min_off, max_off; list_iter_t sit; int count, bits, cw, flush_count; list_t *table; page_table_t *ptable; pigeon_coop_hole_t pch, pt_pch; log_printf(15, "START seg=" XIDT " bytes_to_free=" XOT " bytes_used=" XOT " stack_size=%d\n", segment_id(page_seg), bytes_to_free, cp->bytes_used, stack_size(cp->stack)); freed_bytes = 0; pending_bytes = 0; total_bytes = 0; //** cache_lock(c) is already acquired pch = reserve_pigeon_coop_hole(cp->free_pending_tables); table = *(list_t **)pigeon_coop_hole_data(&pch); //** Get the list of pages to free move_to_bottom(cp->stack); ele = stack_unlink_current(cp->stack, 1); while ((total_bytes < bytes_to_free) && (ele != NULL)) { p = (cache_page_t *)get_stack_ele_data(ele); lp = (page_lru_t *)p->priv; bits = atomic_get(p->bit_fields); log_printf(15, "checking page for release seg=" XIDT " p->offset=" XOT " bits=%d\n", segment_id(p->seg), p->offset, bits); flush_log(); if ((bits & C_TORELEASE) == 0) { //** Skip it if already flagged for removal ptable = (page_table_t *)list_search(table, (list_key_t *)&(segment_id(p->seg))); if (ptable == NULL) { //** Have to make a new segment entry pt_pch = reserve_pigeon_coop_hole(cp->free_page_tables); ptable = (page_table_t *)pigeon_coop_hole_data(&pt_pch); ptable->seg = p->seg; ptable->id = segment_id(p->seg); ptable->pch = pt_pch; list_insert(table, &(ptable->id), ptable); } cp->limbo_pages++; log_printf(15, "UNLINKING seg=" XIDT " p->offset=" XOT " bits=%d limbo=%d\n", segment_id(p->seg), p->offset, bits, cp->limbo_pages); atomic_inc(p->access_pending[CACHE_READ]); //** Do this so it's not accidentally deleted push(ptable->stack, p); s = (cache_segment_t *)p->seg->priv; total_bytes += s->page_size; free(lp->ele); lp->ele = NULL; //** Mark it as removed from the list so a page_release doesn't free also } if (total_bytes < bytes_to_free) ele = stack_unlink_current(cp->stack, 1); } if (total_bytes == 0) { //** Nothing to do so exit log_printf(15, "Nothing to do so exiting\n"); release_pigeon_coop_hole(cp->free_pending_tables, &pch); return(0); } cache_unlock(c); //** Don't need the cache lock for the next part q = new_opque(); opque_start_execution(q); //** Now cycle through the segments to be freed pending_bytes = 0; sit = list_iter_search(table, list_first_key(table), 0); list_next(&sit, (list_key_t **)&segid, (list_data_t **)&ptable); while (ptable != NULL) { //** Verify the segment is still valid. If not then just delete everything pseg = list_search(c->segments, segid); if (pseg != NULL) { segment_lock(ptable->seg); min_off = s->total_size; max_off = -1; s = (cache_segment_t *)ptable->seg->priv; while ((p = pop(ptable->stack)) != NULL) { atomic_dec(p->access_pending[CACHE_READ]); //** Removed my access control from earlier flush_count = atomic_get(p->access_pending[CACHE_FLUSH]); cw = atomic_get(p->access_pending[CACHE_WRITE]); count = atomic_get(p->access_pending[CACHE_READ]) + cw + flush_count; bits = atomic_get(p->bit_fields); if (count != 0) { //** Currently in use so wait for it to be released if (cw > 0) { //** Got writes so need to wait until they complete otherwise the page may not get released bits = bits | C_TORELEASE; //** Mark it for release atomic_set(p->bit_fields, bits); _cache_drain_writes(p->seg, p); //** Drain the write ops bits = atomic_get(p->bit_fields); //** Get the bit fields to see if it's dirty } if (flush_count == 0) { //** Make sure it's not already being flushed if ((bits & C_ISDIRTY) != 0) { //** Have to flush it don't have to track it cause the flush will do the release if (min_off > p->offset) min_off = p->offset; if (max_off < p->offset) max_off = p->offset; } } bits = bits | C_TORELEASE; log_printf(15, "in use tagging for release seg=" XIDT " p->offset=" XOT " bits=%d\n", segment_id(p->seg), p->offset, bits); atomic_set(p->bit_fields, bits); pending_bytes += s->page_size; } else { //** Not in use if ((bits & (C_ISDIRTY|C_EMPTY)) == 0) { //** Don't have to flush it just drop the page cp->limbo_pages--; log_printf(15, "FREEING page seg=" XIDT " p->offset=" XOT " bits=%d limbo=%d\n", segment_id(p->seg), p->offset, bits, cp->limbo_pages); list_remove(s->pages, &(p->offset), p); //** Have to do this here cause p->offset is the key var if (p->data[0].ptr) free(p->data[0].ptr); if (p->data[1].ptr) free(p->data[1].ptr); lp = (page_lru_t *)p->priv; free(lp); freed_bytes += s->page_size; } else { //** Got to flush the page first but don't have to track it cause the flush will do the release if (p->offset > -1) { //** Skip blank pages if (min_off > p->offset) min_off = p->offset; if (max_off < p->offset) max_off = p->offset; } bits = bits | C_TORELEASE; atomic_set(p->bit_fields, bits); pending_bytes += s->page_size; if (p->offset > -1) { log_printf(15, "FLUSHING page seg=" XIDT " p->offset=" XOT " bits=%d\n", segment_id(p->seg), p->offset, bits); } else { log_printf(15, "RELEASE trigger for empty page seg=" XIDT " p->offset=" XOT " bits=%d\n", segment_id(p->seg), p->offset, bits); } } } list_next(&sit, (list_key_t **)&poff, (list_data_t **)&p); } segment_unlock(ptable->seg); if (max_off>-1) { gop = cache_flush_range(ptable->seg, s->c->da, min_off, max_off + s->page_size - 1, s->c->timeout); opque_add(q, gop); } } else { //** Segment has been deleted so drop everything cause it's already freeed empty_stack(ptable->stack, 0); } cache_lock(c); release_pigeon_coop_hole(cp->free_page_tables, &(ptable->pch)); cache_unlock(c); list_next(&sit, (skiplist_key_t **)&pseg, (skiplist_data_t **)&ptable); } cache_lock(c); log_printf(15, "BEFORE waitall seg=" XIDT " bytes_to_free=" XOT " bytes_used=" XOT " freed_bytes=" XOT " pending_bytes=" XOT "\n", segment_id(page_seg), bytes_to_free, cp->bytes_used, freed_bytes, pending_bytes); cache_unlock(c); //** Wait for any tasks to complete opque_waitall(q); opque_free(q, OP_DESTROY); //** Had this when we came in cache_lock(c); log_printf(15, "AFTER waitall seg=" XIDT " bytes_used=" XOT "\n", segment_id(page_seg), cp->bytes_used); cp->bytes_used -= freed_bytes; //** Update how much I directly freed log_printf(15, "AFTER used update seg=" XIDT " bytes_used=" XOT "\n", segment_id(page_seg), cp->bytes_used); //** Clean up empty_skiplist(table); release_pigeon_coop_hole(cp->free_pending_tables, &pch); log_printf(15, "total_bytes marked for removal =" XOT "\n", total_bytes); return(total_bytes); }
/* * build a stack in kernel * set up the contents in terms of user address * this is to be copied into user stack by copyout() */ void *setup_args_mem(struct runprogram_info *prog_info, vaddr_t *usr_stack, size_t *len) { unsigned int i; unsigned long argc = prog_info->argc; char **argv = prog_info->argv; /* * . * . * . * s * g * r * a * l * a * e * r * --- * --- * --- * *(argv+argc-1) -> translate to user stack * ........ * --- * --- * --- * *(argv+1) -> translate to user stack * --- * --- * --- * *(argv+0) -> translate to user stack * --- * --- * --- * argc <-- ks_start */ // start addr of the user stack copy in kernel *len = stack_size(prog_info); int *ks_start = (int *)kmalloc(*len); unsigned long arg_offset[argc]; /* * offset in terms of num of words */ // arg[0] is always there, it stores the program name arg_offset[0] = (unsigned int)argc + 1; // offset of argv[i]: offset of argv[i-1] + len of argv[i-1] for (i = 1; i < argc; i++) { int len_arg = (strlen(*(argv+i-1))+1)/4; if ((strlen(*(argv+i-1))+1)%4 != 0) { len_arg ++ ; } arg_offset[i] = arg_offset[i-1] + len_arg; } //int *ks = ks_start + 1; //*ks_start = argc; /* * setup the pointer address to point to the addr on the user stack */ // the stack size in terms of words int s_size_w = (*len)/4; for (i = 0; i < argc; i++) { *(ks_start + i) = (arg_offset[i] - s_size_w) + (int *)(*usr_stack); } *(ks_start + argc) = NULL; /* * set up the actual contents in argvs */ for (i = 0; i < argc; i++) { char *argi = (char *)(arg_offset[i] + ks_start); char **x = (char **)(argv+i); int arglen = (strlen(*(argv+i))+1)/4; if ((strlen(*(argv+i))+1)%4 != 0) { arglen ++ ; } memmove((void *)argi, (void *)*x, 4*arglen); } /* * ks_start will passed to copyout * usr_stack will be passed to md_usermode * basically they all point to the beginning of the stack * ks_stack is in kernel, usr_stack is in user as */ // *usr_stack is int, so offset it with respect to num of bytes (*len) *usr_stack = *usr_stack - *len; return (void *)ks_start; }
int lru_pages_release(cache_t *c, cache_page_t **page, int n_pages) { cache_lru_t *cp = (cache_lru_t *)c->fn.priv; cache_segment_t *s; page_lru_t *lp; cache_page_t *p; int bits, i; cache_lock(c); for (i=0; i<n_pages; i++) { p = page[i]; bits = atomic_get(p->bit_fields); log_printf(15, "seg=" XIDT " p->offset=" XOT " bits=%d bytes_used=" XOT "\n", segment_id(p->seg), p->offset, bits, cp->bytes_used); if ((bits & C_TORELEASE) > 0) { log_printf(15, "DESTROYING seg=" XIDT " p->offset=" XOT " bits=%d bytes_used=" XOT "cache_pages=%d\n", segment_id(p->seg), p->offset, bits, cp->bytes_used, stack_size(cp->stack)); s = (cache_segment_t *)p->seg->priv; lp = (page_lru_t *)p->priv; cp->bytes_used -= s->page_size; if (lp->ele != NULL) { move_to_ptr(cp->stack, lp->ele); delete_current(cp->stack, 0, 0); } else { cp->limbo_pages--; log_printf(15, "seg=" XIDT " limbo page p->offset=" XOT " limbo=%d\n", segment_id(p->seg), p->offset, cp->limbo_pages); } if (p->offset > -1) { list_remove(s->pages, &(p->offset), p); //** Have to do this here cause p->offset is the key var } if (p->data[0].ptr) free(p->data[0].ptr); if (p->data[1].ptr) free(p->data[1].ptr); free(lp); } } //** Now check if we can handle some waiters _lru_process_waiters(c); cache_unlock(c); return(0); }
uint8_t DataFlash_MAVLink::queue_size(dm_block_queue_t queue) { return stack_size(queue.oldest); }
void *return_address(unsigned frame_no) const { if(frame_no < stack_size()) return frames_[frame_no]; return 0; }
void path_check1(){ printf("Testing FindPath(...) on World1\n"); Grid *g; g = ReadWorld("world1.txt"); Search *s; s = FindPath(g); assert_i(s->Length, 20, "Shortest path length - World1"); assert_i(stack_size(s->s), s->Length+1, "Stack size - World1"); int currR, currC; int last_currR, last_currC; int continuous = 1; int diffR, diffC; stack_pop(s->s, &last_currR, &last_currC); assert_i(last_currR, 10, "Start Location (Row) - World1"); assert_i(last_currC, 4, "Start Location (Col) - World1"); g->data[last_currR][last_currC] = '*'; while(stack_size(s->s) > 0){ stack_pop(s->s, &currR, &currC); g->data[currR][currC] = '*'; diffR = currR - last_currR; diffR *= diffR; diffC = currC - last_currC; diffC *= diffC; // XOR - Eith diffR or diffC should be 1, not both. if(!(!diffR != !diffC)){ continuous = 0; } last_currR = currR; last_currC = currC; } assert_i(currR, 3, "Path Ends on Goal (Row) - World1"); assert_i(currC, 13, "Path Ends on Goal (Col) - World1"); assert_i(continuous, 1, "Continuous Path from source"); int actual[14][17] = {{MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT}, {MAX_INT, 12, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,MAX_INT}, {MAX_INT, 11, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,MAX_INT}, {MAX_INT, 10, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,MAX_INT}, {MAX_INT, 9, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,MAX_INT}, {MAX_INT, 8, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,MAX_INT}, {MAX_INT, 7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,MAX_INT}, {MAX_INT, 6, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,MAX_INT}, {MAX_INT, 5, 4,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT, 14, 15, 16, 17,MAX_INT}, {MAX_INT, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7,MAX_INT, 13, 14, 15, 16,MAX_INT}, {MAX_INT, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6,MAX_INT, 12, 13, 14, 15,MAX_INT}, {MAX_INT, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7,MAX_INT, 11, 12, 13, 14,MAX_INT}, {MAX_INT, 5, 4, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,MAX_INT}, {MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT,MAX_INT}}; int correct_dist = 1, i,j; // Compare Calculated Distances to Model Solution. We only check for the distances less than the path length (you can stop calculating distances when you find the goal). for(i = 0; i < 14; i++){ for(j = 0; j < 17; j++){ if(actual[i][j] < 20 && actual[i][j] != s->Distance[i][j]){ correct_dist = 0; } } } assert_i(correct_dist, 1, "Distance Array - World1"); printf("Path (Not Marked):\n"); for(i = 0; i < 14; i++){ for(j = 0; j < 17; j++){ printf("%c", g->data[i][j]); } printf("\n"); } }
static inline bool stack_empty(struct stack *stack) { return stack_size(stack) == 0; }
void motopp_freeFrame(MotoPP *ppenv) { MotoPPFrame *frame; Enumeration *e; if (stack_size(ppenv->frames) <= 1) { frame = stack_pop(ppenv->frames); buf_cat(ppenv->out, frame->out); ppenv->frame = NULL; } else { MotoPPVal *val; MotoPPFrame *pframe; SymbolTable *macros; MotoMacro *m; char *catstr; int catstrlen; frame = stack_pop(ppenv->frames); pframe = stack_peek(ppenv->frames); switch (frame->type) { case MACRO_FRAME: m = frame->macro; val = motopp_createVal(ppenv); if (ppenv->flags & MACRO_DEBUG_FLAG) { buf_printf(frame->out, LINE_FMT_2, pframe->filename, pframe->lineno); } val->sval = buf_toString(frame->out); stack_push(pframe->opstack, val); macros = stack_pop(ppenv->macrostack); e = stab_getKeys(macros); while (enum_hasNext(e)) { char *name = (char *)enum_next(e); MotoMacro *m = (MotoMacro *)stab_get(macros, name); motopp_freeMacro(m); } stab_free(macros); enum_free(e); break; case INCLUDE_FRAME: catstr = buf_data(frame->out); catstrlen = strlen(catstr); if (catstr[catstrlen - 1] == '\n') { catstr[catstrlen - 1] = '\0'; } buf_puts(pframe->out, catstr); if (ppenv->flags & MACRO_DEBUG_FLAG) { buf_printf(pframe->out, LINE_FMT_2, pframe->filename, pframe->lineno); } break; default: buf_cat(pframe->out, frame->out); break; } /* reset pplineno */ pplineno = pframe->lineno; /* reset frame */ ppenv->frame = pframe; } if (frame->yybuf != NULL) { if (shared_check(frame->yybuf)) { motopp_freeYYBuffer(frame->yybuf); } } if (frame->yybufmem != NULL) { free(frame->yybufmem); } buf_free(frame->out); stack_free(frame->opstack); free(frame->filename); free(frame->relative_root); free(frame); }
void motopp_freeEnv(MotoPP *ppenv) { Enumeration *e; log_debug(__FILE__, ">>> motopp_freeEnv\n"); buf_free(ppenv->out); buf_free(ppenv->err); buf_free(ppenv->argbuf); istack_free(ppenv->dirstack); stack_free(ppenv->frames); /* free vals */ e = hset_elements(ppenv->vallist); while (enum_hasNext(e)) { MotoPPVal *val = enum_next(e); if (shared_check(val->sval)) { free(val->sval); hset_remove(ppenv->ptrs, val->sval); } free(val); } enum_free(e); hset_free(ppenv->vallist); /* free macros */ while (stack_size(ppenv->macrostack) > 0) { SymbolTable *macros = stack_pop(ppenv->macrostack); e = stab_getKeys(macros); while (enum_hasNext(e)) { char *name = (char *)enum_next(e); MotoMacro *m = (MotoMacro *)stab_get(macros, name); motopp_freeMacro(m); } stab_free(macros); enum_free(e); } /* free all remaining sys pointers */ e = hset_elements(ppenv->sysptrs); while (enum_hasNext(e)) { void *ptr = enum_next(e); if (ptr) { sys_free(ptr); } } enum_free(e); /* free all remaining pointers */ e = hset_elements(ppenv->ptrs); while (enum_hasNext(e)) { void *ptr = enum_next(e); if (shared_check(ptr)) { free(ptr); } } enum_free(e); /* free remaining pooled memory */ mpool_free(ppenv->mpool); /* free remainder of env struct */ hset_free(ppenv->sysptrs); hset_free(ppenv->ptrs); stack_free(ppenv->macrostack); free(ppenv); log_debug(__FILE__, "<<< motopp_freeEnv\n"); }
inline int opstack_size(MotoEnv *env) { return stack_size(env->frame->opstack); }
void _opque_cb(void *v, int mode) { int type, n; op_status_t success; op_generic_t *gop = (op_generic_t *)v; que_data_t *q = &(gop->base.parent_q->qd); log_printf(15, "_opque_cb: START qid=%d gid=%d\n", gop_id(&(q->opque->op)), gop_id(gop)); //** Get the status (gop is already locked) type = gop_get_type(gop); if (type == Q_TYPE_QUE) { n = stack_size(gop->q->failed); log_printf(15, "_opque_cb: qid=%d gid=%d stack_size(q->failed)=%d gop->status=%d\n", gop_id(&(q->opque->op)), gop_id(gop), n, gop->base.status.op_status); success = (n == 0) ? gop->base.status : op_failure_status; } else { success = gop->base.status; } lock_opque(q); log_printf(15, "_opque_cb: qid=%d gid=%d success=%d gop_type(gop)=%d\n", gop_id(&(q->opque->op)), gop_id(gop), success, gop_get_type(gop)); //** It always goes on the finished list move_to_bottom(q->finished); insert_below(q->finished, gop); log_printf(15, "PUSH finished gid=%d qid=%d\n", gop_id(gop), gop_id(&(q->opque->op))); log_printf(15, "Printing finished stack for qid=%d\n", gop_id(&(q->opque->op))); if (log_level() > 15) _opque_print_stack(q->finished); if (success.op_status == OP_STATE_FAILURE) push(q->failed, gop); //** Push it on the failed list if needed q->nleft--; log_printf(15, "_opque_cb: qid=%d gid=%d nleft=%d stack_size(q->failed)=%d stack_size(q->finished)=%d\n", gop_id(&(q->opque->op)), gop_id(gop), q->nleft, stack_size(q->failed), stack_size(q->finished)); flush_log(); if (q->nleft <= 0) { //** we're finished if (stack_size(q->failed) == 0) { q->opque->op.base.status = op_success_status; callback_execute(q->opque->op.base.cb, OP_STATE_SUCCESS); //** Lastly trigger the signal. for anybody listening apr_thread_cond_broadcast(q->opque->op.base.ctl->cond); } else if (q->opque->op.base.retries == 0) { //** How many times we're retried //** Trigger the callbacks q->opque->op.base.retries++; q->nleft = 0; q->opque->op.base.failure_mode = 0; callback_execute(&(q->failure_cb), OP_STATE_FAILURE); //** Attempt to fix things if (q->opque->op.base.failure_mode == 0) { //** No retry q->opque->op.base.status = op_failure_status; callback_execute(q->opque->op.base.cb, OP_STATE_FAILURE); //**Execute the other CBs apr_thread_cond_broadcast(q->opque->op.base.ctl->cond); //** and fail for good } //** If retrying don't send the broadcast log_printf(15, "_opque_cb: RETRY END qid=%d gid=%d\n", gop_id(&(q->opque->op)), gop_id(gop)); flush_log(); } else { //** Finished with errors but trigger the signal for anybody listening apr_thread_cond_broadcast(q->opque->op.base.ctl->cond); } } else { //** Not finished but trigger the signal for anybody listening apr_thread_cond_broadcast(q->opque->op.base.ctl->cond); } log_printf(15, "_opque_cb: END qid=%d gid=%d\n", gop_id(&(q->opque->op)), gop_id(gop)); flush_log(); unlock_opque(q); }
int main(int argc, char **argv) { Stack stack; int *data, i; /***************************************************************************** * * * Initialize the stack. * * * *****************************************************************************/ stack_init(&stack, free); /***************************************************************************** * * * Perform some stack operations. * * * *****************************************************************************/ fprintf(stdout, "Pushing 10 elements\n"); for (i = 0; i < 10; i++) { if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = i + 1; if (stack_push(&stack, data) != 0) return 1; } print_stack(&stack); fprintf(stdout, "Popping 5 elements\n"); for (i = 0; i < 5; i++) { if (stack_pop(&stack, (void **)&data) == 0) free(data); else return 1; } print_stack(&stack); fprintf(stdout, "Pushing 100 and 200\n"); if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = 100; if (stack_push(&stack, data) != 0) return 1; if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = 200; if (stack_push(&stack, data) != 0) return 1; print_stack(&stack); if ((data = stack_peek(&stack)) != NULL) fprintf(stdout, "Peeking at the top element...Value=%03d\n", *data); else fprintf(stdout, "Peeking at the top element...Value=NULL\n"); print_stack(&stack); fprintf(stdout, "Popping all elements\n"); while (stack_size(&stack) > 0) { if (stack_pop(&stack, (void **)&data) == 0) free(data); } if ((data = stack_peek(&stack)) != NULL) fprintf(stdout, "Peeking at an empty stack...Value=%03d\n", *data); else fprintf(stdout, "Peeking at an empty stack...Value=NULL\n"); print_stack(&stack); /***************************************************************************** * * * Destroy the stack. * * * *****************************************************************************/ fprintf(stdout, "Destroying the stack\n"); stack_destroy(&stack); return 0; }
/** local data type declaration and local struct, union, enum section **/ /** local function prototype section **/ /** exported global variable definition section **/ /** local global variable definition section **/ /** exported function implementation section **/ void test_stack(void) { { stack_t t_sk = create_stack(int); stack_init(&t_sk); printf("stack1 - size:%u, empty:%d\n", stack_size(&t_sk), stack_empty(&t_sk)); stack_push(&t_sk, 100); printf("top:%d\n", *(int*)stack_top(&t_sk)); *(int*)stack_top(&t_sk) = 3333; printf("top:%d\n", *(int*)stack_top(&t_sk)); stack_destroy(&t_sk); } { stack_t t_st = create_stack(int); stack_init(&t_st); stack_push(&t_st, 1); stack_push(&t_st, 2); stack_push(&t_st, 3);
cache_page_t *_lru_new_page(cache_t *c, segment_t *seg) { cache_lru_t *cp = (cache_lru_t *)c->fn.priv; cache_segment_t *s = (cache_segment_t *)seg->priv; page_lru_t *lp; cache_page_t *p; type_malloc_clear(lp, page_lru_t, 1); p = &(lp->page); p->curr_data = &(p->data[0]); p->current_index = 0; type_malloc_clear(p->curr_data->ptr, char, s->page_size); cp->bytes_used += s->page_size; p->priv = (void *)lp; p->seg = seg; // p->offset = -1; p->offset = atomic_dec(lru_dummy); atomic_set(p->bit_fields, C_EMPTY); //** This way it's not accidentally deleted //** Store my position push(cp->stack, p); lp->ele = get_ptr(cp->stack); log_printf(15, " seg=" XIDT " page created initial->offset=" XOT " page_size=" XOT " data[0]=%p bytes_used=" XOT " stack_size=%d\n", segment_id(seg), p->offset, s->page_size, p->curr_data->ptr, cp->bytes_used, stack_size(cp->stack)); return(p); }
void FpuStackSim::push(int rnr) { if (TraceFPUStack) { tty->print("FPU-push %d", rnr); print(); tty->cr(); } assert(regs_at(stack_size()) == EMPTY, "should be empty"); set_regs_at(stack_size(), rnr); inc_stack_size(); }
int main (int argc, char ** argv) { stack_t stack; size_t i, size, j; void * tmp; /* Create a new stack */ stack = stack_init (); if (stack == NULL) { printf ("Initial Memory allocation failed\n"); return 99; } /* Test an empty stack */ test_empty (stack); /* Small Number Test */ size = 10; for (i = 0; i < size; i++) stack_push (stack, (void*)i); if (size != stack_size (stack)) { printf ("Small Number: Size Failure\n"); return EXIT_FAILURE; } for (i = size; i > 0; i--) if (stack_peek (stack) != (void*)(i-1) || stack_pop (stack) != (void*)(i-1)) { printf ("Small Number: Integrity Failure\n"); return EXIT_FAILURE; } /* Make sure stack is empty */ test_empty (stack); /* Clear Test */ size = 100; for (i = 0; i < size; i++) stack_push (stack, (void*)i); stack_clear (stack); test_empty (stack); /* Many data test */ size = 10000; for (i = 0; i < size; i++) { stack_push (stack, (void*)i); for (j = 0; j < 32; j++) stack_push (stack, (void*)(i+j)); for (j = 32; j > 0; j--) if ((tmp = stack_peek (stack)) != (void*)(j+i-1) || (tmp = stack_pop (stack)) != (void*)(j+i-1)) { printf ("Many Data: Integrity Failure\nExpected: %p Got: %p\n", j+i-1, tmp); return EXIT_FAILURE; } } if (size != stack_size (stack)) { printf ("Many Data: Size Failure\nExpected: %lu Got: %lu\n", size, stack_size (stack)); return EXIT_FAILURE; } for (i = size; i > 0; i--) if ((tmp = stack_peek (stack)) != (void*)(i-1) || (tmp = stack_pop (stack)) != (void*)(i-1)) { printf ("Many Data: Integrity Failure\nExpected: %p Got %p\n", i-1, tmp); return EXIT_FAILURE; } /* Destroy the stack */ stack_destroy (stack); return EXIT_SUCCESS; }