Fixed memory leak

Optimized memory usage
This commit is contained in:
Luca Deri
2018-05-18 10:15:05 +02:00
parent f56b083bc3
commit 5d3ac78250
2 changed files with 47 additions and 45 deletions
+6 -1
View File
@@ -2120,7 +2120,7 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(void) {
ndpi_str->custom_categories.hostnames_shadow.ac_automa = ac_automata_init(ac_match_handler); ndpi_str->custom_categories.hostnames_shadow.ac_automa = ac_automata_init(ac_match_handler);
#endif #endif
ndpi_str->custom_categories.hostnames_hash = (void*)ht_create(65536); ndpi_str->custom_categories.hostnames_hash = NULL;
ndpi_str->custom_categories.ipAddresses = ndpi_New_Patricia(32 /* IPv4 */); ndpi_str->custom_categories.ipAddresses = ndpi_New_Patricia(32 /* IPv4 */);
ndpi_str->custom_categories.ipAddresses_shadow = ndpi_New_Patricia(32 /* IPv4 */); ndpi_str->custom_categories.ipAddresses_shadow = ndpi_New_Patricia(32 /* IPv4 */);
@@ -3924,7 +3924,12 @@ int ndpi_load_hostname_category(struct ndpi_detection_module_struct *ndpi_struct
return(-1); return(-1);
if(!ndpi_struct->enable_category_substring_match) { if(!ndpi_struct->enable_category_substring_match) {
if(ndpi_struct->custom_categories.hostnames_hash == NULL)
ndpi_struct->custom_categories.hostnames_hash = (void*)ht_create(65536);
if(ndpi_struct->custom_categories.hostnames_hash)
ht_set((hashtable_t*)ndpi_struct->custom_categories.hostnames_hash, name, (u_int16_t)category); ht_set((hashtable_t*)ndpi_struct->custom_categories.hostnames_hash, name, (u_int16_t)category);
return(0); return(0);
} else { } else {
AC_PATTERN_t ac_pattern; AC_PATTERN_t ac_pattern;
+8 -11
View File
@@ -17,15 +17,14 @@ hashtable_t *ht_create( int size ) {
if(size < 1) return NULL; if(size < 1) return NULL;
/* Allocate the table itself. */ /* Allocate the table itself. */
if( ( hashtable = ndpi_malloc( sizeof( hashtable_t ) ) ) == NULL ) { if((hashtable = ndpi_malloc(sizeof(hashtable_t))) == NULL)
return NULL; return NULL;
}
/* Allocate pointers to the head nodes. */ /* Allocate pointers to the head nodes. */
if( ( hashtable->table = ndpi_malloc( sizeof( entry_t * ) * size ) ) == NULL ) { if((hashtable->table = ndpi_malloc(sizeof(entry_t *) * size)) == NULL)
return NULL; return NULL;
} else {
for( i = 0; i < size; i++ ) { for(i = 0; i < size; i++)
hashtable->table[i] = NULL; hashtable->table[i] = NULL;
} }
@@ -38,8 +37,7 @@ hashtable_t *ht_create( int size ) {
/* Hash a string for a particular hash table. */ /* Hash a string for a particular hash table. */
int ht_hash(hashtable_t *hashtable, char *key) { int ht_hash(hashtable_t *hashtable, char *key) {
unsigned long int hashval = 0;
unsigned long int hashval;
int i = 0; int i = 0;
/* Convert our string to an integer */ /* Convert our string to an integer */
@@ -58,13 +56,11 @@ int ht_hash( hashtable_t *hashtable, char *key ) {
entry_t *ht_newpair(char *key, u_int16_t value) { entry_t *ht_newpair(char *key, u_int16_t value) {
entry_t *newpair; entry_t *newpair;
if( ( newpair = ndpi_malloc( sizeof( entry_t ) ) ) == NULL ) { if((newpair = ndpi_malloc(sizeof(entry_t))) == NULL)
return NULL; return NULL;
}
if( ( newpair->key = ndpi_strdup( key ) ) == NULL ) { if((newpair->key = ndpi_strdup(key)) == NULL)
return NULL; return NULL;
}
newpair->value = value, newpair->next = NULL; newpair->value = value, newpair->next = NULL;
@@ -163,6 +159,7 @@ void ht_free(hashtable_t *hashtable) {
} }
} }
ndpi_free(hashtable->table);
ndpi_free(hashtable); ndpi_free(hashtable);
} }