-
Notifications
You must be signed in to change notification settings - Fork 934
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HASH_DEL should be able to delete a const-qualified node
HASH_DEL doesn't need to modify the target node; in fact it should not, because that target node is usually going straight back to `free` and any modifications to it would be wasted effort. So, let's make that an actual tested guarantee, and let's make it const-correct. Inspired by the discussion in #253.
- Loading branch information
1 parent
095425f
commit ca98384
Showing
5 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include <assert.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "uthash.h" | ||
|
||
struct item { | ||
int payload; | ||
UT_hash_handle hh; | ||
}; | ||
|
||
void delete_without_modifying(struct item *head, const struct item *p) | ||
{ | ||
struct item old; | ||
memcpy(&old, p, sizeof(struct item)); // also copy the padding bits | ||
assert(memcmp(&old, p, sizeof(struct item)) == 0); | ||
assert(p->hh.tbl == head->hh.tbl); // class invariant | ||
HASH_DEL(head, p); | ||
assert(memcmp(&old, p, sizeof(struct item)) == 0); // unmodified by HASH_DEL | ||
} | ||
|
||
int main() | ||
{ | ||
struct item *items = NULL; | ||
struct item *found = NULL; | ||
int fortytwo = 42; | ||
int i; | ||
|
||
for (i=0; i < 100; i++) { | ||
struct item *p = (struct item *)malloc(sizeof *p); | ||
p->payload = i; | ||
HASH_ADD_INT(items, payload, p); | ||
} | ||
assert(HASH_COUNT(items) == 100); | ||
|
||
// Delete item "42" from the hash, wherever it is. | ||
HASH_FIND_INT(items, &fortytwo, found); | ||
assert(found != NULL); | ||
assert(found->payload == 42); | ||
delete_without_modifying(items, found); | ||
|
||
assert(HASH_COUNT(items) == 99); | ||
HASH_FIND_INT(items, &fortytwo, found); | ||
assert(found == NULL); | ||
|
||
// Delete the very first item in the hash. | ||
assert(items != NULL); | ||
i = items->payload; | ||
delete_without_modifying(items, items); | ||
|
||
assert(HASH_COUNT(items) == 98); | ||
HASH_FIND_INT(items, &i, found); | ||
assert(found == NULL); | ||
|
||
// leak the items, we don't care | ||
|
||
return 0; | ||
} |