Fixed some memory-management related bugs and a crash bug.
This commit is contained in:
parent
573438eb6a
commit
3f7da48836
|
@ -22,7 +22,6 @@ struct MtTagRegistry
|
||||||
MtTagEntry *entries;
|
MtTagEntry *entries;
|
||||||
|
|
||||||
u32_t next_identifier;
|
u32_t next_identifier;
|
||||||
|
|
||||||
u32_t num_collections;
|
u32_t num_collections;
|
||||||
MtTagCollection *collections;
|
MtTagCollection *collections;
|
||||||
};
|
};
|
||||||
|
@ -36,10 +35,10 @@ struct MtTagEntry
|
||||||
struct MtTagCollection
|
struct MtTagCollection
|
||||||
{
|
{
|
||||||
u32_t capacity;
|
u32_t capacity;
|
||||||
u32_t num_tags;
|
u32_t usage;
|
||||||
|
|
||||||
/// @brief An array of pointers into the tag-registry's 'entries'-array.
|
/// @brief An array of indices for the tag-registry's 'entries'-array.
|
||||||
MtTagEntry **tags;
|
u32_t *tag_indices;
|
||||||
};
|
};
|
||||||
|
|
||||||
MtTagRegistry mt_create_tag_registry(void *parent_state);
|
MtTagRegistry mt_create_tag_registry(void *parent_state);
|
||||||
|
|
|
@ -7,10 +7,10 @@ MtTagRegistry mt_create_tag_registry(void *parent_state)
|
||||||
{
|
{
|
||||||
MtTagRegistry registry;
|
MtTagRegistry registry;
|
||||||
registry.parent_state = parent_state;
|
registry.parent_state = parent_state;
|
||||||
registry.num_collections = 256;
|
registry.num_collections = 512;
|
||||||
registry.collections = calloc(sizeof(MtTagCollection), registry.num_collections);
|
registry.collections = calloc(sizeof(MtTagCollection), registry.num_collections);
|
||||||
registry.next_identifier = 1;
|
registry.next_identifier = 1;
|
||||||
registry.entries_capacity = 512;
|
registry.entries_capacity = 2048;
|
||||||
registry.num_entries = 0;
|
registry.num_entries = 0;
|
||||||
registry.entries = calloc(sizeof(MtTagEntry), registry.entries_capacity);
|
registry.entries = calloc(sizeof(MtTagEntry), registry.entries_capacity);
|
||||||
return registry;
|
return registry;
|
||||||
|
@ -22,12 +22,12 @@ void mt_delete_all_tag_registry_collections(MtTagRegistry registry)
|
||||||
while(index < registry.num_collections)
|
while(index < registry.num_collections)
|
||||||
{
|
{
|
||||||
MtTagCollection collection = registry.collections[index];
|
MtTagCollection collection = registry.collections[index];
|
||||||
if(collection.capacity == 0)
|
if(collection.capacity != 0)
|
||||||
return;
|
free(collection.tag_indices);
|
||||||
|
|
||||||
free(collection.tags);
|
|
||||||
++index;
|
++index;
|
||||||
}
|
}
|
||||||
|
free(registry.collections);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mt_delete_all_tag_registry_entries(MtTagRegistry registry)
|
void mt_delete_all_tag_registry_entries(MtTagRegistry registry)
|
||||||
|
@ -36,9 +36,9 @@ void mt_delete_all_tag_registry_entries(MtTagRegistry registry)
|
||||||
while(index < registry.num_entries)
|
while(index < registry.num_entries)
|
||||||
{
|
{
|
||||||
MtTagEntry entry = registry.entries[index];
|
MtTagEntry entry = registry.entries[index];
|
||||||
if(entry.label == NULL)
|
if(entry.label != NULL)
|
||||||
return;
|
|
||||||
free(entry.label);
|
free(entry.label);
|
||||||
|
|
||||||
++index;
|
++index;
|
||||||
}
|
}
|
||||||
free(registry.entries);
|
free(registry.entries);
|
||||||
|
@ -85,13 +85,18 @@ MtTagEntry * mt_create_tag_hash_entry(MtTagRegistry *registry, const char *tag_l
|
||||||
u32_t collection_index = mt_tag_label_to_hash(tag_label, registry->num_collections);
|
u32_t collection_index = mt_tag_label_to_hash(tag_label, registry->num_collections);
|
||||||
MtTagCollection *collection = ®istry->collections[collection_index];
|
MtTagCollection *collection = ®istry->collections[collection_index];
|
||||||
|
|
||||||
if(collection->num_tags >= collection->capacity)
|
if(collection->usage >= collection->capacity)
|
||||||
{
|
{
|
||||||
collection->capacity *= 2;
|
collection->capacity *= 2;
|
||||||
collection->tags = realloc(collection->tags, sizeof(MtTagEntry *) * collection->capacity);
|
if(collection->capacity == 0)
|
||||||
|
collection->capacity = 4;
|
||||||
|
|
||||||
|
collection->tag_indices = realloc(collection->tag_indices, sizeof(u32_t) * collection->capacity);
|
||||||
|
rr_memset(&collection->tag_indices[collection->usage], collection->capacity - collection->usage, 0x00);
|
||||||
}
|
}
|
||||||
MtTagEntry *entry = collection->tags[collection->num_tags];
|
MtTagEntry *entry = ®istry->entries[collection->tag_indices[collection->usage]];
|
||||||
++collection->num_tags;
|
++registry->num_entries;
|
||||||
|
++collection->usage;
|
||||||
|
|
||||||
usz_t len_tag_label = rr_measure_string(tag_label);
|
usz_t len_tag_label = rr_measure_string(tag_label);
|
||||||
|
|
||||||
|
@ -110,9 +115,9 @@ u32_t mt_get_tag_id(MtTagRegistry *registry, const char *tag_label)
|
||||||
u32_t collection_index = mt_tag_label_to_hash(tag_label, registry->num_collections);
|
u32_t collection_index = mt_tag_label_to_hash(tag_label, registry->num_collections);
|
||||||
MtTagCollection collection = registry->collections[collection_index];
|
MtTagCollection collection = registry->collections[collection_index];
|
||||||
usz_t entry_index = 0;
|
usz_t entry_index = 0;
|
||||||
while(entry_index < collection.num_tags)
|
while(entry_index < collection.usage)
|
||||||
{
|
{
|
||||||
MtTagEntry *entry = collection.tags[entry_index];
|
MtTagEntry *entry = ®istry->entries[collection.tag_indices[entry_index]];
|
||||||
if(rr_strings_equal(entry->label, tag_label))
|
if(rr_strings_equal(entry->label, tag_label))
|
||||||
return entry->identifier;
|
return entry->identifier;
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ int main(int argc, char **argv)
|
||||||
if(some_tag != mt_get_tag_id(&tag_registry, "some_tag"))
|
if(some_tag != mt_get_tag_id(&tag_registry, "some_tag"))
|
||||||
{
|
{
|
||||||
puts("Inconsistent returned tag identifiers.");
|
puts("Inconsistent returned tag identifiers.");
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
mt_delete_tag_registry(tag_registry);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue