int s2n_map_add(struct s2n_map *map, struct s2n_blob *key, struct s2n_blob *value) { if (map->immutable) { S2N_ERROR(S2N_ERR_MAP_IMMUTABLE); } if (map->capacity < (map->size * 2)) { /* Embiggen the map */ GUARD(s2n_map_embiggen(map, map->capacity * 2)); } uint32_t slot = s2n_map_slot(map, key); /* Linear probing until we find an empty slot */ while(map->table[slot].key.size) { if (key->size != map->table[slot].key.size || memcmp(key->data, map->table[slot].key.data, key->size)) { slot++; slot %= map->capacity; continue; } /* We found a duplicate key */ S2N_ERROR(S2N_ERR_MAP_DUPLICATE); } GUARD(s2n_dup(key, &map->table[slot].key)); GUARD(s2n_dup(value, &map->table[slot].value)); map->size++; return 0; }
struct s2n_map *s2n_map_new() { struct s2n_blob mem; struct s2n_map *map; GUARD_PTR(s2n_alloc(&mem, sizeof(struct s2n_map))); map = (void *) mem.data; map->capacity = 0; map->size = 0; map->immutable = 0; map->table = NULL; GUARD_PTR(s2n_map_embiggen(map, S2N_INITIAL_TABLE_SIZE)); return map; }