libselinux: adapting hashtab to libselinux

To adapt to the scenarios of libselinux, this patch does three things:

1. Add a new function hashtab_destroy_key. This function is used to
   reclaim memory using the customized key destruction method.

2. Changed the macro definition to _SELINUX_HASHTAB_H_.

3. Add a function declaration to the header file.

Signed-off-by: wanghuizhao <wanghuizhao1@huawei.com>
Acked-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
wanghuizhao 2023-03-08 17:53:07 +08:00 committed by James Carter
parent d95bc8b755
commit 4a420508a9
2 changed files with 30 additions and 2 deletions

View File

@ -156,6 +156,32 @@ void hashtab_destroy(hashtab_t h)
free(h);
}
void hashtab_destroy_key(hashtab_t h,
int (*destroy_key) (hashtab_key_t k))
{
unsigned int i;
hashtab_ptr_t cur, temp;
if (!h)
return;
for (i = 0; i < h->size; i++) {
cur = h->htable[i];
while (cur != NULL) {
temp = cur;
cur = cur->next;
destroy_key(temp->key);
free(temp);
}
h->htable[i] = NULL;
}
free(h->htable);
h->htable = NULL;
free(h);
}
int hashtab_map(hashtab_t h,
int (*apply) (hashtab_key_t k,
hashtab_datum_t d, void *args), void *args)

View File

@ -11,8 +11,8 @@
* provided by the creator of the table.
*/
#ifndef _NEWROLE_HASHTAB_H_
#define _NEWROLE_HASHTAB_H_
#ifndef _SELINUX_HASHTAB_H_
#define _SELINUX_HASHTAB_H_
#include <stdint.h>
#include <errno.h>
@ -93,6 +93,8 @@ extern hashtab_datum_t hashtab_search(hashtab_t h, const_hashtab_key_t k);
Destroys the specified hash table.
*/
extern void hashtab_destroy(hashtab_t h);
extern void hashtab_destroy_key(hashtab_t h,
int (*destroy_key) (hashtab_key_t k));
/*
Applies the specified apply function to (key,datum,args)