| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/private/hash_table_impl.h> |
| 7 |
#include <proof_helpers/nondet.h> |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
bool aws_hash_table_is_valid(const struct aws_hash_table *map) { |
| 23 |
return map && map->p_impl; |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
bool aws_hash_iter_is_valid(const struct aws_hash_iter *iter) { |
| 30 |
if (!iter) { |
| 31 |
return false; |
| 32 |
} |
| 33 |
if (!iter->map) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
if (!aws_hash_table_is_valid(iter->map)) { |
| 37 |
return false; |
| 38 |
} |
| 39 |
if (iter->limit != iter->map->p_impl->entry_count) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
switch (iter->status) { |
| 44 |
case AWS_HASH_ITER_STATUS_DONE: |
| 45 |
|
| 46 |
return iter->slot == iter->limit; |
| 47 |
case AWS_HASH_ITER_STATUS_DELETE_CALLED: |
| 48 |
|
| 49 |
|
| 50 |
return iter->slot <= iter->limit; |
| 51 |
case AWS_HASH_ITER_STATUS_READY_FOR_USE: |
| 52 |
|
| 53 |
return iter->slot < iter->limit; |
| 54 |
} |
| 55 |
|
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
void make_hash_table_with_no_backing_store(struct aws_hash_table *map, size_t max_table_entries) { |
| 67 |
if (map != NULL) { |
| 68 |
map->p_impl = malloc(sizeof(struct hash_table_state)); |
| 69 |
__CPROVER_assume(map->p_impl != NULL); |
| 70 |
__CPROVER_assume(map->p_impl->entry_count <= max_table_entries); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
void aws_hash_table_clear(struct aws_hash_table *map) { |
| 78 |
AWS_PRECONDITION(aws_hash_table_is_valid(map)); |
| 79 |
struct hash_table_state *state = map->p_impl; |
| 80 |
state->entry_count = 0; |
| 81 |
AWS_POSTCONDITION(aws_hash_table_is_valid(map)); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
int aws_hash_table_put(struct aws_hash_table *map, const void *key, void *value, int *was_created) { |
| 88 |
AWS_PRECONDITION(aws_hash_table_is_valid(map)); |
| 89 |
|
| 90 |
int track_was_created; |
| 91 |
if (was_created) { |
| 92 |
*was_created = track_was_created; |
| 93 |
} |
| 94 |
|
| 95 |
int rval; |
| 96 |
|
| 97 |
struct hash_table_state *state = map->p_impl; |
| 98 |
|
| 99 |
|
| 100 |
if (state->entry_count == SIZE_MAX) { |
| 101 |
return track_was_created ? AWS_OP_ERR : rval; |
| 102 |
} |
| 103 |
|
| 104 |
if (rval == AWS_OP_SUCCESS) { |
| 105 |
state->entry_count++; |
| 106 |
} |
| 107 |
AWS_POSTCONDITION(aws_hash_table_is_valid(map)); |
| 108 |
return rval; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
int aws_hash_table_remove_element(struct aws_hash_table *map, struct aws_hash_element *p_value); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
int aws_hash_table_remove( |
| 120 |
struct aws_hash_table *map, |
| 121 |
const void *key, |
| 122 |
struct aws_hash_element *p_value, |
| 123 |
int *was_present); |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
int aws_hash_table_create( |
| 129 |
struct aws_hash_table *map, |
| 130 |
const void *key, |
| 131 |
struct aws_hash_element **p_elem, |
| 132 |
int *was_created); |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
#ifdef HASH_TABLE_FIND_ELEMENT_GENERATOR |
| 150 |
void HASH_TABLE_FIND_ELEMENT_GENERATOR( |
| 151 |
const struct aws_hash_table *map, |
| 152 |
const void *key, |
| 153 |
struct aws_hash_element *p_elem); |
| 154 |
#endif |
| 155 |
|
| 156 |
int aws_hash_table_find(const struct aws_hash_table *map, const void *key, struct aws_hash_element **p_elem) { |
| 157 |
AWS_PRECONDITION(aws_hash_table_is_valid(map), "Input hash_table [map] must be valid."); |
| 158 |
AWS_PRECONDITION(AWS_OBJECT_PTR_IS_WRITABLE(p_elem), "Input pointer [p_elem] must be writable."); |
| 159 |
const struct hash_table_state *const state = map->p_impl; |
| 160 |
if (nondet_bool()) { |
| 161 |
*p_elem = NULL; |
| 162 |
} else { |
| 163 |
*p_elem = malloc(sizeof(struct aws_hash_element)); |
| 164 |
#ifdef HASH_TABLE_FIND_ELEMENT_GENERATOR |
| 165 |
HASH_TABLE_FIND_ELEMENT_GENERATOR(map, key, *p_elem); |
| 166 |
#endif |
| 167 |
} |
| 168 |
AWS_POSTCONDITION(aws_hash_table_is_valid(map), "Output hash_table [map] must be valid."); |
| 169 |
return AWS_OP_SUCCESS; |
| 170 |
} |
| 171 |
|
| 172 |
#ifdef HASH_ITER_ELEMENT_GENERATOR |
| 173 |
void HASH_ITER_ELEMENT_GENERATOR(struct aws_hash_iter *new_iter, const struct aws_hash_iter *old_iter); |
| 174 |
#endif |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
struct aws_hash_iter aws_hash_iter_begin(const struct aws_hash_table *map) { |
| 179 |
|
| 180 |
AWS_PRECONDITION(aws_hash_table_is_valid(map)); |
| 181 |
|
| 182 |
|
| 183 |
struct aws_hash_iter rval; |
| 184 |
rval.map = map; |
| 185 |
rval.limit = map->p_impl->entry_count; |
| 186 |
rval.slot = 0; |
| 187 |
rval.status = (rval.slot == rval.limit) ? AWS_HASH_ITER_STATUS_DONE : AWS_HASH_ITER_STATUS_READY_FOR_USE; |
| 188 |
#ifdef HASH_ITER_ELEMENT_GENERATOR |
| 189 |
HASH_ITER_ELEMENT_GENERATOR(&rval, NULL); |
| 190 |
#endif |
| 191 |
return rval; |
| 192 |
} |
| 193 |
|
| 194 |
bool aws_hash_iter_done(const struct aws_hash_iter *iter) { |
| 195 |
AWS_PRECONDITION(aws_hash_iter_is_valid(iter)); |
| 196 |
AWS_PRECONDITION( |
| 197 |
iter->status == AWS_HASH_ITER_STATUS_DONE || iter->status == AWS_HASH_ITER_STATUS_READY_FOR_USE, |
| 198 |
"Input aws_hash_iter [iter] status should either be done or ready to use."); |
| 199 |
bool rval = iter->slot == iter->limit; |
| 200 |
assert(rval == (iter->status == AWS_HASH_ITER_STATUS_DONE)); |
| 201 |
return rval; |
| 202 |
} |
| 203 |
|
| 204 |
void aws_hash_iter_next(struct aws_hash_iter *iter) { |
| 205 |
AWS_PRECONDITION(aws_hash_iter_is_valid(iter)); |
| 206 |
|
| 207 |
if (iter->slot == iter->limit) { |
| 208 |
iter->status = AWS_HASH_ITER_STATUS_DONE; |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
struct aws_hash_iter rval; |
| 214 |
rval.map = iter->map; |
| 215 |
rval.limit = iter->limit; |
| 216 |
rval.slot = iter->slot + 1; |
| 217 |
rval.status = (rval.slot == rval.limit) ? AWS_HASH_ITER_STATUS_DONE : AWS_HASH_ITER_STATUS_READY_FOR_USE; |
| 218 |
#ifdef HASH_ITER_ELEMENT_GENERATOR |
| 219 |
HASH_ITER_ELEMENT_GENERATOR(&rval, iter); |
| 220 |
#endif |
| 221 |
|
| 222 |
*iter = rval; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
void aws_hash_iter_delete(struct aws_hash_iter *iter, bool destroy_contents) { |
| 227 |
AWS_PRECONDITION(aws_hash_iter_is_valid(iter)); |
| 228 |
AWS_PRECONDITION( |
| 229 |
iter->status == AWS_HASH_ITER_STATUS_READY_FOR_USE, "Input aws_hash_iter [iter] must be ready for use."); |
| 230 |
AWS_PRECONDITION(aws_hash_iter_is_valid(iter)); |
| 231 |
AWS_PRECONDITION( |
| 232 |
iter->map->p_impl->entry_count > 0, |
| 233 |
"The hash_table_state pointed by input [iter] must contain at least one entry."); |
| 234 |
|
| 235 |
|
| 236 |
iter->map->p_impl->entry_count--; |
| 237 |
|
| 238 |
|
| 239 |
struct aws_hash_iter rval; |
| 240 |
rval.map = iter->map; |
| 241 |
rval.slot = iter->slot; |
| 242 |
rval.limit = iter->limit - 1; |
| 243 |
rval.status = AWS_HASH_ITER_STATUS_DELETE_CALLED; |
| 244 |
*iter = rval; |
| 245 |
} |