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_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); }
/* Allocate and return a new list. */ list* list_new() { list* l = malloc(sizeof(list)); l->head = list_node_new(NULL); l->tail = list_node_new(NULL); l->length = 0; return l; }
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); }
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; }
/** Prepend data to list. * * Create a new list node and prepend it at the beginning of the list. * * @param list Linked list. * @param data Data for the new node. */ void list_prepend(list_t *list, void *data) { list_node_t *node; node = list_node_new(data); list_node_insert_between(node, list->head.prev, &list->head); }
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; }
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; }
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 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; }
list_t* parseServerUserGetUser(int sock,void* buf,list_t** outResult){ uint16_t length = ntohs(*(uint16_t*)buf); buf += sizeof(uint16_t); char *txt = malloc(length); memcpy(txt,buf,length); buf += length; cJSON* json = cJSON_Parse(txt); if(!json){ free(txt); return NULL; } list_t* list = *outResult; if(list == NULL) list = list_new(); int i = 0; for(i=0;i<cJSON_GetArraySize(json);i++){ user_info_t* uinfo = malloc(sizeof(user_info_t)); char* str = cJSON_GetObjectItem(json,"username")->valuestring; if(str != NULL){ strncpy(uinfo->username,str,strlen(str)); } str = cJSON_GetObjectItem(json,"userid")->valuestring; if(str != NULL){ strncpy(uinfo->userid,str,strlen(str)); } cJSON* arr = cJSON_GetObjectItem(json,"drivces"); uinfo->drivces = list_new(); uinfo->drivces->free=free; int i=0; for(i=0;i<cJSON_GetArraySize(arr);i++){ str = cJSON_GetArrayItem(arr,i)->valuestring; char* drivce = malloc(strlen(str)); memset(drivce,0,strlen(str)); list_lpush(uinfo->drivces,list_node_new(drivce)); } list_lpush(list,list_node_new(uinfo)); } *outResult = list; free(txt); cJSON_Delete(json); return list; }
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]); } }
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 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 sendMessage(char* clientMessageid,int delytime, char* message, int messagetype, list_t* sendtoList){ send_message_t* ms = malloc(sizeof(send_message_t)); ms->message = malloc(strlen(message)); strcpy(ms->message,message); strcpy(ms->messageid,clientMessageid); ms->messagetype = messagetype; ms->delytime = delytime; ms->sendto = list_new(); ms->sendto->free = free; int i = 0; for(i=0;i<sendtoList->len;i++){ list_node_t* node = list_at(sendtoList,i); char* str = malloc(strlen(node->val)); list_lpush(ms->sendto,list_node_new(str)); } list_lpush(sendlist,list_node_new(ms)); pthread_cond_signal(&queue_send); pthread_mutex_unlock(&send_lock); }
user_info_t* parseServerUserFindUser(int sock,void* buf,int* outResult){ uint16_t length = ntohs(*(uint16_t*)buf); buf += sizeof(uint16_t); char *txt = malloc(length); memcpy(txt,buf,length); buf += length; cJSON* json = cJSON_Parse(txt); if(!json){ free(txt); return NULL; } int isSucess = cJSON_GetObjectItem(json,"result")->valueint; user_info_t* info = NULL; if(isSucess){ info = malloc(sizeof(user_info_t)); char* str = cJSON_GetObjectItem(json,"username")->valuestring; strncpy(info->username,str,strlen(str)); str = cJSON_GetObjectItem(json,"userid")->valuestring; strncpy(info->userid,str,strlen(str)); // info->drivces = list_new(); CJSON* arr = cJSON_GetObjectItem(json,"driveces"); int i = 0; for(i=0;i<cJSON_GetArraySize(arr);i++){ str = cJSON_GetArrayItem(arr,i)->valuestring; char* drivceId = malloc(strlen(str)+1); list_lpush(info->drivces,list_node_new(drivceId)); } info->drivces->free = free; } *outResult = isSucess; /* int isSucess = cJSON_GetObjectItem(json,"result")->valueint; char* username = NULL; char* str = cJSON_GetObjectItem(json,"username")->valuestring; if(str != NULL){ username = malloc(strlen(str)+1); strncpy(username,str,strlen(str)); } //free(str); //str = cJSON_GetObjectItem(json,"userid")->valuestring; //char* userid = malloc(strlen(str)+1); */ *outResult = isSucess; free(txt); cJSON_Delete(json); return info;//*outResult; }
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)); }
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); }
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); } }
/* Create a list node from the specified element and append it to the list. */ int list_add(list* l, void* e) { list_node* n = list_node_new(e); if (l == NULL || e == NULL) { return 0; } if (n != NULL) { if (l->head->next == NULL) { l->head->next = n; } if (l->tail != NULL) { l->tail->next = n; } l->tail = n; l->length++; return 1; } return 0; }
/* * 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; }
/** * Parse a sequence of headers, and set them on the `req`. * * It is assumed that the `parser` has just encountered a * YAML_SCALAR_EVENT whose value was "headers", and has been * advanced beyond the YAML_SEQUENCE_START_EVENT; after this * are expected a series of YAML_SCALAR_EVENT pairs, the * header names and values. * * After return, the `parser` will have consumed the matching * YAML_SEQUENCE_END_EVENT. * * Return 1 on error or 0 on success. */ int parse_headers(yaml_parser_t* parser, request* req) { unsigned long i; char* full_header; yaml_event_t name_event, value_event; list* headers; node* n; header* hdr; headers = list_new(); while (1) { if (!yaml_parser_parse(parser, &name_event)) goto parse_headers_error; while (name_event.type == YAML_MAPPING_START_EVENT || name_event.type == YAML_MAPPING_END_EVENT) { yaml_event_delete(&name_event); if (!yaml_parser_parse(parser, &name_event)) goto parse_headers_error; } if (name_event.type == YAML_SEQUENCE_END_EVENT) break; if (name_event.type != YAML_SCALAR_EVENT) goto parse_headers_error; if (!yaml_parser_parse(parser, &value_event)) goto parse_headers_error; if (value_event.type != YAML_SCALAR_EVENT) { fprintf(stderr, "invalid header value for '%s'\n", name_event.data.scalar.value); goto parse_headers_error; } if (!(hdr = malloc(sizeof(header)))) goto parse_headers_error; if (!(hdr->name = malloc(sizeof(char) * (strlen((const char*)name_event.data.scalar.value) + 1)))) goto parse_headers_error; if (!(hdr->value = malloc(sizeof(char) * (strlen((const char*)value_event.data.scalar.value) + 1)))) goto parse_headers_error; strcpy(hdr->name, (const char*)name_event.data.scalar.value); strcpy(hdr->value, (const char*)value_event.data.scalar.value); n = list_node_new(hdr); list_push(headers, n); yaml_event_delete(&name_event); yaml_event_delete(&value_event); } if (!(req->headers = malloc(sizeof(header) * headers->length))) goto parse_headers_error; req->curl_headers = NULL; n = headers->head; for (i=0; i<headers->length; i++) { hdr = (header *)n->data; req->headers[i].name = hdr->name; req->headers[i].value = hdr->value; // also set up header list for libcurl full_header = malloc(sizeof(char) * (strlen(hdr->name) + strlen(hdr->value) + 3)); if (!full_header) goto parse_headers_error; sprintf(full_header, "%s: %s", hdr->name, hdr->value); req->curl_headers = curl_slist_append(req->curl_headers, full_header); if (req->curl_headers == NULL) goto parse_headers_error; free(full_header); n = n->next; } req->num_headers = headers->length; list_free(headers, 0); return 0; parse_headers_error: // printf("in parse_headers_error\n"); list_free(headers, 1); return 1; }
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_unshift (json_array_t *self, json_value_t *value) { list_node_t *node = list_node_new(value); list_lpush(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; }
/** * Parse a URL file into a requests */ requests parse_urls(const char* url_filename) { unsigned long i; requests reqs; request* req; list* req_list; node* n; FILE* urls; char done = 0; yaml_parser_t parser; yaml_event_t event; reqs.count = 0; reqs.reqs = NULL; req_list = list_new(); urls = fopen(url_filename, "r"); yaml_parser_initialize(&parser); yaml_parser_set_input_file(&parser, urls); while (!done) { if (!yaml_parser_parse(&parser, &event)) { break; } else { switch (event.type) { case YAML_MAPPING_START_EVENT: // printf("event.type: MAPPING_START\n"); // printf(" calling parse_request()\n"); if ((req = parse_request(&parser)) == NULL) { done = 2; } else { // printf(" got a good request!\n"); n = list_node_new(req); list_push(req_list, n); } break; case YAML_STREAM_START_EVENT: case YAML_DOCUMENT_START_EVENT: case YAML_SEQUENCE_START_EVENT: case YAML_SEQUENCE_END_EVENT: break; case YAML_STREAM_END_EVENT: case YAML_DOCUMENT_END_EVENT: done = 1; break; default: // printf("got event type %d\n", event.type); fprintf(stderr, "Malformed URLS_FILE at pos: %lu\n", (unsigned long)parser.offset); done = 2; } } yaml_event_delete(&event); } yaml_parser_delete(&parser); if (done == 2) { fprintf(stderr, "Error parsing URLs file\n"); exit(1); } if (!(reqs.reqs = malloc(sizeof(request) * req_list->length))) { list_free(req_list, 1); exit(2); } n = req_list->head; for (i=0; i<req_list->length; i++) { req = (request *)(n->data); reqs.reqs[i].method = req->method; reqs.reqs[i].url = req->url; reqs.reqs[i].payload_length = req->payload_length; reqs.reqs[i].payload = req->payload; reqs.reqs[i].headers = req->headers; reqs.reqs[i].num_headers = req->num_headers; reqs.reqs[i].curl_headers = req->curl_headers; // printf("set request %lu with url %s\n", i, reqs.reqs[i].url); n = n->next; } reqs.count = req_list->length; list_free(req_list, 0); return reqs; }
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; }