hcreate: add alloc/free func hook for user to deallocate memory
Signed-off-by: guohao15 <guohao15@xiaomi.com>
This commit is contained in:
parent
43bcac952a
commit
c63adb5c14
2 changed files with 18 additions and 4 deletions
|
@ -43,6 +43,7 @@ struct hsearch_data
|
||||||
{
|
{
|
||||||
FAR struct internal_head *htable;
|
FAR struct internal_head *htable;
|
||||||
size_t htablesize;
|
size_t htablesize;
|
||||||
|
CODE void (*free_entry)(FAR ENTRY *entry);
|
||||||
};
|
};
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
|
|
@ -82,6 +82,16 @@ struct internal_entry
|
||||||
SLIST_HEAD(internal_head, internal_entry);
|
SLIST_HEAD(internal_head, internal_entry);
|
||||||
extern uint32_t (*g_default_hash)(FAR const void *, size_t);
|
extern uint32_t (*g_default_hash)(FAR const void *, size_t);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static void hfree_r(FAR ENTRY *entry)
|
||||||
|
{
|
||||||
|
lib_free(entry->key);
|
||||||
|
lib_free(entry->data);
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Functions
|
* Public Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
@ -157,6 +167,11 @@ int hcreate_r(size_t nel, FAR struct hsearch_data *htab)
|
||||||
SLIST_INIT(&(htab->htable[idx]));
|
SLIST_INIT(&(htab->htable[idx]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (htab->free_entry == NULL)
|
||||||
|
{
|
||||||
|
htab->free_entry = hfree_r;
|
||||||
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,8 +205,7 @@ void hdestroy_r(FAR struct hsearch_data *htab)
|
||||||
{
|
{
|
||||||
ie = SLIST_FIRST(&(htab->htable[idx]));
|
ie = SLIST_FIRST(&(htab->htable[idx]));
|
||||||
SLIST_REMOVE_HEAD(&(htab->htable[idx]), link);
|
SLIST_REMOVE_HEAD(&(htab->htable[idx]), link);
|
||||||
lib_free(ie->ent.key);
|
htab->free_entry(&ie->ent);
|
||||||
lib_free(ie->ent.data);
|
|
||||||
lib_free(ie);
|
lib_free(ie);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -245,8 +259,7 @@ int hsearch_r(ENTRY item, ACTION action, FAR ENTRY **retval,
|
||||||
if (ie != NULL)
|
if (ie != NULL)
|
||||||
{
|
{
|
||||||
SLIST_REMOVE(head, ie, internal_entry, link);
|
SLIST_REMOVE(head, ie, internal_entry, link);
|
||||||
lib_free(ie->ent.key);
|
htab->free_entry(&ie->ent);
|
||||||
lib_free(ie->ent.data);
|
|
||||||
lib_free(ie);
|
lib_free(ie);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue