page_node *add_page(stored_object *object, uint32_t page_id) { page_node *curr,*page; HASH_FIND_INT(global_page_table,&page_id,page); if(page) { printf("ERROR[add_page] Object %lu already contains page %d\n",page->object_id,page_id); return NULL; } object->size += PAGE_SIZE; if(object->pages == NULL) { page = allocate_new_page(object->id,page_id); HASH_ADD_INT(global_page_table, page_id, page); object->pages = page; return object->pages; } for(curr=page=object->pages; page && page->page_id != page_id; curr=page,page=page->next) ; if(page) { printf("ERROR[add_page] Object %lu already contains page %d\n",page->object_id,page_id); return NULL; } page = allocate_new_page(object->id,page_id); HASH_ADD_INT(global_page_table, page_id, page); curr->next = page; return curr->next; }
int main() { item *i, *j, *items=NULL; int k; /* first item */ k = 12345; i = (item*)malloc(sizeof(item)); i->key = k; i->data = 0; HASH_ADD_INT(items,key,i); /* second item */ k = 6789; i = (item*)malloc(sizeof(item)); i->key = k; i->data = 0; HASH_ADD_INT(items,key,i); /* third item */ k = 98765; i = (item*)malloc(sizeof(item)); i->key = k; i->data = 0; HASH_ADD_INT(items,key,i); /* look them all up */ k = 12345; HASH_FIND_INT(items, &k, j); if (j) printf("found %d\n",k); k = 6789; HASH_FIND_INT(items, &k, j); if (j) printf("found %d\n",k); k = 98765; HASH_FIND_INT(items, &k, j); if (j) printf("found %d\n",k); /* delete them not the way we prefer but it works */ for(j=items; j != NULL; j=(item*)j->hh.next) { printf("deleting %d\n", j->key); HASH_DEL(items,j); } return 0; }
int mkparticle(particle_kind_t kind, cpVect pos, cpVect impulse, double energy) { particle_t* p = malloc(sizeof *p); if(p == NULL) { return -1; // bad malloc } cpSpace* space = current_space(); cpBody* body = cpBodyNew(energy / 1000.0, particle_moi); cpShape* shape = cpCircleShapeNew(body, particle_r, cpvzero); cpBodySetUserData(body, p); cpShapeSetUserData(shape, p); cpSpaceAddBody(space, body); cpSpaceAddShape(space, shape); *p = (particle_t){ .id = id, .kind = kind, .energy = energy, .life = energy, .body = body }; HASH_ADD_INT(particles, id, p); return id++; }
void ActionManager::addAction(Action *pAction, Node *target, bool paused) { CCASSERT(pAction != NULL, ""); CCASSERT(target != NULL, ""); tHashElement *pElement = NULL; // we should convert it to Object*, because we save it as Object* Object *tmp = target; HASH_FIND_INT(_targets, &tmp, pElement); if (! pElement) { pElement = (tHashElement*)calloc(sizeof(*pElement), 1); pElement->paused = paused; target->retain(); pElement->target = target; HASH_ADD_INT(_targets, target, pElement); } actionAllocWithHashElement(pElement); CCASSERT(! ccArrayContainsObject(pElement->actions, pAction), ""); ccArrayAppendObject(pElement->actions, pAction); pAction->startWithTarget(target); }
/** * Return an array of prototypes labeled with cluster numbers * @param c cluster structure * @param a assignment of prototypes * @param p prototypes * @return rejected feature vectors */ farray_t *cluster_get_prototypes(cluster_t *c, assign_t *a, farray_t *p) { int i; farray_t *n = farray_create("prototypes"); count_t *hash = NULL, *entry; for (i = 0; i < a->len; i++) { /* Skip rejected clusters */ if (!c->cluster[i]) continue; /* Check if prototype has been added */ int j = a->proto[i]; HASH_FIND_INT(hash, &j, entry); if (entry) continue; /* Add new prototype */ entry = malloc(sizeof(count_t)); entry->label = j; HASH_ADD_INT(hash, label, entry); /* Add prototype */ farray_add(n, fvec_clone(p->x[j]), cluster_get_name(c, i)); } /* Delete hash table */ while (hash) { entry = hash; HASH_DEL(hash, entry); free(entry); } return n; }
static GLboolean AllocDispatchIndex(__GLXvendorInfo *vendor, const GLubyte *procName) { __GLXdispatchIndexHash *pEntry = malloc(sizeof(*pEntry)); if (!pEntry) { return GL_FALSE; } pEntry->procName = (GLubyte *)strdup((const char *)procName); if (!pEntry->procName) { free(pEntry); return GL_FALSE; } LKDHASH_WRLOCK(__glXPthreadFuncs, __glXDispatchIndexHash); pEntry->index = __glXNextUnusedHashIndex++; // Notify the vendor this is the index which should be used vendor->staticDispatch-> glxvc.setDispatchIndex(procName, pEntry->index); HASH_ADD_INT(_LH(__glXDispatchIndexHash), index, pEntry); LKDHASH_UNLOCK(__glXPthreadFuncs, __glXDispatchIndexHash); return GL_TRUE; }
bool consolidate_gpos_single(otfcc_Font *font, table_OTL *table, otl_Subtable *_subtable, const otfcc_Options *options) { subtable_gpos_single *subtable = &(_subtable->gpos_single); gpos_single_hash *h = NULL; for (glyphid_t k = 0; k < subtable->length; k++) { if (!GlyphOrder.consolidateHandle(font->glyph_order, &subtable->items[k].target)) { logWarning("[Consolidate] Ignored missing glyph /%s.\n", subtable->items[k].target.name); continue; } gpos_single_hash *s; int fromid = subtable->items[k].target.index; HASH_FIND_INT(h, &fromid, s); if (s) { logWarning("[Consolidate] Detected glyph double-mapping about /%s.\n", subtable->items[k].target.name); } else { NEW(s); s->fromid = subtable->items[k].target.index; s->fromname = sdsdup(subtable->items[k].target.name); s->v = subtable->items[k].value; HASH_ADD_INT(h, fromid, s); } } HASH_SORT(h, gpos_by_from_id); iSubtable_gpos_single.clear(subtable); gpos_single_hash *s, *tmp; HASH_ITER(hh, h, s, tmp) { iSubtable_gpos_single.push(subtable, ((otl_GposSingleEntry){ .target = Handle.fromConsolidated(s->fromid, s->fromname), .value = s->v, }));
int h3m_add_oa_by_def(h3mlib_ctx_t ctx, const char *def, int *oa_index) { struct H3M_OA_ENTRY *oa_entry = NULL; struct META_OA_HASH_ENTRY *oa_hash_entry = NULL; const struct H3M_OA_BODY *body = NULL; int oa_body_index = 0; if (NULL == (body = h3m_get_def_body(def, &oa_body_index))) { return 1; } *oa_index = ctx->h3m.oa.count++; ctx->h3m.oa.entries = realloc(ctx->h3m.oa.entries, sizeof(struct H3M_OA_ENTRY) * ctx->h3m.oa.count); oa_entry = &ctx->h3m.oa.entries[ctx->h3m.oa.count - 1]; oa_entry->header.def_size = strlen(def); oa_entry->header.def = (uint8_t *)strdup(def); memcpy(&oa_entry->body, body, sizeof(struct H3M_OA_BODY)); oa_hash_entry = calloc(1, sizeof(*oa_hash_entry)); oa_hash_entry->def = strdup(def); oa_hash_entry->oa_body_index = oa_body_index; oa_hash_entry->oa_index = *oa_index; HASH_ADD_INT(ctx->meta.oa_hash, oa_body_index, oa_hash_entry); return 0; }
caryll_buffer *caryll_write_gsub_ligature_subtable(otl_subtable *_subtable) { caryll_buffer *buf = bufnew(); subtable_gsub_ligature *subtable = &(_subtable->gsub_ligature); ligature_aggerator *h = NULL, *s, *tmp; uint16_t nLigatures = subtable->to->numGlyphs; for (uint16_t j = 0; j < nLigatures; j++) { int sgid = subtable->from[j]->glyphs[0].gid; HASH_FIND_INT(h, &sgid, s); if (!s) { NEW(s); s->gid = sgid; s->ligid = HASH_COUNT(h); HASH_ADD_INT(h, gid, s); } } HASH_SORT(h, by_gid); otl_coverage *startCoverage; NEW(startCoverage); startCoverage->numGlyphs = HASH_COUNT(h); NEW_N(startCoverage->glyphs, startCoverage->numGlyphs); uint16_t jj = 0; foreach_hash(s, h) { s->ligid = jj; startCoverage->glyphs[jj].gid = s->gid; startCoverage->glyphs[jj].name = NULL; jj++; }
int main(int argc,char *argv[]) { int i; example_user_t *user, *users=NULL, *altusers=NULL; /* create elements */ for(i=0;i<1000;i++) { if ( (user = (example_user_t*)malloc(sizeof(example_user_t))) == NULL) exit(-1); user->id = i; user->cookie = i*i; if (i<10) HASH_ADD_INT(users,id,user); HASH_ADD(alth,altusers,id,sizeof(int),user); } printf("sorting users ascending\n"); HASH_SRT(hh,users,ascending_sort); for(user=users; user; user=(example_user_t*)user->hh.next) printf("user %d\n", user->id); printf("sorting altusers descending\n"); HASH_SRT(alth,altusers,descending_sort); for(user=altusers; user; user=(example_user_t*)user->alth.next) printf("altuser %d\n", user->id); /* HASH_FSCK(hh,users); */ /* HASH_FSCK(alth,altusers); */ return 0; }
client_t * init_new_client(agent_t *agent, uuid_t * uuid) { client_t *new_client; new_client = calloc(sizeof(client_t),1); if(new_client == NULL) { printf("Malloc failed!\n"); exit(1); } new_client->buffered_packet_table = NULL; new_client->allowed_connections = 0; new_client->agent_sock = calloc(sizeof(int) , agent->options.num_parallel_connections); new_client->agent_side_event_info = calloc(sizeof(struct event_info_struct) ,agent->options.num_parallel_connections); new_client->send_seq = 0; new_client->recv_seq = 0; new_client->agent_fd_poll = calloc(sizeof(char) , agent->options.num_parallel_connections); new_client->num_parallel_connections = 0; new_client->client_hash.client = new_client; gettimeofday(&new_client->client_hash.accept_start, NULL); if(uuid == NULL) { uuid_generate(new_client->client_hash.id); } else { memcpy(new_client->client_hash.id, *uuid, sizeof(uuid_t)); } HASH_ADD_INT(agent->clients_hashes, id, (&new_client->client_hash)); return new_client; }
static int adtn_socket_base(const char *data_path) { static int UID = 1; int sock_id = -1; int len; bunsock_s *identifier; ++UID; HASH_FIND_INT( sockStore, &sock_id, identifier); if (identifier == NULL) { sock_id = UID; identifier = (bunsock_s *)calloc(1, sizeof(bunsock_s)); identifier->id = sock_id; identifier->sopt.proc_flags = H_DESS | H_NOTF; identifier->sopt.block_flags = B_DEL_NP; identifier->sopt.lifetime = 100000; //this time is in seconds len = strlen(data_path) + 1; identifier->data_path = (char *)calloc(len, sizeof(char)); strncpy(identifier->data_path, data_path, len); identifier->bdata = NULL; HASH_ADD_INT( sockStore, id, identifier); } else { errno = EBUSY; } return sock_id; }
stored_object *create_object(size_t size) { stored_object *obj = malloc(sizeof(stored_object)); uint32_t page_id; // initialize to stored_object struct with size and initial pages obj->id = current_id++; obj->size = 0; obj->pages = NULL; // add the new object to the objects' hashtable HASH_ADD_INT(objects_table, id, obj); while(size > obj->size) { if (GET_NEW_PAGE(VICTIM_OVERALL, EMPTY_TABLE_ENTRY_NB, &page_id) == FAIL) { // cleanup just in case we managed to do anything up until now remove_object(obj); return NULL; } if(!add_page(obj, page_id)) return NULL; // mark new page as valid and used UPDATE_NEW_PAGE_MAPPING_NO_LOGICAL(page_id); } return obj; }
int _FTL_OBJ_COPYBACK(int32_t source, int32_t destination) { page_node *source_p; source_p = lookup_page(source); // source_p can be NULL if the GC is working on some old pages that belonged to an object we deleted already if (source_p != NULL) { // invalidate the source page UPDATE_INVERSE_BLOCK_VALIDITY(CALC_FLASH(source), CALC_BLOCK(source), CALC_PAGE(source), INVALID); // mark new page as valid and used UPDATE_NEW_PAGE_MAPPING_NO_LOGICAL(destination); // change the object's page mapping to the new page HASH_DEL(global_page_table, source_p); source_p->page_id = destination; HASH_ADD_INT(global_page_table, page_id, source_p); } #ifdef FTL_DEBUG else { printf("Warning[%s] %u copyback page not mapped to an object \n", __FUNCTION__, source); } #endif return SUCCESS; }
void lru_cache_insert(lru_cache_t* lru, DIR_handle_t handle, struct giga_directory* entry) { ACQUIRE_MUTEX(&(lru->mutex_), "lru_cache_insert(%d)", handle); // printf("INSERT %d %d, length: %ld, cap: %ld\n", handle, entry->split_flag, lru->length_+1, lru->capacity_); struct giga_directory* old; HASH_FIND_INT(lru->table, &handle, old); if (old != NULL) { HASH_DEL(lru->table, old); double_list_remove(old); lru_cache_unref(old); } else { ++lru->length_; } HASH_ADD_INT(lru->table, handle, entry); double_list_append(&(lru->dummy), entry); entry->refcount = 2; while (lru->length_ > lru->capacity_ && lru->dummy.next != &(lru->dummy)) { struct giga_directory* old = lru->dummy.next; // printf("EVICT ENTRY %d\n", old->handle); HASH_DEL(lru->table, old); double_list_remove(old); lru_cache_unref(old); --lru->length_; } RELEASE_MUTEX(&(lru->mutex_), "lru_cache_insert(%d)", handle); }
hash_token_t *uq_tokenize_to_hash(const char *str, const char *delimiters) { unsigned int hash; hash_token_t *s, *table = NULL; char *tok; char tmp[strlen(str) + 1]; strcpy(tmp, str); tok = strtok(tmp, delimiters); while(tok != NULL) { hash = str_hash(tok); HASH_FIND_INT(table, &hash, s); if(s == NULL) { s = malloc(sizeof(hash_token_t)); s->key = str_hash(tok); s->value = strdup(tok); HASH_ADD_INT(table, key, s); } tok = strtok(NULL, delimiters); } return (table); }
struct file_descriptor_table * allocate_file_descriptor_table(int pid) { struct file_descriptor_table * result_table; HASH_FIND_INT(file_descriptor_tables, &pid, result_table); if (result_table == NULL) { int i; struct file_descriptor_table * table = (struct file_descriptor_table *) malloc(sizeof(struct file_descriptor_table)); table->pid = pid; table->entries = (struct file_descriptor_entry *) malloc(8 * sizeof(struct file_descriptor_entry)); table->total_descriptors = 8; table->used_descriptors = 3; table->entries[0].fd = 0; table->entries[1].fd = 1; table->entries[2].fd = 2; for (i = 3; i < table->total_descriptors; i++) { table->entries[i].fd = FD_NOT_USED; } HASH_ADD_INT(file_descriptor_tables, pid, table); return table; } return NULL; }
void add_record(char *word, char *filename) { struct Record *r; int record_id = djb2((unsigned char *) word); HASH_FIND_INT(records, &record_id, r); // linear probing to resolve collisions while (r != NULL) { // found word if (strcmp(r->word, word) == 0) { break; } record_id++; HASH_FIND_INT(records, &record_id, r); } if (r == NULL) { // found empty spot, proceed to add r = (struct Record *)malloc(sizeof(struct Record)); r->id = record_id; r->word = word; r->filenames = SLCreate(compareStrings, destroyStrings); SLInsert(r->filenames, filename); HASH_ADD_INT(records, id, r); } else { SLInsert(r->filenames, filename); } }
int main() { int i; example_user_t *user, *users=NULL, *ausers=NULL, *tmp; /* create elements */ for(i=0;i<10;i++) { user = (example_user_t*)m_malloc(sizeof(example_user_t)); user->id = i; HASH_ADD_INT(users,id,user); } for(user=users; user; user=(example_user_t*)(user->hh.next)) { printf("user %d\n", user->id); } /* now select some users into ausers */ HASH_SELECT(ah,ausers,hh,users,evens); HASH_SRT(ah,ausers,idcmp); for(user=ausers; user; user=(example_user_t*)(user->ah.next)) { printf("auser %d\n", user->id); } /* free memory */ HASH_ITER(ah, ausers, user, tmp) { HASH_DELETE(ah, ausers, user); /* delete; users advances to next */ }
void CCBMFontConfiguration::parseKerningEntry(std::string line) { ////////////////////////////////////////////////////////////////////////// // line to parse: // kerning first=121 second=44 amount=-7 ////////////////////////////////////////////////////////////////////////// // first int first; int index = line.find("first="); int index2 = line.find(' ', index); std::string value = line.substr(index, index2-index); sscanf(value.c_str(), "first=%d", &first); // second int second; index = line.find("second="); index2 = line.find(' ', index); value = line.substr(index, index2-index); sscanf(value.c_str(), "second=%d", &second); // amount int amount; index = line.find("amount="); index2 = line.find(' ', index); value = line.substr(index, index2-index); sscanf(value.c_str(), "amount=%d", &amount); tKerningHashElement *element = (tKerningHashElement *)calloc( sizeof( *element ), 1 ); element->amount = amount; element->key = (first<<16) | (second&0xffff); HASH_ADD_INT(m_pKerningDictionary,key, element); }
void mgr_on_accept(int fd, event_manager* ev_mgr) { if (internal_threads(ev_mgr->excluded_threads, pthread_self())) return; uint32_t leader_id = get_leader_id(ev_mgr->con_node); if (ev_mgr->node_id == leader_id) { leader_tcp_pair* new_conn = malloc(sizeof(leader_tcp_pair)); memset(new_conn,0,sizeof(leader_tcp_pair)); new_conn->key = fd; HASH_ADD_INT(ev_mgr->leader_tcp_map, key, new_conn); rsm_op(ev_mgr->con_node, 0, NULL, P_TCP_CONNECT, &new_conn->vs); } else { request_record* retrieve_data = NULL; size_t data_size; while (retrieve_data == NULL){ retrieve_record(ev_mgr->db_ptr, sizeof(db_key_type), &ev_mgr->cur_rec, &data_size, (void**)&retrieve_data); } replica_tcp_pair* ret = NULL; HASH_FIND(hh, ev_mgr->replica_tcp_map, &retrieve_data->clt_id, sizeof(view_stamp), ret); ret->s_p = fd; ret->accepted = 1; } return; }
int main(int argc,char *argv[]) { int i; example_user_t *user, *users=NULL, *ausers=NULL; /* create elements */ for(i=0;i<10;i++) { user = (example_user_t*)malloc(sizeof(example_user_t)); user->id = i; HASH_ADD_INT(users,id,user); } for(user=users; user!=NULL; user=(example_user_t*)(user->hh.next)) { printf("user %d\n", user->id); } printf("users count: %u\n", HASH_CNT(hh,users)); /* now select some users into ausers */ HASH_SELECT(ah,ausers,hh,users,EVENS); HASH_SRT(ah,ausers,idcmp); for(user=ausers; user!=NULL; user=(example_user_t*)(user->ah.next)) { printf("auser %d\n", user->id); } printf("ausers count: %u\n", HASH_CNT(ah,ausers)); HASH_CLEAR(ah,ausers); printf("cleared ausers.\n"); printf("ausers count: %u\n", HASH_CNT(ah,ausers)); for(user=ausers; user!=NULL; user=(example_user_t*)(user->ah.next)) { printf("auser %d\n", user->id); } printf("users count: %u\n", HASH_CNT(hh,users)); return 0; }
/* Obtain and set a PID for the new process. * O(1) in average case, O(N) degenerate case (more than PID_MAX procs) * Uses hardware FFS instruction, so runs in very quick N/32 time */ static int _set_pid(process_create_data *data) { // Try to increment _last_pid. int err; sos_pcb *pcb = data->pcb; int cur_pid = _last_pid + 1; sos_pcb *taken = NULL; if (!pids) { dprintf(6, "Allocating PID set...\n"); bitset_alloc(&pids, SOS_PID_MAX - SOS_PID_MIN); } dprintf(1, "bs size: %d\n", pids->size); if (bitset_test(pids, cur_pid)) { dprintf(6, "\tPID %d is already taken.\n", cur_pid); // cur_pid is taken cur_pid = bitset_ffz(pids); dprintf(6, "\tfirst free PID: %d\n", cur_pid); if (cur_pid < 0 || cur_pid > pids->size) { return SOS_PROCESS_MAXPROC; } } // cur_pid is known to be free bitset_set(pids, cur_pid); pcb->pid = cur_pid + SOS_PID_MIN; _last_pid = cur_pid; HASH_ADD_INT(_process_table, pid, pcb); return SOS_PROCESS_SUCCESS; }
int main(int argc,char *argv[]) { int i; example_user_t *user, *tmp, *users=NULL, *altusers=NULL; /* create elements */ for(i=0;i<1000;i++) { if ( (user = (example_user_t*)malloc(sizeof(example_user_t))) == NULL) exit(-1); user->id = i; user->cookie = i*i; if (i<10) HASH_ADD_INT(users,id,user); HASH_ADD(alth,altusers,id,sizeof(int),user); } /* printf("hh items: %d, alth items: %d\n", users->hh.tbl->num_items, users->alth.tbl->num_items); printf("hh buckets: %d, alth buckets: %d\n", users->hh.tbl->num_buckets, users->alth.tbl->num_buckets); */ i=9; HASH_FIND_INT(users,&i,tmp); printf("%d %s in hh\n", i, (tmp ? "found" : "not found")); HASH_FIND(alth,altusers,&i,sizeof(int),tmp); printf("%d %s in alth\n", i, (tmp ? "found" : "not found")); i=10; HASH_FIND_INT(users,&i,tmp); printf("%d %s in hh\n", i, (tmp ? "found" : "not found")); HASH_FIND(alth,altusers,&i,sizeof(int),tmp); printf("%d %s in alth\n", i, (tmp ? "found" : "not found")); return 0; }
int main(int argc,char *argv[]) { int i; example_user_t *user, *users=NULL, *ausers=NULL; /* create elements */ for(i=0;i<10;i++) { user = (example_user_t*)malloc(sizeof(example_user_t)); if (user == NULL) exit(-1); user->id = i; HASH_ADD_INT(users,id,user); } for(user=users; user!=NULL; user=(example_user_t*)(user->hh.next)) { printf("user %d\n", user->id); } /* now select some users into ausers */ HASH_SELECT(ah,ausers,hh,users,evens); HASH_SRT(ah,ausers,idcmp); for(user=ausers; user!=NULL; user=(example_user_t*)(user->ah.next)) { printf("auser %d\n", user->id); } return 0; }
void CCActionManager::addAction(CCAction *pAction, CAView *pTarget, bool paused) { CCAssert(pAction != NULL, ""); CCAssert(pTarget != NULL, ""); tHashElement *pElement = NULL; // we should convert it to CAObject*, because we save it as CAObject* CAObject *tmp = pTarget; HASH_FIND_INT(m_pTargets, &tmp, pElement); if (! pElement) { pElement = (tHashElement*)calloc(sizeof(*pElement), 1); pElement->paused = paused; pTarget->retain(); pElement->target = pTarget; HASH_ADD_INT(m_pTargets, target, pElement); } actionAllocWithHashElement(pElement); CCAssert(! ccArrayContainsObject(pElement->actions, pAction), ""); ccArrayAppendObject(pElement->actions, pAction); pAction->startWithTarget(pTarget); }
int main(int argc,char *argv[]) { int i; example_user_t *user, *tmp, *users=NULL; /* create elements */ for(i=0;i<10;i++) { if ( (user = malloc(sizeof(example_user_t))) == NULL) exit(-1); user->id = i; user->cookie = i*i; HASH_ADD_INT(users,id,user); } /* delete each even ID */ for(i=0;i<10;i+=2) { HASH_FIND_INT(users,&i,tmp); if (tmp) { HASH_DEL(users,tmp); free(tmp); } else printf("user id %d not found\n", i); } i=9; HASH_FIND_INT(users,&i,tmp); if (tmp) { while(tmp) { printf("id %d, following prev...\n", tmp->id); tmp = tmp->hh.prev; } } else printf("user id %d not found\n", i); }
int main(int argc,char *argv[]) { int i; example_user_t *user, *users=NULL; /* create elements */ for(i=0; i<10; i++) { user = (example_user_t*)malloc(sizeof(example_user_t)); if (user == NULL) { exit(-1); } user->id = i; user->cookie = i*i; HASH_ADD_INT(users,id,user); } for(user=users; user != NULL; user=(example_user_t*)user->hh.next) { printf("user %d, cookie %d\n", user->id, user->cookie); } /* delete them all, pathologically */ while(users != NULL) { printf("deleting id %i\n", users->id); HASH_DEL(users,users); /* single head/deletee var! */ } return 0; }
int main(int argc,char *argv[]) { int i; example_user_t *user, *tmp, *users=NULL; /* create elements */ for(i=0;i<10;i++) { if ( (user = (example_user_t*)malloc(sizeof(example_user_t))) == NULL) exit(-1); user->id = i; user->cookie = i*i; HASH_ADD_INT(users,id,user); } /* delete each even ID */ for(i=0;i<10;i+=2) { HASH_FIND_INT(users,&i,tmp); if (tmp) { HASH_DEL(users,tmp); free(tmp); } else printf("user id %d not found\n", i); } /* show the hash */ for(user=users; user != NULL; user=(example_user_t*)(user->hh.next)) { printf("user %d, cookie %d\n", user->id, user->cookie); } return 0; }
void js_register_jsb_iOSiapWrapper_iOSiapWrapper(JSContext *cx, JSObject *global) { jsb_iOSiapWrapper_class = (JSClass *)calloc(1, sizeof(JSClass)); jsb_iOSiapWrapper_class->name = "iOSiapWrapper"; jsb_iOSiapWrapper_class->addProperty = JS_PropertyStub; jsb_iOSiapWrapper_class->delProperty = JS_PropertyStub; jsb_iOSiapWrapper_class->getProperty = JS_PropertyStub; jsb_iOSiapWrapper_class->setProperty = JS_StrictPropertyStub; jsb_iOSiapWrapper_class->enumerate = JS_EnumerateStub; jsb_iOSiapWrapper_class->resolve = JS_ResolveStub; jsb_iOSiapWrapper_class->convert = JS_ConvertStub; jsb_iOSiapWrapper_class->finalize = js_jsb_iOSiapWrapper_iOSiapWrapper_finalize; jsb_iOSiapWrapper_class->flags = JSCLASS_HAS_RESERVED_SLOTS(2); static JSPropertySpec properties[] = { {0, 0, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER} }; static JSFunctionSpec funcs[] = { JS_FN("payForProduct", js_jsb_iOSiapWrapper_iOSiapWrapper_payForProduct, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("getPurchaseCallbackTarget", js_jsb_iOSiapWrapper_iOSiapWrapper_getPurchaseCallbackTarget, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("setPurchaseCallbackTarget", js_jsb_iOSiapWrapper_iOSiapWrapper_setPurchaseCallbackTarget, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("ctor", js_jsb_iOSiapWrapper_iOSiapWrapper_ctor, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FS_END }; static JSFunctionSpec st_funcs[] = { JS_FN("getInstance", js_jsb_iOSiapWrapper_iOSiapWrapper_getInstance, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FS_END }; jsb_iOSiapWrapper_prototype = JS_InitClass( cx, global, NULL, // parent proto jsb_iOSiapWrapper_class, js_jsb_iOSiapWrapper_iOSiapWrapper_constructor, 0, // constructor properties, funcs, NULL, // no static properties st_funcs); // make the class enumerable in the registered namespace JSBool found; JS_SetPropertyAttributes(cx, global, "iOSiapWrapper", JSPROP_ENUMERATE | JSPROP_READONLY, &found); // add the proto and JSClass to the type->js info hash table TypeTest<iOSiapWrapper> t; js_type_class_t *p; uint32_t typeId = t.s_id(); HASH_FIND_INT(_js_global_type_ht, &typeId, p); if (!p) { p = (js_type_class_t *)malloc(sizeof(js_type_class_t)); p->type = typeId; p->jsclass = jsb_iOSiapWrapper_class; p->proto = jsb_iOSiapWrapper_prototype; p->parentProto = NULL; HASH_ADD_INT(_js_global_type_ht, type, p); } iOSiapWrapper::initAll(); }