int ins_node(yadb_t* db, char* key, char* val) { int ret = 0; node_t* node = _new_node(); if (node) { upd_node(node, key, val); if (db->tail == NULL) { db->tail = node; } if (db->head == NULL) { db->head = node; } else { node->next = db->head; db->head->prev = node; db->head = node; } db->size++; ret = 1; } return ret; }
/* * REQUIRES: * - cannot put duplicate key, deal it in judge_callback */ void skiplist_put(struct skiplist *sl, void *key) { int i; int height; struct skipnode *x; struct skipnode *prev[SKIPLIST_MAX_LEVEL]; memset(prev, 0, sizeof(struct skipnode*) * SKIPLIST_MAX_LEVEL); x = skiplist_find_greater_or_equal(sl, key, prev); height = _rand_height(); /* init prev arrays */ if (height > _get_height(sl)) { for (i = _get_height(sl); i < height; i++) prev[i] = sl->header; _set_height(&sl->height, &height); } x = _new_node(sl, height); x->key = key; for (i = 0; i < height; i++) { _set_next(&x->next[i], prev[i]->next[i]); _set_next(&prev[i]->next[i], x); } sl->count++; }
struct skiplist *skiplist_new(struct mempool *mpool, SKIPLIST_COMPARE_CALLBACK compare_cb) { struct skiplist *sl; sl = xcalloc(1, sizeof(*sl)); sl->mpool = mpool; sl->header = _new_node(sl, SKIPLIST_MAX_LEVEL); sl->compare_cb = compare_cb; sl->height = 1; return sl; }
Graph *load_graph(Database *db){ if(db == NULL){ puts("Error: load graph failed. Db is null."); exit(1); } Graph *graph = _new_graph(); graph->db = db; graph->nodes = NULL; int i; uint64_t count = 0; uint64_t total = 0; NodeData *node_data; ArcData *arc_data; Node *node; Arc *arc; ArcNode *arcnode; puts("Loading graph into memory..."); fseek(db->fp, 0L, SEEK_SET); while(!feof(db->fp)){ node_data = malloc(sizeof(NodeData)); count = read_database(node_data, sizeof(NodeData), db); total += count; if(count == 0){ continue; } HASH_FIND_INT(graph->nodes, &(node_data->id), node); if(node != NULL){ printf("Error: node %i already in tree.\n", node_data->id); exit(1); } node = _new_node(node_data, graph); HASH_ADD_INT(graph->nodes, data->id, node); if(node_data->num_arcnodes > 0){ for(i=0; i<node_data->num_arcnodes; i++){ arc_data = malloc(sizeof(ArcData)); total += read_database((void *)arc_data, sizeof(ArcData), db); arc = _new_arc(arc_data); arcnode = _new_arcnode(arc); node->arcnodes[i] = arcnode; } } } printf("Loaded %i nodes.\n", (int)HASH_COUNT(graph->nodes)); printf("Read %llu bytes from database.\n", (long long unsigned int)total); return graph; }
//ms int timer_add(int time, void (*cb)(void *ud), void *ud) { struct timer_node *n = _new_node(time, cb, ud); if (n == NULL) return -1; while (__sync_lock_test_and_set(&TIMER->lock, 1)) ; n->next = TIMER->list; TIMER->list = n; __sync_lock_release(&TIMER->lock); return 0; }
static int _refill_pool(allocman_t *alloc, utspace_split_t *split, struct utspace_split_node **heads, size_t size_bits, uintptr_t paddr) { struct utspace_split_node *node; struct utspace_split_node *left, *right; int sel4_error; if (paddr == ALLOCMAN_NO_PADDR) { /* see if pool is actually empty */ if (heads[size_bits]) { return 0; } } else { /* see if the pool has the paddr we want */ for (node = heads[size_bits]; node; node = node->next) { if (node->paddr <= paddr && paddr < node->paddr + BIT(size_bits)) { return 0; } } } /* ensure we are not the highest pool */ if (size_bits >= sizeof(seL4_Word) * 8 - 2) { /* bugger, no untypeds bigger than us */ ZF_LOGV("Failed to refill pool of size %zu, no larger pools", size_bits); return 1; } /* get something from the highest pool */ if (_refill_pool(alloc, split, heads, size_bits + 1, paddr)) { /* could not fill higher pool */ ZF_LOGV("Failed to refill pool of size %zu", size_bits); return 1; } if (paddr == ALLOCMAN_NO_PADDR) { /* use the first node for lack of a better one */ node = heads[size_bits + 1]; } else { for (node = heads[size_bits + 1]; node && !(node->paddr <= paddr && paddr < node->paddr + BIT(size_bits + 1)); node = node->next); /* _refill_pool should not have returned if this wasn't possible */ assert(node); } /* allocate two new nodes */ left = _new_node(alloc); if (!left) { ZF_LOGV("Failed to allocate left node"); return 1; } right = _new_node(alloc); if (!right) { ZF_LOGV("Failed to allocate right node"); _delete_node(alloc, left); return 1; } /* perform the first retype */ sel4_error = seL4_Untyped_Retype(node->ut.capPtr, seL4_UntypedObject, size_bits, left->ut.root, left->ut.dest, left->ut.destDepth, left->ut.offset, 1); if (sel4_error != seL4_NoError) { _delete_node(alloc, left); _delete_node(alloc, right); /* Well this shouldn't happen */ ZF_LOGE("Failed to retype untyped, error %d\n", sel4_error); return 1; } /* perform the second retype */ sel4_error = seL4_Untyped_Retype(node->ut.capPtr, seL4_UntypedObject, size_bits, right->ut.root, right->ut.dest, right->ut.destDepth, right->ut.offset, 1); if (sel4_error != seL4_NoError) { vka_cnode_delete(&left->ut); _delete_node(alloc, left); _delete_node(alloc, right); /* Well this shouldn't happen */ ZF_LOGE("Failed to retype untyped, error %d\n", sel4_error); return 1; } /* all is done. remove the parent and insert the children */ _remove_node(&heads[size_bits + 1], node); left->parent = right->parent = node; left->sibling = right; left->origin_head = &heads[size_bits]; right->origin_head = &heads[size_bits]; right->sibling = left; if (node->paddr != ALLOCMAN_NO_PADDR) { left->paddr = node->paddr; right->paddr = node->paddr + BIT(size_bits); } else { left->paddr = right->paddr = ALLOCMAN_NO_PADDR; } /* insert in this order so that we end up pulling the untypeds off in order of contiugous * physical address. This makes various allocation problems slightly less likely to happen */ _insert_node(&heads[size_bits], right); _insert_node(&heads[size_bits], left); return 0; }
static int _refill_pool(allocman_t *alloc, utspace_split_t *split, uint32_t size_bits) { struct utspace_split_node *node; struct utspace_split_node *left, *right; int sel4_error; /* see if pool is actually empty */ if (split->heads[size_bits]) { return 0; } /* ensure we are not the highest pool */ if (size_bits >= 30) { /* bugger, no untypeds bigger than us */ return 1; } /* get something from the highest pool */ if (_refill_pool(alloc, split, size_bits + 1)) { /* could not fill higher pool */ return 1; } /* use the first node for lack of a better one */ node = split->heads[size_bits + 1]; /* allocate two new nodes */ left = _new_node(alloc); if (!left) { return 1; } right = _new_node(alloc); if (!right) { _delete_node(alloc, left); return 1; } /* perform the first retype */ #if defined(CONFIG_KERNEL_STABLE) sel4_error = seL4_Untyped_RetypeAtOffset(node->ut.capPtr, seL4_UntypedObject, 0, size_bits, left->ut.root, left->ut.dest, left->ut.destDepth, left->ut.offset, 1); #else sel4_error = seL4_Untyped_Retype(node->ut.capPtr, seL4_UntypedObject, size_bits, left->ut.root, left->ut.dest, left->ut.destDepth, left->ut.offset, 1); #endif if (sel4_error != seL4_NoError) { _delete_node(alloc, left); _delete_node(alloc, right); /* Well this shouldn't happen */ return 1; } /* perform the second retype */ #if defined(CONFIG_KERNEL_STABLE) sel4_error = seL4_Untyped_RetypeAtOffset(node->ut.capPtr, seL4_UntypedObject, BIT(size_bits), size_bits, right->ut.root, right->ut.dest, right->ut.destDepth, right->ut.offset, 1); #else sel4_error = seL4_Untyped_Retype(node->ut.capPtr, seL4_UntypedObject, size_bits, right->ut.root, right->ut.dest, right->ut.destDepth, right->ut.offset, 1); #endif if (sel4_error != seL4_NoError) { vka_cnode_delete(&left->ut); _delete_node(alloc, left); _delete_node(alloc, right); /* Well this shouldn't happen */ return 1; } /* all is done. remove the parent and insert the children */ _remove_node(&split->heads[size_bits + 1], node); left->parent = right->parent = node; left->sibling = right; right->sibling = left; if (node->paddr) { left->paddr = node->paddr; right->paddr = node->paddr + BIT(size_bits); } else { left->paddr = right->paddr = 0; } /* insert in this order so that we end up pulling the untypeds off in order of contiugous * physical address. This makes various allocation problems slightly less likely to happen */ _insert_node(&split->heads[size_bits], right); _insert_node(&split->heads[size_bits], left); return 0; }