w_err_t wind_msgbox_destroy(w_msgbox_s *msgbox) { w_err_t err; w_dnode_s *dnode; w_thread_s *thread; WIND_ASSERT_RETURN(msgbox != W_NULL,W_ERR_PTR_NULL); WIND_ASSERT_RETURN(msgbox->obj.magic == WIND_MSGBOX_MAGIC,W_ERR_INVALID); wind_notice("destroy msgbox:%s",msgbox->obj.name?msgbox->obj.name:"null"); thread = wind_thread_current(); WIND_ASSERT_RETURN(msgbox->owner == thread,W_ERR_FAIL); err = wind_obj_deinit(&msgbox->obj,WIND_MSGBOX_MAGIC,&msgboxlist); WIND_ASSERT_RETURN(err == W_ERR_OK, W_ERR_FAIL); thread = msgbox->owner; if((msgbox->owner->runstat == THREAD_STATUS_SLEEP) && (msgbox->owner->cause == CAUSE_MSG)) { thread->runstat = THREAD_STATUS_READY; } dnode = dlist_head(&msgbox->msglist); if(dnode != W_NULL) { wind_warn("msgbox:%s is NOT empty while destroying it.",msgbox->obj.name?msgbox->obj.name:"null"); } if(IS_F_MSGBOX_POOL(msgbox)) msgbox_free(msgbox); return W_ERR_OK; }
static LakeVal *_lambda(LakeCtx *ctx, Env *env, LakeList *expr) { /* (lambda (a b c) ...) */ if (LIST_N(expr) >= 3 && lake_is_type(TYPE_LIST, LIST_VAL(expr, 1))) { list_shift(expr); /* drop the "lambda" symbol */ LakeList *params = LIST(list_shift(expr)); LakeList *body = expr; return VAL(fn_make(params, NULL, body, env)); } else if (LIST_N(expr) >= 3 && lake_is_type(TYPE_DLIST, LIST_VAL(expr, 1))) { list_shift(expr); /* drop the "lambda" symbol */ LakeDottedList *def = DLIST(list_shift(expr)); LakeList *params = dlist_head(def); LakeSym *varargs = SYM(dlist_tail(def)); LakeList *body = expr; return VAL(fn_make(params, varargs, body, env)); } else if (LIST_N(expr) >= 3 && lake_is_type(TYPE_SYM, LIST_VAL(expr, 1))) { list_shift(expr); /* drop the "lambda" symbol */ LakeSym *varargs = SYM(list_shift(expr)); LakeList *body = expr; return VAL(fn_make(list_make(), varargs, body, env)); } else { invalid_special_form(expr, "lambda requires at least 2 parameters"); return NULL; } }
void* spellcast_hash_iterator_next(hash_iterator *h_iter) { int bucket, i; listElement *element; while (h_iter->index != h_iter->htable->buckets) { bucket = hash_key(h_iter->htable, h_iter->index); while (dlist_size(&h_iter->htable->table[bucket]) != h_iter->inner_index) { element = dlist_head(&h_iter->htable->table[bucket]); for (i = 0; i < h_iter->inner_index; i++) { element = dlist_next(element); } h_iter->inner_index++; return dlist_data(element); } h_iter->inner_index = 0; h_iter->index++; } h_iter->index = 0; return NULL; }
DList* postorder(QuadTree* tree){ Stack *s = malloc(sizeof(Stack)); dlist_init(s, &free); Queue *q = malloc(sizeof(Queue)); dlist_init(q, &free); push(s, tree->root); QuadTreeNode** current_data = malloc(sizeof(QuadTreeNode*)); while(pop(s, (void**)current_data) != -1){ if(quadtree_is_visited(*current_data)){ enqueue(q, *current_data); }else{ quadtree_mark(*current_data); push(s, *current_data); if(!quadtree_is_leaf(*current_data)){ DList* children = (*current_data)->children_list; DListElmt* current_elem = dlist_head(children); while(current_elem != NULL){ push(s, current_elem->data); current_elem = dlist_next(current_elem); } } } } return q; }
pSlipObject s_NewString(pSlip gd, uint8_t *data, int length) { pSlipObject obj; DLElement *e; e = dlist_head(gd->lstStrings); while (e != NULL) { obj = dlist_data(e); e = dlist_next(e); if (obj->data.string.length == length) { if ( memcmp(obj->data.string.data, data, length) == 0) return obj; } } obj = s_NewObject(gd); obj->type = eType_STRING; obj->data.string.data = malloc(length + 4); memmove(obj->data.string.data, data, 1+length); obj->data.string.length = length; dlist_ins(gd->lstStrings, obj); return obj; }
int dlist_remove(DList *list, DListNode *element, void **data) { if (dlist_size(list) == 0 || element == NULL) { printf("dlist_remove error for dlist_size(list) = 0 || element = NULL"); return -1; } *data = element->data; if (dlist_head(list) == element) { list->head = element->next; if (list->head == NULL) list->tail = NULL; else element->next->prev = list->head; } else { element->prev->next = element->next; if (element->next == NULL) list->tail = element->prev; else element->next->prev = element->prev; } free(element); list->size--; return 0; }
void print_list(const DList *list) { DListElmt *element; int *data, i; /***************************************************************************** * * * Display the doubly-linked list. * * * *****************************************************************************/ fprintf(stdout, "List size is %d\n", dlist_size(list)); i = 0; element = dlist_head(list); while (1) { data = dlist_data(element); fprintf(stdout, "list[%03d]=%03d\n", i, *data); i++; if (dlist_is_tail(element)) break; else element = dlist_next(element); } return; }
void list_plugins(const char *const nick) { notice(nick, "%d plugins loaded:", dlist_length(&plugins)); DLINK_FOREACH(node, dlist_head(&plugins)) { const struct plugin_handle_t *const p = dlink_data(node); notice(nick, "%s - %s", p->plugin->name, p->file_name); } }
static pSlipEnvironment get_global_environment(pSlip gd) { pSlipEnvironment env; DLElement *e; e = dlist_head(gd->lstGlobalEnvironment); env = dlist_data(e); return env; }
void dlist_destroy(Dlist *dlist) { void *data; while(dlist_size(dlist)>0) { if(dlist_remove(dlist,dlist_head(dlist),(void **)&data)==0 && dlist->destroy != NULL) dlist->destroy(data); } memset(dlist,0,sizeof(Dlist)); return ; }
int spellcast_htable_lookup(sp_hash_table *ht, int key, void **data) { int bucket = hash_key(ht, key); listElement *element; for (element = dlist_head(&ht->table[bucket]); element != NULL; element = dlist_next(element)) { if (ht->match(key, dlist_data(element))) { *data = dlist_data(element); return 0; } } *data = NULL; return -1; }
void dlist_free(dlist *d) { /* call dlist_remove_head until dlist_head(d) is NULL, then free the dlist structure itself */ if(d == NULL) { return; } while(dlist_head(d) != NULL) { dlist_remove_head(d); } free(d->head); free(d->tail); free(d); }
w_err_t wind_msgbox_trydestroy(w_msgbox_s *msgbox) { w_dnode_s *dnode; w_thread_s *thread; WIND_ASSERT_RETURN(msgbox != W_NULL,W_ERR_PTR_NULL); WIND_ASSERT_RETURN(msgbox->obj.magic == WIND_MSGBOX_MAGIC,W_ERR_INVALID); thread = wind_thread_current(); WIND_ASSERT_RETURN(msgbox->owner == thread,W_ERR_FAIL); wind_disable_switch(); dnode = dlist_head(&msgbox->msglist); if(dnode != W_NULL) { wind_enable_switch(); return W_ERR_FAIL; } wind_enable_switch(); return wind_msgbox_destroy(msgbox); }
static pSlipValue ScanForVar(pSlipObject o, pSlipEnvironment env) { DLElement *e; pSlipValue v; e = dlist_head(env->lstVars); while (e != NULL) { v = dlist_data(e); e = dlist_next(e); if (v->var == o) { return v; } } return NULL; }
int spellcast_htable_remove(sp_hash_table *ht, int key, void **data) { int bucket = hash_key(ht, key); listElement *element; for (element = dlist_head(&ht->table[bucket]); element != NULL; element = dlist_next(element)) { if (ht->match(key, dlist_data(element))) { if (dlist_remove(&ht->table[bucket], element, data) == 0) { ht->size--; return 0; } else { return -1; } } } return -1; }
state_t __PICK(context_t *ctx) { int u; if (popnum(ctx->ds, &u)) { dlist_elem_t *element = dlist_head(ctx->ds); while (u >= 0 && element != NULL) { if (u == 0) { int *num = dlist_data(element); pushnum(ctx->ds, *num); return OK; } u--; element = dlist_next(element); } } return error(ctx, -11); // result out of range }
static LakeVal *_define(LakeCtx *ctx, Env *env, LakeList *expr) { /* TODO: make these more robust, check all expected params */ /* (define x 42) */ if (LIST_N(expr) == 3 && lake_is_type(TYPE_SYM, LIST_VAL(expr, 1))) { list_shift(expr); /* drop the "define" symbol */ LakeSym *var = SYM(list_shift(expr)); LakeVal *form = list_shift(expr); env_define(env, var, eval(ctx, env, form)); } /* (define (inc x) (+ 1 x)) */ else if (LIST_N(expr) >= 3 && lake_is_type(TYPE_LIST, LIST_VAL(expr, 1))) { list_shift(expr); /* drop the "define" symbol */ LakeList *params = LIST(list_shift(expr)); LakeSym *var = SYM(list_shift(params)); LakeList *body = expr; env_define(env, var, VAL(fn_make(params, NULL, body, env))); } /* (define (print format . args) (...)) */ else if (LIST_N(expr) >= 3 && lake_is_type(TYPE_DLIST, LIST_VAL(expr, 1))) { list_shift(expr); /* drop the "define" symbol */ LakeDottedList *def = DLIST(list_shift(expr)); LakeList *params = dlist_head(def); LakeSym *varargs = SYM(dlist_tail(def)); LakeSym *var = SYM(list_shift(params)); LakeList *body = expr; env_define(env, var, VAL(fn_make(params, varargs, body, env))); } else { invalid_special_form(expr, "define requires at least 2 parameters"); } return NULL; }
static int dump_visit_list(int destroy) { struct dlist *list = visit_list; struct dlist_node *node; int i = 0; if (!list) return; for (node = dlist_head(list); node != NULL; node = node->next) { fprintf(stdout, "%4d ", *(int *) node->data); if (!(++i % 16)) fprintf(stdout, "\n"); } fprintf(stdout, "\n"); if (destroy) { dlist_destroy(list); dlist_init(visit_list, NULL); } return i; }
pSlipObject s_NewSymbol(pSlip gd, uint8_t *data) { pSlipObject obj; DLElement *e; e = dlist_head(gd->lstSymbols); while (e != NULL) { obj = dlist_data(e); e = dlist_next(e); if (strcmp(obj->data.symbol.value, data) == 0) return obj; } obj = s_NewObject(gd); obj->type = eType_SYMBOL; obj->data.symbol.value = strdup(data); dlist_ins(gd->lstSymbols, obj); return obj; }
int stack_push(stack_t *stack, const void *data) { return dlist_ins_prev(stack, dlist_head(stack), data); }
void dlist_remove_head(dlist *d) { dlist_remove(d, dlist_head(d)); }
static int TokeniseBuffer(pSlip ctx, char *buff, uint32_t bufflen) { char *z; pToken t; int rc = -1; z = buff; // cleans out token stream, resets info. slip_reset_parser(ctx); do { int flag; flag = 0; t = PP_IDToken(ctx, z); if (t != NULL) { t->line = ctx->parse_data.current_line; if (t->id == kNEWLINE) { // TODO: Test multiple newline tokens in one... ctx->parse_data.current_line += strlen(t->z); } if (t->id == kCOMMENT_START) { ctx->parse_data.comment_depth += 1; if (ctx->parse_data.comment_depth > 1) { printf("Nested comments unsupported\n"); exit(0); } z = ctx->parse_data.buff_start + strlen(t->z); FreeToken(t); t = (void*) 1; flag = 2; } else if (t->id == kCOMMENT_END) { ctx->parse_data.comment_depth -= 1; if (ctx->parse_data.comment_depth < 0) { printf("Unmatched comments\n"); exit(0); } z = ctx->parse_data.buff_start + strlen(t->z); FreeToken(t); t = (void*) 1; flag = 2; } if (flag == 0 && ctx->parse_data.comment_depth == 1000) { z = ctx->parse_data.buff_start + strlen(t->z); if (t->id == kNEWLINE) ctx->parse_data.comment_depth = 0; FreeToken(t); t = (void*) 1; flag = 2; } else if (flag == 0 && ctx->parse_data.comment_depth >= 1) { if(strlen(t->z) == 0) ctx->parse_data.buff_start += 1; z = ctx->parse_data.buff_start + strlen(t->z); // skip token! FreeToken(t); t = (void*) 1; flag = 2; } else if (flag == 0 && t->id == kCOMMENT_LINE) { z = ctx->parse_data.buff_start + strlen(t->z); // skip token! if (ctx->parse_data.comment_depth == 0) ctx->parse_data.comment_depth = 1000; FreeToken(t); t = (void*) 1; flag = 2; } else if (flag == 0) { if (strlen(t->z) > 0) { if (t->id != kNEWLINE && ctx->parse_data.comment_depth == 0) dlist_ins(ctx->parse_data.lstTokens, t); z = ctx->parse_data.buff_start + strlen(t->z); } } else { if (flag == 1) z = ctx->parse_data.buff_start + strlen(t->z); } } } while (t != NULL); if (ctx->parse_data.comment_depth > 0) { printf("Unclosed comments\n"); } rc = 0; ctx->parse_data.eCurrentToken = dlist_head(ctx->parse_data.lstTokens); return rc; }
int stack_pop(stack_t *stack, void **data) { return dlist_remove(stack, dlist_head(stack), data); }
int main(int argc, char **argv) { DList list; DListElmt *element; int *data, i; /***************************************************************************** * * * Initialize the doubly-linked list. * * * *****************************************************************************/ dlist_init(&list, free); /***************************************************************************** * * * Perform some doubly-linked list operations. * * * *****************************************************************************/ element = dlist_head(&list); for (i = 10; i > 0; i--) { if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = i; if (dlist_ins_prev(&list, dlist_head(&list), data) != 0) return 1; } print_list(&list); element = dlist_head(&list); for (i = 0; i < 8; i++) element = dlist_next(element); data = dlist_data(element); fprintf(stdout, "Removing an element after the one containing %03d\n", *data); if (dlist_remove(&list, element, (void **)&data) != 0) return 1; print_list(&list); fprintf(stdout, "Inserting 011 at the tail of the list\n"); *data = 11; if (dlist_ins_next(&list, dlist_tail(&list), data) != 0) return 1; print_list(&list); fprintf(stdout, "Removing an element at the tail of the list\n"); element = dlist_tail(&list); if (dlist_remove(&list, element, (void **)&data) != 0) return 1; print_list(&list); fprintf(stdout, "Inserting 012 just before the tail of the list\n"); *data = 12; if (dlist_ins_prev(&list, dlist_tail(&list), data) != 0) return 1; print_list(&list); fprintf(stdout, "Iterating and removing the fourth element\n"); element = dlist_head(&list); element = dlist_next(element); element = dlist_prev(element); element = dlist_next(element); element = dlist_prev(element); element = dlist_next(element); element = dlist_next(element); element = dlist_next(element); if (dlist_remove(&list, element, (void **)&data) != 0) return 1; print_list(&list); fprintf(stdout, "Inserting 013 before the first element\n"); *data = 13; if (dlist_ins_prev(&list, dlist_head(&list), data) != 0) return 1; print_list(&list); fprintf(stdout, "Removing an element at the head of the list\n"); if (dlist_remove(&list, dlist_head(&list), (void **)&data) != 0) return 1; print_list(&list); fprintf(stdout, "Inserting 014 just after the head of the list\n"); *data = 14; if (dlist_ins_next(&list, dlist_head(&list), data) != 0) return 1; print_list(&list); fprintf(stdout, "Inserting 015 two elements after the head of the list\n"); if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = 15; element = dlist_head(&list); element = dlist_next(element); if (dlist_ins_next(&list, element, data) != 0) return 1; print_list(&list); i = dlist_is_head(dlist_head(&list)); fprintf(stdout, "Testing dlist_is_head...Value=%d (1=OK)\n", i); i = dlist_is_head(dlist_tail(&list)); fprintf(stdout, "Testing dlist_is_head...Value=%d (0=OK)\n", i); i = dlist_is_tail(dlist_tail(&list)); fprintf(stdout, "Testing dlist_is_tail...Value=%d (1=OK)\n", i); i = dlist_is_tail(dlist_head(&list)); fprintf(stdout, "Testing dlist_is_tail...Value=%d (0=OK)\n", i); /***************************************************************************** * * * Destroy the doubly-linked list. * * * *****************************************************************************/ fprintf(stdout, "Destroying the list\n"); dlist_destroy(&list); return 0; }