static int setup_program(SceneData* sceneData, SceneObject* so, SoData* soData, GLuint* programHandle) { GLuint vShaderHandle; GLuint fShaderHandle; if ( -1 == setup_shaders( sceneData, so, soData, &vShaderHandle, &fShaderHandle ) ) return -1; char progKey[KEYSIZE]; snprintf (progKey, KEYSIZE, "%p,%p", &so->vShader, &so->fShader); GLuint program = (GLuint)hashmap_find(progKey, sceneData->mapShaderShader2Handle); size_t programUsers; if (program) { *programHandle = program; programUsers = (size_t)hashmap_find ( progKey, sceneData->countProgramUsers ); hashmap_insert ( progKey, (void*)(programUsers+1), sceneData->countProgramUsers ); } else { program = create_program( so->vShader, so->fShader, vShaderHandle, fShaderHandle ); if ( -1 == program ) return -1; list_add ( (void*)program, sceneData->listPrograms ); hashmap_insert ( progKey, (void*)program, sceneData->mapShaderShader2Handle ); hashmap_insert ( progKey, (void*)1, sceneData->countProgramUsers ); *programHandle = program; } return 0; }
static void random_insertions( void ) { int res; hashmap_t hashmap; unsigned int i; res = hashmap_init_with_buckets( &hashmap, 8192 /* pow 2 */ ); assert( 0 == res ); for( i = 0; i < 100000; ++i ) { char str[10]; hashmap_value_t val = rand(); hashmap_value_t retrieved; random_string( str, sizeof(str) ); res = hashmap_insert( &hashmap, str, val ); assert( 0 == res ); res = hashmap_find( &hashmap, str, &retrieved ); assert( 1 == res ); assert( retrieved == val ); } hashmap_term( &hashmap ); }
HashItem *construct_preset(char const *setkey, char *rest) { char *key; HashItem *mapItem; if (!(key = string_firstword(&rest))) /* get the key */ { /* e.g., title */ message_error("`preset ...: missing key"); return 0; } if (strcmp(key, "nohtmlfive") == 0) global.d_html5 = 0; else { if (!*string_strip(&rest)) /* get the value */ warning("Empty value of symbol `%s'", key); /* e.g., This is... */ if (strcmp(key, "styleopt") == 0) /* store styleopts */ lines_add(&global.d_styleopt, rest); else { /* look up the key */ mapItem = hashmap_find(&global.d_symbol, key, ANY); if (mapItem != PFAILED) /* reassign */ hashitem_set(mapItem, rest, free); /* existing value */ else /* or insert new */ hashmap_insert(&global.d_symbol, /* element */ hashitem_construct(VOIDPTR, key, new_str(rest), free)); } } free(key); return hashitem_construct(VOIDPTR, "", 0, root_nop); }
static int tear_down_textures (SceneData* sceneData, SceneObject* so, SoData* soData) { Uniform* uniform; char uniKey[KEYSIZE]; GLuint textureHandle; size_t textureUsers; int i; for ( i = 0; i < list_size ( soData->textures ); i++) { uniform = list_get( i, soData->textures); snprintf ( uniKey, KEYSIZE, "%p", uniform ); textureHandle = (GLuint)hashmap_find ( uniKey, sceneData->mapTexture2Handle ); textureUsers = (size_t)hashmap_find ( uniKey, sceneData->countTextureUsers ); textureUsers--; if ( 0 == textureUsers ) { glDeleteTextures( 1, &textureHandle ); hashmap_delete ( uniKey, sceneData->mapTexture2Handle ); hashmap_delete ( uniKey, sceneData->countTextureUsers ); list_remove_elem ( (void*)textureHandle, sceneData->listTextures ); } else { hashmap_insert ( uniKey, (void*)textureUsers, sceneData->countTextureUsers ); } } return 0; }
static int tear_down_program(SceneData* sceneData, SceneObject* so, SoData* soData) { tear_down_shaders ( sceneData, so, soData ); char progKey[KEYSIZE]; snprintf (progKey, KEYSIZE, "%p,%p", &so->vShader, &so->fShader); GLuint program = (GLuint)hashmap_find(progKey, sceneData->mapShaderShader2Handle); size_t programUsers = (size_t)hashmap_find ( progKey, sceneData->countProgramUsers ); programUsers--; if ( 0 == programUsers ) { glDeleteProgram ( program ); hashmap_delete ( progKey, sceneData->mapShaderShader2Handle ); hashmap_delete ( progKey, sceneData->countProgramUsers ); list_remove_elem ( (void*)program, sceneData->listPrograms ); } else { hashmap_insert ( progKey, (void*)programUsers, sceneData->countProgramUsers ); } return 0; }
void hash_test_func(hashmap_t *hmp) { char *val = NULL; char key[1024] = {0}; char buf[1024] = {0}; int j=0; int i=0; for(; i< 9; i++) { j = 0xffffff - i; sprintf(key, "key_%x", j); sprintf(buf, "val_%x", j); val = strdup(buf); hashmap_insert(hmp, key, strlen(key), val); } for(i=0; i< 9; i++) { j = 0xffffff - i; sprintf(buf, "key_%x", j); printf("find key:%s, val:%s\n", buf, hashmap_find(hmp, buf, strlen(buf))); } }
DECLARE_TEST( hashmap, erase ) { hashmap_t* map = hashmap_allocate( 0, 0 ); void* prev = 0; EXPECT_EQ( hashmap_lookup( map, 0 ), 0 ); EXPECT_EQ( hashmap_size( map ), 0 ); prev = hashmap_insert( map, 0, map ); EXPECT_EQ( prev, 0 ); EXPECT_EQ( hashmap_size( map ), 1 ); EXPECT_TRUE( hashmap_has_key( map, 0 ) ); prev = hashmap_erase( map, 0 ); EXPECT_EQ( prev, map ); EXPECT_EQ( hashmap_size( map ), 0 ); EXPECT_FALSE( hashmap_has_key( map, 0 ) ); prev = hashmap_erase( map, 0 ); EXPECT_EQ( prev, 0 ); EXPECT_EQ( hashmap_size( map ), 0 ); EXPECT_FALSE( hashmap_has_key( map, 0 ) ); prev = hashmap_erase( map, (hash_t)(uintptr_t)map ); EXPECT_EQ( prev, 0 ); EXPECT_EQ( hashmap_size( map ), 0 ); EXPECT_FALSE( hashmap_has_key( map, (hash_t)(uintptr_t)map ) ); hashmap_deallocate( map ); return 0; }
DECLARE_TEST( hashmap, lookup ) { hashmap_t* map = hashmap_allocate( 31, 0 ); char* value = (void*)(uintptr_t)1234; hash_t key = (hash_t)4321; unsigned int ikey = 0; for( ; ikey < 1024; ++ikey, ++key, ++value ) { void* prev = hashmap_insert( map, key, value ); EXPECT_EQ( prev, 0 ); } for( ikey = 0, key = (hash_t)4321, value = (void*)(uintptr_t)1234; ikey < 1024; ++ikey, ++key, ++value ) { void* prev = hashmap_lookup( map, key ); EXPECT_EQ( prev, value ); EXPECT_TRUE( hashmap_has_key( map, key ) ); hashmap_erase( map, key ); EXPECT_FALSE( hashmap_has_key( map, key ) ); } hashmap_deallocate( map ); return 0; }
static int tear_down_index_buffer (SceneData* sceneData, SceneObject* so, SoData* soData) { Draw* method = so->draw; char bufferKey[KEYSIZE]; Attribute* indices = method->indices; GLuint bufferHandle; size_t bufferUsers; if ( indices && 1 == indices->useBuffer ) { snprintf (bufferKey, KEYSIZE, "%p", indices); bufferUsers = (size_t)hashmap_find ( bufferKey, sceneData->countBufferUsers ); bufferUsers--; if ( 0 == bufferUsers ) { bufferHandle = (GLuint)hashmap_find ( bufferKey, sceneData->mapBuffer2Handle ); glDeleteBuffers ( 1, &bufferHandle ); hashmap_delete ( bufferKey, sceneData->mapBuffer2Handle ); hashmap_delete ( bufferKey, sceneData->countBufferUsers ); list_remove_elem ( (void*)bufferHandle, sceneData->listBuffers ); } else { hashmap_insert ( bufferKey, (void*)bufferUsers, sceneData->countBufferUsers ); } } return 0; }
HashItem *construct_label(char const *key, char *rest) { HashItem *item; LabelInfo *li = new_memory(1, sizeof(LabelInfo)); li->d_section = lines_size(&global.d_section) - 1; li->d_filenr = global.d_filecount; if (!*rest) { message_error("Missing label name"); return 0; } item = hashitem_construct(VOIDPTR, rest, li, free); if (hashmap_insert(&symtab, item) == FAILED) { message_show(MSG_ERR); message("%s: %s doubly defined", args_programName(), key); return 0; } return hashitem_construct(VOIDPTR, rest, 0, root_nop); }
int global_add_plug(void * plug){ int id = -1; global_init(); id = global_number_next(&self.plug_serial); if(hashmap_insert(self.plugs, id, plug) < 0){ return -1; } return id; }
int global_add_device(void * device_){ int id = -1; global_init(); id = global_number_next(&self.device_serial); if(hashmap_insert(self.devices, id, device_) < 0){ return -1; } return id; }
void *cache_add_glyph(Hashmap *glyph_cache, GlyphHashKey *key, GlyphHashValue *val) { if (val->glyph && val->glyph->format == FT_GLYPH_FORMAT_BITMAP) { FT_Bitmap *bitmap = &((FT_BitmapGlyph) val->glyph)->bitmap; glyph_cache->cache_size += bitmap->rows * bitmap->pitch; } return hashmap_insert(glyph_cache, key, val); }
static int tear_down_shaders (SceneData* sceneData, SceneObject* so, SoData* soData) { char shaderKey[KEYSIZE]; snprintf ( shaderKey, KEYSIZE, "%p", &so->vShader ); GLuint handle = (GLuint)hashmap_find ( shaderKey, sceneData->mapVShader2Handle ); size_t shaderUsers = (size_t)hashmap_find ( shaderKey, sceneData->countVShaderUsers ); shaderUsers--; if ( 0 == shaderUsers ) { glDeleteShader ( handle ); list_remove_elem ( (void*)handle, sceneData->listVShaders ); hashmap_delete ( shaderKey, sceneData->mapVShader2Handle ); hashmap_delete ( shaderKey, sceneData->countVShaderUsers ); } else { hashmap_insert ( shaderKey, (void*)shaderUsers, sceneData->countVShaderUsers ); } snprintf ( shaderKey, KEYSIZE, "%p", &so->fShader ); handle = (GLuint)hashmap_find ( shaderKey, sceneData->mapFShader2Handle ); shaderUsers = (GLuint)hashmap_find ( shaderKey, sceneData->countFShaderUsers ); shaderUsers--; if ( 0 == shaderUsers ) { glDeleteShader ( handle ); list_remove_elem ( (void*)handle, sceneData->listFShaders ); hashmap_delete ( shaderKey, sceneData->mapFShader2Handle ); hashmap_delete ( shaderKey, sceneData->countFShaderUsers ); } else { hashmap_insert ( shaderKey, (void*)shaderUsers, sceneData->countFShaderUsers ); } return 0; }
void *cache_add_bitmap(Hashmap *bitmap_cache, BitmapHashKey *key, BitmapHashValue *val) { // Note: this is only an approximation if (val->bm_o) bitmap_cache->cache_size += val->bm_o->w * val->bm_o->h * 3; else if (val->bm) bitmap_cache->cache_size += val->bm->w * val->bm->h * 3; return hashmap_insert(bitmap_cache, key, val); }
/* This needs to: * -get a block from the in-memory blocks if possible * -otherwise pull block from disk * -increment blocks' num_open counter * -we do NOT need to save the num_open update to disk, as it is reset after a * crash anyway. * * Clients must ALWAYS check the return ID, in case a file was deleted. */ block_t _retrieve_block(int block_id) { block_t block; minifile_t inode; int ret; // todo - handle semaphores for inodes, destroy the in inode delete // First check if it's already in memory if (block_id < 0) return NULL; block = hashmap_get(retrieved_blocks, block_id); if (block != NULL && block->is_deleted == 0) { if ( block->block_type == BLOCK_TYPE_FILE || block->block_type == BLOCK_TYPE_DIRECTORY) { block->num_open++; inode = (minifile_t) block; inode->u.data.mutex = semaphore_create(); semaphore_initialize(inode->u.data.mutex, 1); } return block; } // Otherwise, get it from disk block = (block_t) malloc(sizeof(struct block)); ret = _perform_full_disk_read(disk, block_id, (char*) block); // Setup the semaphore if it's an inode type // ONly check deleted for file/dir, as data blocks can have anything if (ret == 0 && !((block->block_type == BLOCK_TYPE_FILE || block->block_type == BLOCK_TYPE_DIRECTORY) && block->is_deleted == 1)) { // Ensure num_open is set to just one, as we got it from disk (may have crashed with old data) hashmap_insert(retrieved_blocks, block_id, (void*) block); if ( block->block_type == BLOCK_TYPE_FILE || block->block_type == BLOCK_TYPE_DIRECTORY) { inode = (minifile_t) block; inode->u.data.mutex = semaphore_create(); semaphore_initialize(inode->u.data.mutex, 1); block->num_open = 1; } return block; } else { return NULL; } }
int add_error_variable (struct conn_s *connptr, const char *key, const char *val) { if (!connptr->error_variables) if (! (connptr->error_variables = hashmap_create (ERRVAR_BUCKETCOUNT))) return (-1); return hashmap_insert (connptr->error_variables, key, val, strlen (val) + 1); }
void builtin_insert(HashMap *symtab, Builtin *builtin) { while (builtin->d_name) { hashmap_insert ( symtab, hashitem_construct(BUILTIN, builtin->d_name, builtin, root_nop) ); builtin++; } }
static void arbitrary( void ) { int res, val; hashmap_t *hashmap; hashmap = hashmap_create(); assert( hashmap ); res = hashmap_insert( hashmap, "test", 100 ); assert( 0 == res ); res = hashmap_insert( hashmap, "test", 101 ); assert( 0 == res ); res = hashmap_insert( hashmap, "test2", 3 ); assert( 0 == res ); res = hashmap_find( hashmap, "test2", NULL ); assert( 1 == res ); res = hashmap_find( hashmap, "test2", &val ); assert( 1 == res ); assert( 3 == val ); res = hashmap_find( hashmap, "test", &val ); assert( 1 == res ); assert( 101 == val ); hashmap_erase( hashmap, "test" ); res = hashmap_find( hashmap, "test", NULL ); assert( 0 == res ); hashmap_erase( hashmap, "test" ); res = hashmap_find( hashmap, "test", NULL ); assert( 0 == res ); hashmap_destroy( hashmap ); }
static int setup_index_buffer (SceneData* sceneData, SceneObject* so, SoData* soData) { Draw* method = so->draw; char bufferKey[KEYSIZE]; Attribute* indices = method->indices; GLuint bufferHandle; size_t bufferUsers; if ( indices && 1 == indices->useBuffer ) { snprintf (bufferKey, KEYSIZE, "%p", indices); bufferHandle = (GLuint)hashmap_find ( bufferKey, sceneData->mapBuffer2Handle ); if ( !bufferHandle ) { glGenBuffers(1, &bufferHandle); glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, bufferHandle); glBufferData (GL_ELEMENT_ARRAY_BUFFER, indices->size, indices->data, GL_STATIC_DRAW); glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0); // update sceneData list_add ( (void*)bufferHandle, sceneData->listBuffers ); hashmap_insert ( bufferKey, (void*)bufferHandle, sceneData->mapBuffer2Handle ); hashmap_insert ( bufferKey, (void*)1, sceneData->countBufferUsers ); } else { // update sceneData bufferUsers = (size_t)hashmap_find (bufferKey, sceneData->countBufferUsers); hashmap_insert (bufferKey, (void*)(bufferUsers+1), sceneData->countBufferUsers); } } return 0; }
int add_new_errorpage (char *filepath, unsigned int errornum) { char errornbuf[ERRORNUM_BUFSIZE]; config.errorpages = hashmap_create (ERRPAGES_BUCKETCOUNT); if (!config.errorpages) return (-1); snprintf (errornbuf, ERRORNUM_BUFSIZE, "%u", errornum); if (hashmap_insert (config.errorpages, errornbuf, filepath, strlen (filepath) + 1) < 0) return (-1); return (0); }
// moves entry li to the front and sets // its id to id. static inline void move_to_front(cache_t* c, long li, cache_id_t id) { cache_id_t to_free = NULL; cache_list_entry_t* le; cache_list_entry_t* prev = NULL; cache_list_entry_t* next = NULL; cache_list_entry_t* head = NULL; hm_entry_t entry; assert( li >= 0 ); le = & c->list[li]; if( li != c->head ) { if( le->prev != -1 ) prev = & c->list[le->prev]; if( le->next != -1 ) next = & c->list[le->next]; // remove the element from the list if( prev ) prev->next = le->next; else c->head = le->next; if( next ) next->prev = le->prev; else c->tail = le->prev; // now place it at the beginning of the list. head = &c->list[c->head]; le->next = c->head; le->prev = -1; head->prev = li; c->head = li; } // update the IDs. if( le->id ) { // remove the old entry entry.key = le->id; entry.value = NULL; hashmap_delete(&c->mapping, &entry); to_free = le->id; } le->id = c->copy_id(id); if( to_free ) c->free_id(to_free); entry.key = le->id; entry.value = (void*) li; hashmap_insert(&c->mapping, &entry); }
int cache_update(struct cache_s *cache, const char* key, const char* value, size_t len) { pthread_rwlock_wrlock(&cache -> lock); ssize_t freed_size = MAX_CACHE_SIZE - cache -> curr_size; ssize_t size; if(len > MAX_OBJECT_SIZE){ //MITLogWrite(MITLOG_LEVEL_WARNING, // "object size (%d) exceeds the MAX_OBJECT_SIZE(%d)", // len, MAX_OBJECT_SIZE); pthread_rwlock_unlock(&cache -> lock); return 0; } if(len > MAX_CACHE_SIZE - cache -> curr_size){ //MITLogWrite(MITLOG_LEVEL_WARNING, // "Cache (size: %d) is almost full." // "Should evict object for incoming object" // "(size: %d)", cache -> curr_size, len); while(freed_size < len){ if((size = hashmap_remove_lru(cache -> map)) < 0){ pthread_rwlock_unlock(&cache -> lock); return -1; } //MITLogWrite(MITLOG_LEVEL_COMMON, "successfully evict %d bytes", size); cache -> curr_size = cache -> curr_size - size; freed_size += size; } } if(hashmap_insert(cache -> map, key, value, len) < 0){ pthread_rwlock_unlock(&cache -> lock); return -1; } cache -> curr_size = cache -> curr_size + len; MITLogWrite(MITLOG_LEVEL_COMMON, "New cache object added, current size: %d", cache -> curr_size); pthread_rwlock_unlock(&cache -> lock); return 0; }
void lua_module_register(const char* name, size_t length, const uuid_t uuid, lua_fn loader, lua_preload_fn preload) { hash_t namehash = hash(name, length); mutex_lock(_lua_modulemap_lock); lua_modulemap_entry_t* entry = hashmap_lookup(_lua_modulemap, namehash); if (!entry) { entry = memory_allocate(HASH_LUA, sizeof(lua_modulemap_entry_t), 0, MEMORY_PERSISTENT); entry->name = namehash; entry->uuid = uuid; entry->loader = loader; entry->preload = preload; hashmap_insert(_lua_modulemap, hash(name, length), entry); } mutex_unlock(_lua_modulemap_lock); }
int main() { int i; struct test *t; struct hashmap map; hashmap_init(&map, hash_test, cmp_test); for (i = 0; i < COUNT; ++i) { t = calloc(1, sizeof *t); t->i = i; t->j = i + 123; hashmap_insert(&map, &t->node, &i); } for (i = 0; i < GET_COUNT; ++i) { int k = rand() % COUNT; struct test *t; struct hash_node *node = hashmap_get(&map, &k); if (node == NULL) { printf("%d not found\n", k); assert(0); } t = container_of(node, struct test, node); assert (t->i == k && t->j == k + 123); } for (i = 0; i < COUNT; ++i) { int k = COUNT - 1 - i; struct hash_node *node = hashmap_remove(&map, &k); if (node == NULL) { printf("%d not found\n", k); assert(0); } t = container_of(node, struct test, node);; assert (t->i == k && t->j == k + 123); } assert(map.len == MIN_SLOTS); return 0; }
/* * Take a complete header line and break it apart (into a key and the data.) * Now insert this information into the hashmap for the connection so it * can be retrieved and manipulated later. */ static inline int add_header_to_connection(hashmap_t hashofheaders, char *header, size_t len) { char *sep; /* Get rid of the new line and return at the end */ len -= chomp(header, len); sep = strchr(header, ':'); if (!sep) return -1; /* Blank out colons, spaces, and tabs. */ while (*sep == ':' || *sep == ' ' || *sep == '\t') *sep++ = '\0'; /* Calculate the new length of just the data */ len -= sep - header - 1; return hashmap_insert(hashofheaders, header, sep, len); }
static int setup_so (SceneData* sceneData, SceneObject* so) { SoData* soData = malloc(sizeof(SoData)); soData->attributes = list_alloc(); soData->textures = list_alloc(); GLuint programHandle; if ( -1 == setup_program( sceneData, so, soData, &programHandle ) ) return -1; if ( -1 == setup_attributes( sceneData, so, soData, programHandle ) ) return -1; if ( -1 == setup_textures ( sceneData, so, soData, programHandle ) ) return -1; if ( -1 == setup_index_buffer ( sceneData, so, soData ) ) return -1; char soKey[KEYSIZE]; snprintf ( soKey, KEYSIZE, "%p", so ); hashmap_insert ( soKey, soData, sceneData->mapSo2SoData ); return 0; }
static void clear_test( void ) { int res; hashmap_t hashmap; size_t size; unsigned int existed; res = hashmap_init( &hashmap ); assert( 0 == res ); res = hashmap_insert( &hashmap, "test", 1 ); assert( 0 == res ); hashmap_clear( &hashmap ); size = hashmap_size( &hashmap ); assert( 0 == size ); res = hashmap_insert_existed( &hashmap, "test", 1, &existed ); assert( 0 == res ); assert( 0 == existed ); hashmap_term( &hashmap ); }
Channel createChannel(Msg_t msg) { int newsockfd; if( (newsockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){ perror("<LOG socket_s.c> Socket call failed <end>"); exit(EXIT_FAILURE); } Channel ch = malloc(sizeof(channel_t)); ch->client = msg->data.socket_client_t.client; ch->port = ch->client->sin_port; ch->client->sin_port += openedSockets++; if( (bind( newsockfd, (struct sockaddr *) ch->client, SOCKET_SIZE)) == -1 ){ perror("<LOG socket_s.c> Bind call failed in createChannel() <end>"); exit(EXIT_FAILURE); } hashmap_insert(sockets_hmap, (void *) newsockfd , ch->port); return ch; }
static int tear_down_attributes (SceneData* sceneData, SceneObject* so, SoData* soData) { Attribute* attribute; char bufferKey[KEYSIZE]; size_t bufferUsers; GLuint bufferHandle; int i; for (i = 0; i < list_size(soData->attributes); i++) { attribute = list_get ( i, soData->attributes ); if ( 1 != attribute->useBuffer ) continue; snprintf (bufferKey, KEYSIZE, "%p", attribute); bufferHandle = (GLuint)hashmap_find ( bufferKey, sceneData->mapBuffer2Handle ); bufferUsers = (size_t)hashmap_find ( bufferKey, sceneData->countBufferUsers ); bufferUsers--; if ( 0 == bufferUsers) { // free resources glDeleteBuffers (1, &bufferHandle); list_remove_elem ( (void*)bufferHandle, sceneData->listBuffers ); hashmap_delete ( bufferKey, sceneData->mapBuffer2Handle ); hashmap_delete ( bufferKey, sceneData->countBufferUsers ); } else { hashmap_insert ( bufferKey, (void*)bufferUsers, sceneData->countBufferUsers ); } } return 0; }