Differences From Artifact [0f765c0951]:
- File src/runtime/hashtable.m — part of check-in [0509d7a844] at 2019-01-03 19:13:03 on branch trunk — Update copyright (user: js, size: 5123) [annotate] [blame] [check-ins using]
To Artifact [e12d752357]:
- File
src/runtime/hashtable.m
— part of check-in
[ef6d69931e]
at
2019-02-07 00:46:41
on branch trunk
— Make style consistent between ObjFW and ObjFW_RT
ObjFW_RT used to be a separate project that followed the BSD style, as
it was written in pure C, while ObjFW's style is based on the BSD style
with changes to make it a better fit for Objective-C. This commit
changes ObjFW_RT to use the same style as ObjFW. (user: js, size: 5173) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
75 76 77 78 79 80 81 | return table; } static void resize(struct objc_hashtable *table, uint32_t count) { | | | | | | | | | | | | > | | | | | 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | return table; } static void resize(struct objc_hashtable *table, uint32_t count) { uint32_t fullness, newSize; struct objc_hashtable_bucket **newData; if (count > UINT32_MAX / sizeof(*table->data) || count > UINT32_MAX / 8) OBJC_ERROR("Integer overflow!"); fullness = count * 8 / table->size; if (fullness >= 6) { if (table->size > UINT32_MAX / 2) return; newSize = table->size * 2; } else if (fullness <= 1) newSize = table->size / 2; else return; if (count < table->count && newSize < 16) return; if ((newData = calloc(newSize, sizeof(sizeof(*newData)))) == NULL) OBJC_ERROR("Not enough memory to resize hash table!"); for (uint32_t i = 0; i < table->size; i++) { if (table->data[i] != NULL && table->data[i] != &objc_deleted_bucket) { uint32_t j, last; last = newSize; for (j = table->data[i]->hash & (newSize - 1); j < last && newData[j] != NULL; j++); if (j >= last) { last = table->data[i]->hash & (newSize - 1); for (j = 0; j < last && newData[j] != NULL; j++); } if (j >= last) OBJC_ERROR("No free bucket!"); newData[j] = table->data[i]; } } free(table->data); table->data = newData; table->size = newSize; } static inline bool indexForKey(struct objc_hashtable *table, const void *key, uint32_t *idx) { uint32_t i, hash; hash = table->hash(key) & (table->size - 1); for (i = hash; i < table->size && table->data[i] != NULL; i++) { if (table->data[i] == &objc_deleted_bucket) |
︙ | ︙ | |||
162 163 164 165 166 167 168 | } return false; } void objc_hashtable_set(struct objc_hashtable *table, const void *key, | | | | | 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | } return false; } void objc_hashtable_set(struct objc_hashtable *table, const void *key, const void *object) { uint32_t i, hash, last; struct objc_hashtable_bucket *bucket; if (indexForKey(table, key, &i)) { table->data[i]->object = object; return; } resize(table, table->count + 1); hash = table->hash(key); last = table->size; |
︙ | ︙ | |||
195 196 197 198 199 200 201 | OBJC_ERROR("No free bucket!"); if ((bucket = malloc(sizeof(*bucket))) == NULL) OBJC_ERROR("Not enough memory to allocate hash table bucket!"); bucket->key = key; bucket->hash = hash; | | | | | | 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | OBJC_ERROR("No free bucket!"); if ((bucket = malloc(sizeof(*bucket))) == NULL) OBJC_ERROR("Not enough memory to allocate hash table bucket!"); bucket->key = key; bucket->hash = hash; bucket->object = object; table->data[i] = bucket; table->count++; } void * objc_hashtable_get(struct objc_hashtable *table, const void *key) { uint32_t idx; if (!indexForKey(table, key, &idx)) return NULL; return (void *)table->data[idx]->object; } void objc_hashtable_delete(struct objc_hashtable *table, const void *key) { uint32_t idx; if (!indexForKey(table, key, &idx)) return; free(table->data[idx]); table->data[idx] = &objc_deleted_bucket; table->count--; resize(table, table->count); |
︙ | ︙ |