} END_TEST // Ensure a single value can be prepended and then shfited off one // ✔ Data should be as expected // ✔ Size should be 0 START_TEST (test_prepend_1_shift_1_list) { kld_list_t * list = (kld_list_t *) new_list(); char * buf = "test data"; list_prepend(list, buf); list_shift(list); fail_if(list->size != 0, "Unexpected list size"); fail_if(!list_is_empty(list), "List doesn't report as empty"); } END_TEST
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 void quote_stats(irc_t *irc, const char *channel, const char *user, list_t *list) { const char *who = list_shift(list); if (!who) { int count = quote_length(); int request = database_request_count(irc, "QUOTES"); return irc_write(irc, channel, "%s: quote stats -> %d quotes -> requested %d times", user, count, request); } int count = 0; if (!quote_count(who, &count)) return irc_write(irc, channel, "%s: %s has no quotes", user, who); const char *plural = (count > 1) ? "quotes" : "quote"; if (strcmp(user, who)) irc_write(irc, channel, "%s: %s has %d %s", user, who, count, plural); else irc_write(irc, channel, "%s: you have %d %s", user, count, plural); }
} END_TEST // Ensure that shifting off a list returns the expected data. // ✔ Head should be updated // ✔ Tail should be updated // ✔ Data should be the same START_TEST (test_shift_list) { kld_list_t * list = (kld_list_t *) new_list(); char * buf = "test data"; char * buf2 = "test data2"; list_append(list, buf); list_append(list, buf2); kld_list_node_t * tmp = (kld_list_node_t *) list_shift(list); fail_if(tmp == NULL, "Returned value is null"); fail_if(tmp->data != "test data", "Unexpected data value for returned list node"); } END_TEST
int list_append( list_t *list_in, void *val_in ) { /* If there isn't enough memory allocated */ if( list_in->size + 1 > list_in->alloc ) { if( list_grow( list_in, LIST_SIZE_INC ) == -1 ) return -1; } else /* If there is enough, then either there is some at the beginning or some at the end */ { if( (byte_t*)list_in->start > (byte_t*)list_in->pointer ) if( list_shift( list_in, -1 ) == -1 ) return -1; } bcopy( val_in, (byte_t*) list_in->start + list_in->size * list_in->step, list_in->step ); ++list_in->size; return list_in->size; }
// get the next element without actually removing it from the queeue. int peekQueue(simplequeue_t *msgqueue, void **data, int *size) { simplewrapper_t *swrap; pthread_mutex_lock(&(msgqueue->qlock)); if (msgqueue->cursize <= 0) { *size = 0; *data = NULL; pthread_mutex_unlock(&(msgqueue->qlock)); return EXIT_FAILURE; } else { swrap = list_shift(msgqueue->queue); *size = swrap->size; *data = &(swrap->data); list_unshift(msgqueue->queue, swrap); pthread_mutex_unlock(&(msgqueue->qlock)); return EXIT_SUCCESS; } }
static void utimer(void) { L4_KDB_SetThreadName(sos_my_tid(), "utimer"); L4_Accept(L4_UntypedWordsAcceptor); List *entryq; entryq = list_empty(); for (;;) { L4_Yield(); // Walk the timer list L4_Word_t now = L4_KDB_GetTick(); list_delete(entryq, processExpired, &now); // Wait for a new packet either blocking or non-blocking L4_MsgTag_t tag = L4_Niltag; if (list_null(entryq)) L4_Set_ReceiveBlock(&tag); else L4_Clear_ReceiveBlock(&tag); L4_ThreadId_t wait_tid = L4_nilthread; tag = L4_Ipc(L4_nilthread, L4_anythread, tag, &wait_tid); if (!L4_IpcFailed(tag)) { // Received a time out request queue it L4_Msg_t msg; L4_MsgStore(tag, &msg); // Get the message utimer_entry_t *entry = (utimer_entry_t *) L4_MsgWord(&msg, 0); entry->fTid = wait_tid; list_shift(entryq, entry); } else if (3 == L4_ErrorCode()) // Receive error # 1 continue; // no-partner - non-blocking else assert(!"Unhandled IPC error"); } }
int queue_dequeue(Queue *que, void **data){ return list_shift(que, data); }
void test(string args) { args = splitArg(args, 1); if(streql(args, "") || streql(args, "-H")) { print("\nThis file is in charge of testing the data types embedded in Q-OS.",black); print("\nAccepted Arguments:\n-list\tTests the list.c file\n-set \ttests the set.c file", black); print("\n-strb\ttests the strbuilder.c file\n-y \tshould return the current year...",black); } else if(streql(args, "-LIST"))//For testing lists { newline(); list_t test_list = list_init(); test_list.autoShrink = true; for(uint8 i = 0; i < 4; i++) { list_add(&test_list, "1"); list_add(&test_list, "2"); list_add(&test_list, "3"); list_add(&test_list, "4"); list_add(&test_list, "5"); list_add(&test_list, "6"); list_add(&test_list, "7"); list_add(&test_list, "8"); list_add(&test_list, "9"); list_add(&test_list, "10"); list_add(&test_list, "11"); list_add(&test_list, "12"); list_add(&test_list, "13"); list_add(&test_list, "14"); list_add(&test_list, "15"); list_add(&test_list, "16"); } list_add(&test_list, "Pointless"); println("Done sizing up", white); printint(test_list.capt, white); element_t t; for(uint8 i = 0; i < 64; i++) { t = list_shift(&test_list); } println("\nLast item deleted should be \"16\"", white); println(t.udata.strdata, white); println("\nDeleting all but element \"Pointless\"", white); for(uint8 i = 0; i < test_list.size; i++) { println(list_get(test_list, i), white); } println("Done resizing up", white); printint(test_list.capt, white); list_destroy(&test_list); } else if(streql(args,"-SET")) { set_t test_set = set_init(); for(uint8 i = 0; i < 4; i++) { set_add(&test_set, "0"); set_add(&test_set, "1"); set_add(&test_set, "2"); set_add(&test_set, "3"); set_add(&test_set, "4"); set_add(&test_set, "5"); set_add(&test_set, "6"); set_add(&test_set, "7"); set_add(&test_set, "8"); set_add(&test_set, "9"); set_add(&test_set, "10"); set_add(&test_set, "11"); set_add(&test_set, "12"); set_add(&test_set, "13"); set_add(&test_set, "14"); set_add(&test_set, "15"); set_add(&test_set, "16"); print("\nIteration: ", white); printint(i, white); } println("\n\nInsertion::Output should be 17", white); printint(test_set.size, white); set_t tmp = set_init(); set_add(&tmp, "Union item"); set_union(&test_set, &tmp); println("\n\nUnion::Output should be 18", white); printint(test_set.size, white); set_intersect(&test_set, &tmp); println("\n\nIntersect::Output should be 1", white); printint(test_set.size, white); println("\n\nPreparing for diff test", white); set_add(&test_set, "1"); set_add(&test_set, "2"); set_add(&test_set, "3"); set_add(&tmp, "2"); set_add(&tmp, "3"); set_add(&tmp, "4"); set_diff(&test_set, &tmp); println("Diff::Output should be 2", white); printint(test_set.size, white); set_destroy(&tmp); set_destroy(&test_set); } else if(streql(args, "-STRB")) { static const string bak = "Hello, world "; static const uint32 bln = 13; strbuilder_t test_strb = strbuilder_init(); strbuilder_append(&test_strb, bak); strbuilder_append(&test_strb, "Hello, 2nd world"); println("\nTesting backup text. Output should 1", red); printint(streql(bak, test_strb.prevTxt), green); println("\nOutput should be \"Hello, world Hello, 2nd world\"", red); println(strbuilder_tostr(test_strb), green); println("\nRemoving greeters from first world", red); strbuilder_delete(&test_strb, 0, bln); println("\nOutput should be \"Hello, 2nd world\"", red); println(strbuilder_tostr(test_strb), green); strbuilder_flip(&test_strb); println("\nOutput should be \"dlrow dn2 ,olleH\"", red); println(strbuilder_tostr(test_strb), green); list_t tmp = strbuilder_split(test_strb, " "); println("\nOutput should be last str split by spaces", red); for(uint8 i = 0; i < tmp.size; i++) { println(list_get(tmp, i), white); } list_destroy(&tmp); strbuilder_destroy(&test_strb); } else if(streql(args,"-Y")) { //getTime() test printint(getTime("year"),white); } }
/** Release a Strophe connection object. * Decrement the reference count by one for a connection, freeing the * connection object if the count reaches 0. * * @param conn a Strophe connection object * * @return TRUE if the connection object was freed and FALSE otherwise * * @ingroup Connections */ int xmpp_conn_release(xmpp_conn_t * const conn) { xmpp_ctx_t *ctx; list_t *item; xmpp_handler_t *temp; xmpp_handlist_t *hlitem, *thli; hash_iterator_t *iter; const char *key; if (conn->ref > 1) { conn->ref--; return 0; } ctx = conn->ctx; /* remove connection from context's connlist */ item = list_pop_by_data(ctx->connlist, (void *)conn); if (item) xmpp_free(ctx, item); else xmpp_error(ctx, "xmpp", "Connection not in context's list\n"); /* free handler stuff * note that userdata is the responsibility of the client * and the handler pointers don't need to be freed since they * are pointers to functions */ while ((item = list_shift(conn->timed_handlers))) { xmpp_free(ctx, item->data); xmpp_free(ctx, item); } list_destroy(conn->timed_handlers); /* id handlers * we have to traverse the hash table freeing list elements * then release the hash table */ iter = hash_iter_new(conn->id_handlers); while ((key = hash_iter_next(iter))) { hlitem = (xmpp_handlist_t *)hash_get(conn->id_handlers, key); while (hlitem) { thli = hlitem; hlitem = hlitem->next; xmpp_free(ctx, thli->id); xmpp_free(ctx, thli); } } hash_iter_release(iter); hash_release(conn->id_handlers); while ((item = list_shift(conn->handlers))) { temp = (xmpp_handler_t *)item->data; if (temp->ns) xmpp_free(ctx, temp->ns); if (temp->name) xmpp_free(ctx, temp->name); if (temp->type) xmpp_free(ctx, temp->type); xmpp_free(ctx, temp); xmpp_free(ctx, item); } list_destroy(conn->handlers); if (conn->stream_error) { xmpp_stanza_release(conn->stream_error->stanza); if (conn->stream_error->text) xmpp_free(ctx, conn->stream_error->text); xmpp_free(ctx, conn->stream_error); } parser_free(conn->parser); /* free send_queue */ list_destroy(conn->send_queue); if (conn->domain) xmpp_free(ctx, conn->domain); if (conn->jid) xmpp_free(ctx, conn->jid); if (conn->bound_jid) xmpp_free(ctx, conn->bound_jid); if (conn->pass) xmpp_free(ctx, conn->pass); if (conn->stream_id) xmpp_free(ctx, conn->stream_id); if (conn->lang) xmpp_free(ctx, conn->lang); if (conn->tls) tls_free(conn->tls); xmpp_free(ctx, conn); return 1; }