static void test_list_lpop() { // Setup list_t *list = list_new(); list_node_t *a = list_rpush(list, list_node_new("a")); list_node_t *b = list_rpush(list, list_node_new("b")); list_node_t *c = list_rpush(list, list_node_new("c")); // Assertions assert(3 == list->len); assert(a == list_lpop(list)); assert(2 == list->len); assert(b == list->head); assert(NULL == list->head->prev && "new head node prev is not NULL"); assert(NULL == a->prev && "detached node prev is not NULL"); assert(NULL == a->next && "detached node next is not NULL"); assert(b == list_lpop(list)); assert(1 == list->len); assert(c == list_lpop(list)); assert(0 == list->len); assert(NULL == list->head); assert(NULL == list->tail); assert(NULL == list_lpop(list)); assert(0 == list->len); }
static void test_list_remove() { // Setup list_t *list = list_new(); list_node_t *a = list_rpush(list, list_node_new("a")); list_node_t *b = list_rpush(list, list_node_new("b")); list_node_t *c = list_rpush(list, list_node_new("c")); // Assertions assert(list->len == 3); list_remove(list, b); assert(list->len == 2); assert(list->head == a); assert(list->tail == c); assert(a->next == c); assert(a->prev == NULL); assert(c->next == NULL); assert(c->prev == a); list_remove(list, a); assert(list->len == 1); assert(list->head == c); assert(list->tail == c); assert(c->next == NULL); assert(c->prev == NULL); list_remove(list, c); assert(list->len == 0); assert(list->head == NULL); assert(list->tail == NULL); }
static void test_list_find() { // Setup list_t *langs = list_new(); list_node_t *js = list_rpush(langs, list_node_new("js")); list_node_t *ruby = list_rpush(langs, list_node_new("ruby")); list_t *users = list_new(); users->match = User_equal; User userTJ = { "tj" }; User userSimon = { "simon" }; User userTaylor = { "taylor" }; list_node_t *tj = list_rpush(users, list_node_new(&userTJ)); list_node_t *simon = list_rpush(users, list_node_new(&userSimon)); // Assertions list_node_t *a = list_find(langs, "js"); list_node_t *b = list_find(langs, "ruby"); list_node_t *c = list_find(langs, "foo"); assert(a == js); assert(b == ruby); assert(c == NULL); a = list_find(users, &userTJ); b = list_find(users, &userSimon); c = list_find(users, &userTaylor); assert(a == tj); assert(b == simon); assert(c == NULL); }
/****************************************************************************** **函数名称: list_insert **功 能: 在PREV后插入结点 **输入参数: ** list: 单向链表 ** prev: 前一结点 ** data: 数据 **输出参数: NONE **返 回: 0:成功 !0:失败 **实现描述: **注意事项: ** 1. 如果结点prev为NULL, 则将结点node插入链表头 ** 2. 如果结点prev为tail, 则将结点node插入链表尾 ** 3. 如果结点prev不为NULL, 则将结点node插入prev后 **作 者: # Qifeng.zou # 2014.09.24 # ******************************************************************************/ int list_insert(list_t *list, list_node_t *prev, void *data) { list_node_t *node; /* > 插入链头或链尾 */ if (NULL == prev) { return list_lpush(list, data); } else if (list->tail == prev) { return list_rpush(list, data); } /* > 新建结点 */ node = list->alloc(list->pool, sizeof(list_node_t)); if (NULL == node) { return -1; } node->data = data; /* > 插入链表 */ node->next = prev->next; prev->next = node; ++list->num; return 0; }
int list__construct(data_t **args, argc_t argc, data_t *ret, scope_t *scope) { argc_t i; data_t *data; ret->value.list = gc_add_list(scope->gc, list_new()); check_mem(ret->value.list); for (i = 0; i < argc; i++) { if (!types_identical(args[0]->type, args[i]->type)) { huserr__raise("typeErr", "list items must have one type", NULL, scope); goto error; } data = gc_add(scope->gc, malloc(sizeof(data_t))); check_mem(data); data->type = typedup(args[i]->type, scope->gc); data->value = args[i]->value; check_mem(list_rpush(ret->value.list, list_node_new(data))); } ret->type = construct_type(tid_list, (argc > 0) ? itta(stt{ typedup(args[0]->type, scope->gc) }, 1, scope->gc) : NULL, scope->gc); return 0; error: return -1; }
static inline list_t * parse_package_deps(JSON_Object *obj) { if (NULL == obj) return NULL; list_t *list = list_new(); if (NULL == list) { return NULL; } for (size_t i = 0; i < json_object_get_count(obj); i++) { const char *name = json_object_get_name(obj, i); if (!name) { list_destroy(list); return NULL; } const char *version = json_object_get_string_safe(obj, name); if (!version) { list_destroy(list); return NULL; } clib_package_dependency_t *dep = clib_package_dependency_new(name, version); if (NULL == dep) { list_destroy(list); return NULL; } list_node_t *node = list_node_new(dep); list_rpush(list, node); } return list; }
int list__tail(data_t **args, argc_t argc, data_t *ret, scope_t *scope) { (void) argc; list_node_t *node; list_iterator_t *it = list_iterator_new(args[0]->value.list, LIST_HEAD); check_mem(it); ret->type = typedup(args[0]->type, scope->gc); ret->value.list = gc_add_list(scope->gc, list_new()); node = list_iterator_next(it); while ((node = list_iterator_next(it))) { list_rpush(ret->value.list, list_node_new(node->val)); } if (it) list_iterator_destroy(it); return 0; error: if (it) list_iterator_destroy(it); return -1; }
/****************************************************************************** **函数名称: rtrd_rsvr_keepalive_req_hdl **功 能: 保活请求处理 **输入参数: ** ctx: 全局对象 **输出参数: NONE **返 回: 0:成功 !0:失败 **实现描述: **注意事项: **作 者: # Qifeng.zou # 2015.01.01 # ******************************************************************************/ static int rtrd_rsvr_keepalive_req_hdl(rtrd_cntx_t *ctx, rtrd_rsvr_t *rsvr, rtrd_sck_t *sck) { void *addr; rtmq_header_t *head; /* > 分配消息空间 */ addr = calloc(1, sizeof(rtmq_header_t)); if (NULL == addr) { log_error(rsvr->log, "Alloc memory from slab failed!"); return RTMQ_ERR; } /* > 回复消息内容 */ head = (rtmq_header_t *)addr; head->type = RTMQ_KPALIVE_RSP; head->nodeid = ctx->conf.nodeid; head->length = 0; head->flag = RTMQ_SYS_MESG; head->checksum = RTMQ_CHECK_SUM; /* > 加入发送列表 */ if (list_rpush(sck->mesg_list, addr)) { FREE(addr); log_error(rsvr->log, "Insert into list failed!"); return RTMQ_ERR; } log_debug(rsvr->log, "Add respond of keepalive request!"); return RTMQ_OK; }
static void wiki_registry_iterate_nodes(GumboNode *node, list_t *packages, char *category) { if (node->type != GUMBO_NODE_ELEMENT) return; if (node->v.element.tag == GUMBO_TAG_A) { GumboAttribute* name = gumbo_get_attribute(&node->v.element.attributes, "name"); if (name) { // set the current category for next iteration size_t len = strlen(name->value); category = realloc(category, len + 1); memcpy(category, name->value, len); category[len] = 0; } else { package_t *pkg = package_from_wiki_anchor(node); if (pkg) { pkg->category = strdup(category); list_node_t *pkg_node = list_rpush(packages, list_node_new(pkg)); } } } else { GumboVector* children = &node->v.element.children; for (int i = 0; i < children->length; ++i) { wiki_registry_iterate_nodes(children->data[i], packages, category); } } }
static inline list_t * parse_package_deps(JSON_Object *obj) { list_t *list = NULL; if (!obj) goto done; if (!(list = list_new())) goto done; list->free = clib_package_dependency_free; for (unsigned int i = 0; i < json_object_get_count(obj); i++) { const char *name = NULL; char *version = NULL; clib_package_dependency_t *dep = NULL; int error = 1; if (!(name = json_object_get_name(obj, i))) goto loop_cleanup; if (!(version = json_object_get_string_safe(obj, name))) goto loop_cleanup; if (!(dep = clib_package_dependency_new(name, version))) goto loop_cleanup; if (!(list_rpush(list, list_node_new(dep)))) goto loop_cleanup; error = 0; loop_cleanup: if (version) free(version); if (error) { list_destroy(list); list = NULL; break; } } done: return list; }
static int rtrd_rsvr_get_conn_list_by_nodeid(rtrd_sck_t *sck, _conn_list_t *cl) { if (sck->nodeid != cl->nodeid) { return -1; } return list_rpush(cl->list, sck); /* 注意: 销毁cl->list时, 不必释放sck空间 */ }
static int header_field_cb(http_parser *p, const char *buf, size_t len) { ssdp_packet_t* pkt = SSDP_PACKET_PTR(p); header_t* hdr = header_alloc(); if (hdr) { strncpy(hdr->field, buf, MIN(sizeof(hdr->field), len)); list_node_t* node = list_node_new(hdr); list_rpush(pkt->headers, node); } return 0; }
void case_list_rpush() { struct list *list = list(); char *s1 = "s1", *s2 = "s2", *s3 = "s3"; assert(list_rpush(list, s1) == LIST_OK); assert(list->head->data == s1); assert(list->tail->data == s1); assert(list_len(list) == 1); assert(list_rpush(list, s2) == LIST_OK); assert(list->head->data == s1); assert(list->tail->data == s2); assert(list_len(list) == 2); assert(list_rpush(list, s3) == LIST_OK); assert(list->head->data == s1); assert(list->tail->data == s3); assert(list_len(list) == 3); list_free(list); }
void app_chat_server(){ canvas2 = (Canvas *)sdk_prc_getCanvas(); sdk_scr_printfXY(canvas2, 0, 0, "Starting server.\n"); while (1){ int addr; char msg[1024]; memset(msg, 0, 1024 * sizeof(char)); int recv_size = sdk_nic_recv(msg, 1024, &addr); if ((strlen(msg) > 0) && (strcmp(msg, "> SEARCH CR CHAT SERVER") == 0)){ sdk_scr_printf(canvas2, "New client addr: %d. Reply.\n", addr); sdk_nic_send(addr, "< SEARCH RESPONSE", strlen("< SEARCH RESPONSE")); } if ((strlen(msg) > 0) && (strncmp(msg, "> NICKNAME", 8) == 0)){ sdk_scr_printf(canvas2, "name: %s\n", &msg[11]); ClientInfo * cl = malloc(sizeof(ClientInfo)); memset(cl, 0, sizeof(ClientInfo)); cl->addr = addr; strncpy(cl->name, &msg[10], strlen(&msg[10])); if (list_clients == 0){ list_clients = list_new((void*)cl); } list_rpush(list_clients, list_node_new((void*)cl)); sendMsg(addr, "< NEWUSER %s", cl->name); } if ((strlen(msg) > 0) && (strncmp(msg, "> MESSAGE ", 8) == 0)){ sdk_scr_printf(canvas2, "msg: %s\n", &msg[10]); //sender name ClientInfo * author = NULL; list_node_t *node; list_iterator_t *it = list_iterator_new(list_clients, LIST_HEAD); while ((node = list_iterator_next(it))) { author = (ClientInfo*)node->val; if (author->addr == addr){ break; } } list_iterator_destroy(it); sendMsg(addr, "< MESSAGE %s: %s", author->name, &msg[10]); } } }
static void crawl(GumboTag tag, list_t *elements, GumboNode *node) { NODE_TYPE_CHECK(node); if (tag == node->v.element.tag) list_rpush(elements, list_node_new(node)); GumboVector *children = &node->v.element.children; for (unsigned int i = 0; i < children->length; i++) { crawl(tag, elements, children->data[i]); } }
static void test_list_iterator_t() { // Setup list_t *list = list_new(); list_node_t *tj = list_node_new("tj"); list_node_t *taylor = list_node_new("taylor"); list_node_t *simon = list_node_new("simon"); // tj taylor simon list_rpush(list, tj); list_rpush(list, taylor); list_rpush(list, simon); // Assertions // From head list_iterator_t *it = list_iterator_new(list, LIST_HEAD); list_node_t *a = list_iterator_next(it); list_node_t *b = list_iterator_next(it); list_node_t *c = list_iterator_next(it); list_node_t *d = list_iterator_next(it); assert(a == tj); assert(b == taylor); assert(c == simon); assert(d == NULL); // From tail it = list_iterator_new(list, LIST_TAIL); list_node_t *a2 = list_iterator_next(it); list_node_t *b2 = list_iterator_next(it); list_node_t *c2 = list_iterator_next(it); list_node_t *d2 = list_iterator_next(it); assert(a2 == simon); assert(b2 == taylor); assert(c2 == tj); assert(d2 == NULL); list_iterator_destroy(it); }
void list_insertAt(list l, int index, void* val) { if (l->len <= index) list_push(l, val); else if (index <= 0) list_rpush(l, val); else { list_node i = __list_getNthNode(l, index); list_node n = calloc(1, sizeof(struct list_node)); __node_insertBetween(n, i->prev, i); n->value = val; l->len += 1; } }
static void test_list_at() { // Setup list_t *list = list_new(); list_node_t *a = list_node_new("a"); list_node_t *b = list_node_new("b"); list_node_t *c = list_node_new("c"); // a b c list_rpush(list, a); list_rpush(list, b); list_rpush(list, c); // Assertions assert(a == list_at(list, 0)); assert(b == list_at(list, 1)); assert(c == list_at(list, 2)); assert(NULL == list_at(list, 3)); assert(c == list_at(list, -1)); assert(b == list_at(list, -2)); assert(a == list_at(list, -3)); assert(NULL == list_at(list, -4)); }
void sendMessageToFocused(PRC_MESSAGE type, int reason, int value){ krn_getIdleProcess()->sync_lock = TRUE; Process * prc; list_node_t * node; list_iterator_t * it = list_iterator_new(listPrcLoop, LIST_HEAD); while (node = list_iterator_next(it)){ // send to currently focused process if (currFocusedProc == node->val){ unsigned int msg = type << 24 | reason << 16 | value; prc = node->val; list_rpush(prc->list_msgs, list_node_new((void*)msg)); } } list_iterator_destroy(it); krn_getIdleProcess()->sync_lock = FALSE; }
void timers_add_timer(unsigned int timer_number, unsigned int ms, F_TIMER_CBACK timer_cback){ if (timers_list == NULL) timers_list = list_new(); STR_TIMER * str_timer = (STR_TIMER *)calloc(sizeof(STR_TIMER)); str_timer->timer_number = timer_number; str_timer->ms = ms; str_timer->cback = timer_cback; str_timer->last_tick = sdk_clk_timeSinceBoot(); //str_timer->owner_process = sdk_prc_ list_rpush(timers_list, list_node_new(str_timer)); int nearest = getNearestTimerDelta(); if (nearest > 0){ sdk_debug_logf("ADD TIMER %d, MS: %d", timer_number, ms); sdk_clk_setCountdownTimer(APP_TIMER_RESERVED, nearest, FALSE); } }
int main(int argc, char *argv[]) { list_t *plist = list_create(); list_rpush(plist, "test"); char* value = list_rpop(plist); if(value) { printf("%s\n", value); list_remove(plist, value); } value = list_rpop(plist); if(value == NULL) { printf("no value!\n"); } list_destroy(&plist); return 0; }
/****************************************************************************** **函数名称: rtrd_rsvr_link_auth_rsp **功 能: 链路鉴权应答 **输入参数: ** ctx: 全局对象 ** rsvr: 接收服务 ** sck: 套接字对象 **输出参数: NONE **返 回: 0:成功 !0:失败 **实现描述: 将链路应答信息放入发送队列中 **注意事项: **作 者: # Qifeng.zou # 2015.05.22 # ******************************************************************************/ static int rtrd_rsvr_link_auth_rsp(rtrd_cntx_t *ctx, rtrd_rsvr_t *rsvr, rtrd_sck_t *sck) { void *addr; rtmq_header_t *head; rtmq_link_auth_rsp_t *link_auth_rsp; /* > 分配消息空间 */ addr = calloc(1, sizeof(rtmq_header_t) + sizeof(rtmq_link_auth_rsp_t)); if (NULL == addr) { log_error(rsvr->log, "Alloc memory from slab failed!"); return RTMQ_ERR; } /* > 回复消息内容 */ head = (rtmq_header_t *)addr; link_auth_rsp = (rtmq_link_auth_rsp_t *)(head + 1); head->type = RTMQ_LINK_AUTH_RSP; head->nodeid = ctx->conf.nodeid; head->length = sizeof(rtmq_link_auth_rsp_t); head->flag = RTMQ_SYS_MESG; head->checksum = RTMQ_CHECK_SUM; link_auth_rsp->is_succ = htonl(sck->auth_succ); /* > 加入发送列表 */ if (list_rpush(sck->mesg_list, addr)) { FREE(addr); log_error(rsvr->log, "Insert into list failed!"); return RTMQ_ERR; } log_debug(rsvr->log, "Add respond of link-auth request!"); return RTMQ_OK; }
/* * Messages queue */ void sendMessageToAll(PRC_MESSAGE type, int reason, int value){ krn_getIdleProcess()->sync_lock = TRUE; Process * prc; list_node_t * node; list_iterator_t * it = list_iterator_new(listPrcLoop, LIST_HEAD); while (node = list_iterator_next(it)){ /*PrcMessage * msg = malloc(sizeof(PrcMessage)); msg->type = type; msg->reason = reason; msg->value = value;*/ unsigned int msg = type << 24 | reason << 16 | value; prc = node->val; list_rpush(prc->list_msgs, list_node_new((void*)msg)); /*size_t us, fr, ma; _getmemstats(&us, &fr, &ma); krn_debugLogf("us: %d, fr: %d, ma: %d\n", us, fr, ma);*/ } list_iterator_destroy(it); krn_getIdleProcess()->sync_lock = FALSE; }
static void test_list_destroy() { // Setup list_t *a = list_new(); list_destroy(a); // a b c list_t *b = list_new(); list_rpush(b, list_node_new("a")); list_rpush(b, list_node_new("b")); list_rpush(b, list_node_new("c")); list_destroy(b); // Assertions list_t *c = list_new(); c->free = freeProxy; list_rpush(c, list_node_new(list_node_new("a"))); list_rpush(c, list_node_new(list_node_new("b"))); list_rpush(c, list_node_new(list_node_new("c"))); list_destroy(c); assert(freeProxyCalls == 3); }
/****************************************************************************** **函数名称: rtrd_rsvr_dist_data **功 能: 分发连接队列中的数据 **输入参数: ** ctx: 全局对象 ** rsvr: 接收服务 **输出参数: NONE **返 回: 0:成功 !0:失败 **实现描述: **注意事项: **作 者: # Qifeng.zou # 2015.06.02 # ******************************************************************************/ static int rtrd_rsvr_dist_data(rtrd_cntx_t *ctx, rtrd_rsvr_t *rsvr) { #define RTRD_POP_MAX_NUM (1024) int len, idx, num; queue_t *sendq; void *addr; void *data[RTRD_POP_MAX_NUM]; rtmq_frwd_t *frwd; list_opt_t opt; rtrd_sck_t *sck; _conn_list_t cl; rtmq_header_t *head; sendq = ctx->sendq[rsvr->id]; while (1) { /* > 弹出队列数据 */ num = MIN(queue_used(sendq), RTRD_POP_MAX_NUM); if (0 == num) { break; } num = queue_mpop(sendq, data, num); if (0 == num) { continue; } log_trace(ctx->log, "Multi-pop num:%d!", num); /* > 逐条处理数据 */ for (idx=0; idx<num; ++idx) { frwd = (rtmq_frwd_t *)data[idx]; /* > 查找发送连接 */ memset(&opt, 0, sizeof(opt)); opt.pool = (void *)NULL; opt.alloc = (mem_alloc_cb_t)mem_alloc; opt.dealloc = (mem_dealloc_cb_t)mem_dealloc; cl.nodeid = frwd->dest; cl.list = list_creat(&opt); if (NULL == cl.list) { queue_dealloc(sendq, data[idx]); log_error(rsvr->log, "Create list failed!"); continue; } list2_trav(rsvr->conn_list, (list2_trav_cb_t)rtrd_rsvr_get_conn_list_by_nodeid, &cl); if (0 == cl.list->num) { queue_dealloc(sendq, data[idx]); list_destroy(cl.list, NULL, mem_dummy_dealloc); log_error(rsvr->log, "Didn't find connection by nodeid [%d]!", cl.nodeid); continue; } sck = (rtrd_sck_t *)list_fetch(cl.list, rand()%cl.list->num); /* > 设置发送数据 */ len = sizeof(rtmq_header_t) + frwd->length; addr = calloc(1, len); if (NULL == addr) { queue_dealloc(sendq, data[idx]); list_destroy(cl.list, NULL, mem_dummy_dealloc); log_error(rsvr->log, "Alloc memory from slab failed!"); continue; } head = (rtmq_header_t *)addr; head->type = frwd->type; head->nodeid = frwd->dest; head->flag = RTMQ_EXP_MESG; head->checksum = RTMQ_CHECK_SUM; head->length = frwd->length; memcpy(addr+sizeof(rtmq_header_t), data[idx]+sizeof(rtmq_frwd_t), head->length); queue_dealloc(sendq, data[idx]); /* > 放入发送链表 */ if (list_rpush(sck->mesg_list, addr)) { FREE(addr); log_error(rsvr->log, "Push input list failed!"); } list_destroy(cl.list, NULL, mem_dummy_dealloc); /* 无需是否结点数据空间 */ } } return RTMQ_OK; }
clib_package_t * clib_package_new(const char *json, int verbose) { if (!json) return NULL; clib_package_t *pkg = malloc(sizeof(clib_package_t)); if (!pkg) return NULL; JSON_Value *root = json_parse_string(json); if (!root) { clib_package_free(pkg); return NULL; } JSON_Object *json_object = json_value_get_object(root); if (!json_object) { if (verbose) clib_package_error("error", "unable to parse json"); json_value_free(root); clib_package_free(pkg); return NULL; } pkg->json = str_copy(json); pkg->name = json_object_get_string_safe(json_object, "name"); pkg->repo = NULL; pkg->repo = json_object_get_string_safe(json_object, "repo"); if (NULL != pkg->repo) { pkg->author = clib_package_parse_author(pkg->repo); // repo name may not be the package's name. for example: // stephenmathieson/str-replace.c -> str-replace pkg->repo_name = clib_package_parse_name(pkg->repo); // TODO support npm-style "repository"? } else { if (verbose) clib_package_warn("warning", "missing repo in package.json"); pkg->author = NULL; pkg->repo_name = NULL; } pkg->version = json_object_get_string_safe(json_object, "version"); pkg->license = json_object_get_string_safe(json_object, "license"); pkg->description = json_object_get_string_safe(json_object, "description"); pkg->install = json_object_get_string_safe(json_object, "install"); JSON_Array *src = json_object_get_array(json_object, "src"); if (src) { pkg->src = list_new(); if (!pkg->src) { json_value_free(root); clib_package_free(pkg); return NULL; } for (size_t i = 0; i < json_array_get_count(src); i++) { char *file = json_array_get_string_safe(src, i); if (!file) break; // TODO fail? list_node_t *node = list_node_new(file); list_rpush(pkg->src, node); } } else { pkg->src = NULL; } JSON_Object *deps = json_object_get_object(json_object, "dependencies"); pkg->dependencies = parse_package_deps(deps); JSON_Object *devs = json_object_get_object(json_object, "development"); pkg->development = parse_package_deps(devs); json_value_free(root); return pkg; }
int list__unzip(data_t **args, argc_t argc, data_t *ret, scope_t *scope) { (void) argc; ret->type = construct_type(tid_tuple, itta(stt{ construct_type(tid_list, itta(stt{ typedup(((data_t *) args[0]->value.list->head->val)->value.tuple[0]->type, scope->gc) }, 1, scope->gc), scope->gc), construct_type(tid_list, itta(stt{ typedup(((data_t *) args[0]->value.list->head->val)->value.tuple[1]->type, scope->gc) }, 1, scope->gc), scope->gc), }, 2, scope->gc), scope->gc); check_mem(ret->type); ret->value.tuple = gc_add(scope->gc, malloc(sizeof(data_t *) * 3)); check_mem(ret->value.list); ret->value.tuple[0] = gc_add(scope->gc, malloc(sizeof(data_t))); check_mem(ret->value.tuple[0]); ret->value.tuple[1] = gc_add(scope->gc, malloc(sizeof(data_t))); check_mem(ret->value.tuple[0]); ret->value.tuple[0]->type = construct_type(tid_list, itta(stt{ typedup(((data_t *) args[0]->value.list->head->val)->value.tuple[0]->type, scope->gc) }, 1, scope->gc), scope->gc); ret->value.tuple[0]->value.list = gc_add(scope->gc, list_new()); check_mem(ret->value.tuple[0]->value.list); ret->value.tuple[1]->type = construct_type(tid_list, itta(stt{ typedup(((data_t *) args[0]->value.list->head->val)->value.tuple[1]->type, scope->gc) }, 1, scope->gc), scope->gc); ret->value.tuple[1]->value.list = gc_add(scope->gc, list_new()); check_mem(ret->value.tuple[1]->value.list); ret->value.tuple[2] = NULL; list_node_t *node; list_iterator_t *it = list_iterator_new(args[0]->value.list, LIST_HEAD); while ((node = list_iterator_next(it))) { list_rpush(ret->value.tuple[0]->value.list, gc_add(scope->gc, list_node_new( copy_data(((data_t *) node->val)->value.tuple[0], scope)))); list_rpush(ret->value.tuple[1]->value.list, gc_add(scope->gc, list_node_new( copy_data(((data_t *) node->val)->value.tuple[1], scope)))); } // ret->value.tuple = gc_add(scope->gc, malloc(sizeof(data_t *) * 3)); // check_mem(ret->value.tuple); // ret->value.tuple[0] = gc_add(scope->gc, malloc(sizeof(data_t))); // check_mem(ret->value.tuple[0]); // ret->value.tuple[0]->type = tid_tuple; // ret->value.tuple[0]->value.tuple = gc_add(scope->gc, malloc(sizeof(data_t *) * (length + 1))); // check_mem(ret->value.tuple[0]->value.tuple); // ret->value.tuple[1] = gc_add(scope->gc, malloc(sizeof(data_t))); // check_mem(ret->value.tuple[1]); // ret->value.tuple[1]->type = tid_tuple; // ret->value.tuple[1]->value.tuple = gc_add(scope->gc, malloc(sizeof(data_t *) * (length + 1))); // check_mem(ret->value.tuple[1]->value.tuple); // ret->value.tuple[2] = NULL; // list_node_t *node; // list_iterator_t *it = list_iterator_new(args[0]->value.list, LIST_HEAD); // int i = 0; // while ((node = list_iterator_next(it))) { // if (((data_t *) node->val)->type != tid_tuple) { // scope->error = gc_add(scope->gc, malloc(sizeof(huserr_t))); // scope->error->name = "typeErr"; // scope->error->msg = "must be a zipped list"; // scope->error->token = NULL; // goto error; // } // ret->value.tuple[0]->value.tuple[i] = copy_data(((data_t *) node->val)->value.tuple[0], scope); // check_mem(ret->value.tuple[0]->value.tuple[i]); // ret->value.tuple[1]->value.tuple[i] = copy_data(((data_t *) node->val)->value.tuple[1], scope); // check_mem(ret->value.tuple[1]->value.tuple[i]); // ++i; // } // ret->value.tuple[0]->value.tuple[i] = NULL; // ret->value.tuple[1]->value.tuple[i] = NULL; if (it) list_iterator_destroy(it); return 0; error: if (it) list_iterator_destroy(it); return -1; }
int json_array_push (json_array_t *self, json_value_t *value) { list_node_t *node = list_node_new(value); list_rpush(self->list, node); return (self->length = self->list->len); }
/*! * Draft of multiprocessing stuff */ Process * prc_create(const char * name, uint32_t stackSize, uint32_t heapSize, uint32_t * entryPoint, Usermode mode, uint32_t * arg_line, uint32_t * exist_canvas){ if (krn_is_init() == TRUE) krn_getIdleProcess()->sync_lock = TRUE; Process * prc = calloc(sizeof(Process)); //memset(prc, 0, sizeof(Process)); // allocate stack prc->stack = calloc(stackSize * sizeof(char)); prc->heap = calloc(heapSize * sizeof(char) + heapSize); // info strcpy(prc->name, name); prc->pid = totalPIDs++; prc->context = calloc (sizeof(Ctx)); // copy argument line if (arg_line != 0){ krn_debugLogf("arg line: %s", (char*)arg_line); int arg_line_size = (strlen((char*) arg_line) + 2) * sizeof(char); prc->arg_line = calloc(arg_line_size); //memset(prc->arg_line, 0, arg_line_size); strcpy(prc->arg_line, (char*)arg_line); } // allocate screen buffer ScreenInfo info = hw_scr_screenInfo(); prc->screen = calloc(sizeof(ScreenInfo)); if (exist_canvas == NULL){ prc->screen->addr = calloc(info.res_hor * info.res_ver * info.bytes_per_char*2); prc->screen->res_hor = info.res_hor; prc->screen->res_ver = info.res_ver; prc->screen->bytes_per_char = info.bytes_per_char; prc->screen->cur_x = 0; prc->screen->cur_y = 0; prc->exist_canvas = FALSE; } else { prc->screen = (ScreenInfo *)exist_canvas; prc->exist_canvas = TRUE; } // new process should take a screen hw_scr_mapScreenBuffer(prc->screen->addr); hw_scr_setTextColor(prc->screen, SCR_COLOR_GREEN); hw_scr_setBackColor(prc->screen, SCR_COLOR_BLACK); prc->i_should_die = FALSE; // sutup context stuffs prc->context->gregs[CPU_REG_SP] = (uint32_t)&prc->stack[stackSize - 1]; prc->context->gregs[CPU_REG_PC] = (uint32_t)entryPoint; prc->context->flags = mode; prc->context->gregs[0] = (unsigned int) prc->arg_line; // insert process to scheduler if (listPrcLoop == NULL){ // empty scheduler listPrcLoop = list_new(); } if (stackFocused == NULL){ // empty focused processes stack stackFocused = list_new(); } list_rpush(stackFocused, list_node_new(prc)); currFocusedProc = prc; // set up events queue prc->list_msgs = list_new(); // set up first created process (idle) as current. needed in scheduler. if (currProc == NULL){ currProc = list_rpush(listPrcLoop, list_node_new(prc)); } else { list_rpush(listPrcLoop, list_node_new(prc)); } _mem_init(prc->heap, heapSize * sizeof(char), 0); // synchrinization flag prc->sync_lock = FALSE; if (krn_is_init() == TRUE) krn_getIdleProcess()->sync_lock = FALSE; return prc; }
int Mailbox(int sid) { int sock; struct sockaddr_sv mboxaddr, cliaddr; socklen_t addrlen = sizeof(cliaddr); if ((sock = socket_sv(AF_SERVAL, SOCK_DGRAM, SERVAL_PROTO_UDP)) < 0) { fprintf(stderr, "error creating AF_SERVAL socket: %s\n", strerror_sv(errno)); return -1; } memset(&mboxaddr, 0, sizeof(mboxaddr)); mboxaddr.sv_family = AF_SERVAL; mboxaddr.sv_srvid.s_sid32[0] = htonl(sid); set_reuse_ok(sock); if (bind_sv(sock, (struct sockaddr *)&mboxaddr, sizeof(mboxaddr)) < 0) { fprintf(stderr, "error binding socket: %s\n", strerror_sv(errno)); close_sv(sock); return -1; } printf("mailbox: bound to service id %d\n", sid); memset(&cliaddr, 0, sizeof(cliaddr)); payload p; int n; list_node_t *pl; while (1) { n = recvfrom_sv(sock, &p, sizeof(payload), 0, (struct sockaddr *)&cliaddr, &addrlen); if (n == -1) { fprintf(stderr, "recvfrom: %s\n", strerror_sv(errno)); return -1; } /* if (n == 0) { fprintf(stderr, "server: received EOF"); break; } */ printf("Received a %zd byte packet number %d type %d from \'%s\' \n", n, p.num, p.type, service_id_to_str(&cliaddr.sv_srvid)); //rbuf[n] = '\0'; if (p.type==DATA) { pl = list_node_new(&p); list_rpush(dataPackets,pl); } printf("length of buffer: %d\n", dataPackets->len); } close_sv(sock); return -1; }