xkern_return_t bid_init( XObj self) { Part_s p; PState *ps = (PState *)self->state; xTrace0(bidp, TR_GROSS_EVENTS, "bid Init"); if ( ! (ps = pathAlloc(self->path, sizeof(PState))) ) { xTraceP0(self, TR_ERRORS, "allocation error"); return XK_FAILURE; } self->state = ps; if ( ! xIsProtocol(xGetDown(self, BID_XPORT_I)) ) { xTrace0(bidp, TR_ERRORS, "bid could not get transport protocol -- aborting init"); return XK_FAILURE; } if ( ! xIsProtocol(xGetDown(self, BID_CTL_I)) ) { xTrace0(bidp, TR_ERRORS, "bid could not get control protocol -- aborting init"); return XK_FAILURE; } if ( xControl(xGetDown(self, BID_CTL_I), BIDCTL_GET_LOCAL_BID, (char *)&ps->myBid, sizeof(BootId)) < (int)sizeof(BootId) ) { xTrace0(bidp, TR_ERRORS, "bid could not get my bid -- aborting init"); return XK_FAILURE; } #if BID_CHECK_CLUSTERID if ( xControl(xGetDown(self, BID_CTL_I), BIDCTL_GET_CLUSTERID, (char *)&ps->myCluster, sizeof(ClusterId)) < (int)sizeof(ClusterId) ) { xTrace0(bidp, TR_ERRORS, "bid could not get my clusterid -- aborting init"); return XK_FAILURE; } #endif /* BID_CHECK_CLUSTERID */ xTrace1(bidp, TR_GROSS_EVENTS, "bid using id == %x", ps->myBid); ps->activeMap = mapCreate(BID_ACTIVE_MAP_SIZE, sizeof(ActiveKey), self->path); ps->passiveMap = mapCreate(BID_PASSIVE_MAP_SIZE, sizeof(PassiveKey), self->path); if ( ! ps->activeMap || ! ps->passiveMap ) { xTraceP0(self, TR_ERRORS, "allocation error"); return XK_FAILURE; } semInit(&ps->sessnCreationSem, 1); getProtlFuncs(self); partInit(&p, 1); partPush(p, ANY_HOST, 0); if ( xOpenEnable(self, self, xGetDown(self, BID_XPORT_I), &p) == XK_FAILURE ) { xTrace0(bidp, TR_ERRORS, "openEnable failed in bid init"); return XK_FAILURE; } return XK_SUCCESS; }
Map *idtCreate() { Map *idt = mapCreate(sizeof(Instr), IDT_NUM_BUCKETS, (unsigned (*)(void *))djb2, // Hash function (int (*)(void *a, void *b))strcmp); // Comparison function // Real instructions Instr real_ins; real_ins.type = ins_t_real; for (int i = 0; i < NUM_REAL_INSTR; i++) { real_ins.data.real = real_md[i]; mapInsert(idt, real_symbols[i], strlen(real_symbols[i]) + 1, &real_ins); } // Pseudo instructions Instr pseudo_ins; pseudo_ins.type = ins_t_pseudo; for (int i = 0; i < NUM_PSEUDO_INSTR; i++) { pseudo_ins.data.pseudo = pseudo_instr[i]; mapInsert(idt, pseudo_symbols[i], strlen(pseudo_symbols[i]) + 1, &pseudo_ins); } return idt; }
/** * Performs the first pass of the assembly, reading the code and creating a sym- * bol table, containing each label in the code. * @param in Pointer to the file containing the code. It should already have * been opened in "r" mode and should point to the beginning of the * file. * @param idt Instruction Data Table created by idtCreate (idt.h) * @return Address to the newly created symbol table. */ Map *asmBuildSymTable(FILE *in, Map *idt){ Map *sym_table = mapCreate(sizeof(int), NUM_BUCKETS, (unsigned (*)(void *))djb2, // Hash function (int (*)(void *a, void *b))strcmp // Comparison function ); Line l; Instr ins; int ilc = 0; char buf[BUF_LEN + 1]; while (fgets(buf, BUF_LEN, in)){ parseLine(buf, &l); if (l.label){ mapInsert(sym_table, l.label, strlen(l.label) + 1, &ilc); } if (l.instr){ int get_status = mapGet(idt, l.instr, &ins); if (get_status == 1){ fprintf(stderr, "Invalid instruction: %s " "(ILC: %d)\n", l.instr, ilc); exit(1); } if (ins.type == ins_t_real){ ilc += 1 + ins.data.real.num_ops; } else { ilc += ins.data.pseudo.ilc_inc; } } } return sym_table; }
clusterMap_t clusterMapCreate(clusterMapResultHandler_t resultHandler) { clusterMapImpl_t* pCM = ALLOCATE_1(clusterMapImpl_t); if (pCM) { pCM->resultHandler = resultHandler; pCM->serverMap = mapCreate(); } return pCM; }
static void test() { struct map *m = mapCreate(); insert(m,"1st","Hello"); insert(m,"2nd","World"); struct map_op op; op.op = MAP_TRAVERSE; op.value = NULL; const char *v =NULL; while ((v=mapSearch(m,&op))!=NULL) { printf("%s : %s\n",(const char *)(op.key.p),v); } mapRelease(m); }
// Create peer manager object. static int peermgtCreate(struct s_peermgt *mgt, const int peer_slots, const int auth_slots, struct s_nodekey *local_nodekey, struct s_dh_state *dhstate) { int tnow = utilGetTime(); const char *defaultid = "default"; struct s_peermgt_data *data_mem; struct s_crypto *ctx_mem; if((peer_slots > 0) && (auth_slots > 0) && (peermgtSetNetID(mgt, defaultid, 7))) { data_mem = malloc(sizeof(struct s_peermgt_data) * (peer_slots + 1)); if(data_mem != NULL) { ctx_mem = malloc(sizeof(struct s_crypto) * (peer_slots + 1)); if(ctx_mem != NULL) { if(cryptoCreate(ctx_mem, (peer_slots + 1))) { if(dfragCreate(&mgt->dfrag, peermgt_MSGSIZE_MIN, peermgt_FRAGBUF_COUNT)) { if(authmgtCreate(&mgt->authmgt, &mgt->netid, auth_slots, local_nodekey, dhstate)) { if(nodedbCreate(&mgt->nodedb, ((peer_slots * 8) + 1))) { if(mapCreate(&mgt->map, (peer_slots + 1), nodeid_SIZE, 1)) { mgt->nodekey = local_nodekey; mgt->data = data_mem; mgt->ctx = ctx_mem; mgt->lastconnect = tnow; mgt->rrmsg.msg = mgt->rrmsgbuf; if(peermgtInit(mgt)) { return 1; } mgt->nodekey = NULL; mgt->data = NULL; mgt->ctx = NULL; mapDestroy(&mgt->map); } nodedbDestroy(&mgt->nodedb); } authmgtDestroy(&mgt->authmgt); } dfragDestroy(&mgt->dfrag); } cryptoDestroy(ctx_mem, (peer_slots + 1)); } free(ctx_mem); } free(data_mem); } } return 0; }
static char *map_test(){ int map_len = 8; // Initialization test Map *m = mapCreate(sizeof(int), map_len, (unsigned int (*)(void *))djb2, (int(*)(void *a, void *b))strcmp); mu_assert("m == NULL", m != NULL); mu_assert("m->len != 8", m->len == 8); // Insertion tests char *key = "sprite"; int val = 33; mapInsert(m, key, strlen(key) + 1, &val); unsigned int bucket_id = djb2((unsigned char *)key) % map_len; Bucket *b = m->bs[bucket_id]; mu_assert("bucket is empty", b->tail != b->head); mu_assert("bucket->last->data != val", *(int *)b->tail->data == val); // Retrieval tests int x = 12, get_status; get_status = mapGet(m, key, &x); mu_assert("x has the wrong value", x == val); mu_assert("get_status != 0", get_status == 0); // Removal tests int y = 44, pop_status; pop_status = mapPop(m, key, &y, free); mu_assert("c has the wrong value", y == val); mu_assert("pop_status != 0", pop_status == 0); mu_assert("bucket is not empty", b->tail == b->head); mapDestroy(m, free); return 0; }
//reates a copy of target map. //Iterator values for both maps is undefined after this operation. Map mapCopy(Map map) { if(map==NULL) return NULL; NodeResult status_node; Map new_map = mapCreate(map->copy_data, map->copy_key, map->free_data_map, map->free_key_map, map->compare_key); if (new_map == NULL) { return NULL; } MapKeyElement key_first = nodeReturnKey(map->first_pointer,&status_node); MapKeyElement copy_key_first=map->copy_key(key_first); if (status_node == NODE_NULL_ARGUMENT) { return new_map; // its mean that we dont have a nodes } MapDataElement data_first= nodeReturnData(map->first_pointer,&status_node); MapKeyElement copy_data_first=map->copy_data(data_first); //no need to check if status is NULL, already check for first pointer, Node node_first = createNode(copy_key_first, copy_data_first); Node previous_node = node_first; new_map->first_pointer=node_first; new_map->iterator=node_first; map->iterator=map->first_pointer; map->iterator = nodeGetNextIteration(map->iterator, &status_node); //increase the iterator for (; map->iterator != NULL; mapGetNext(map)){ MapKeyElement new_key = nodeReturnKey(map->iterator,&status_node); MapKeyElement copy_new_key=map->copy_key(new_key); MapDataElement new_data = nodeReturnData(map->iterator,&status_node); MapDataElement copy_new_data=map->copy_data(new_data); Node new_node = createNode(copy_new_key, copy_new_data); nodeUpdateNext(previous_node,new_node, &status_node); previous_node = new_node; } new_map->iterator = NULL; map->iterator = NULL; new_map->size_map =map->size_map; return new_map; }