DirectResult direct_map_insert( DirectMap *map, const void *key, void *object ) { unsigned int hash; int pos; MapEntry *entry; D_DEBUG_AT( Direct_Map, "%s( key %p, object %p )\n", __func__, key, object ); DIRECT_MAP_ASSERT( map ); D_ASSERT( key != NULL ); D_ASSERT( object != NULL ); /* Need to resize the map? */ if ((map->count + map->removed) > map->size / 4) resize_map( map, map->size * 3 ); hash = map->hash( map, key, map->ctx ); pos = hash % map->size; D_DEBUG_AT( Direct_Map, " -> hash %u, pos %d\n", hash, pos ); entry = &map->entries[pos]; while (entry->object && entry->object != REMOVED) { if (entry->hash == hash && map->compare( map, key, entry->object, map->ctx )) { if (entry->object == object) { D_DEBUG_AT( Direct_Map, " -> same object with matching key already exists\n" ); return DR_BUSY; } else { D_DEBUG_AT( Direct_Map, " -> different object with matching key already exists\n" ); D_BUG( "different object with matching key already exists" ); return DR_BUG; } } if (++pos == map->size) pos = 0; entry = &map->entries[pos]; } if (entry->object == REMOVED) map->removed--; entry->hash = hash; entry->object = object; map->count++; D_DEBUG_AT( Direct_Map, " -> new count = %d, removed = %d, size = %d\n", map->count, map->removed, map->size ); return DR_OK; }
void do_change_map_size(partition_map_header *map) { long size; if (map == NULL) { bad_input("No partition map exists"); return; } if (!rflag && map->writable == 0) { printf("The map is not writable.\n"); } if (get_number_argument("New size: ", &size, kDefault) == 0) { bad_input("Bad size"); return; } resize_map(size, map); }