Browse code

Implemented hash table in shared memory

Juha Heinanen authored on 19/01/2003 14:20:41
Showing 4 changed files
... ...
@@ -1,4 +1,4 @@
1
-/* domain_mod.c v 0.1 2002/12/27
1
+/* domain_mod.c v 0.2 2003/1/19
2 2
  *
3 3
  * Domain module
4 4
  *
... ...
@@ -29,6 +29,7 @@
29 29
 
30 30
 #include "domain_mod.h"
31 31
 #include <stdio.h>
32
+#include "../../mem/shm_mem.h"
32 33
 #include "../../sr_module.h"
33 34
 #include "domain.h"
34 35
 #include "fifo.h"
... ...
@@ -53,9 +54,9 @@ char* domain_domain_col = "domain";   /* Name of domain column */
53 54
  * Other module variables
54 55
  */
55 56
 db_con_t* db_handle = NULL;                  /* Database connection handle */
56
-struct domain_list *hash_table_1[HASH_SIZE]; /* Hash table for domains */
57
-struct domain_list *hash_table_2[HASH_SIZE]; /* Hash table for domains */
58
-struct domain_list **current_hash_table;
57
+struct domain_list ***hash_table;            /* Pointer to current hash table pointer */
58
+struct domain_list **hash_table_1;           /* Pointer to hash table 1 */
59
+struct domain_list **hash_table_2;           /* Pointer to hash table 2 */
59 60
 
60 61
 /*
61 62
  * Module interface
... ...
@@ -86,7 +87,7 @@ static int mod_init(void)
86 87
 
87 88
 	fprintf(stderr, "domain - initializing\n");
88 89
 	
89
-	/* Check if database module has been laoded */
90
+	/* Check if database module has been loaded */
90 91
 	if (bind_dbmod()) {
91 92
 		LOG(L_ERR, "domain:mod_init(): Unable to bind database module\n");
92 93
 		return -1;
... ...
@@ -104,10 +105,22 @@ static int mod_init(void)
104 105
 		(void)init_domain_fifo();
105 106
 
106 107
 		/* Initializing hash tables and hash table variable */
108
+		hash_table_1 = (struct domain_list **)shm_malloc(sizeof(struct domain_list *) * HASH_SIZE);
109
+		if (hash_table_1 == 0) {
110
+			LOG(L_ERR, "domain: mod_init(): No memory for hash table\n");
111
+		}
112
+
113
+		hash_table_2 = (struct domain_list **)shm_malloc(sizeof(struct domain_list *) * HASH_SIZE);
114
+		if (hash_table_2 == 0) {
115
+			LOG(L_ERR, "domain: mod_init(): No memory for hash table\n");
116
+		}
107 117
 		for (i = 0; i < HASH_SIZE; i++) {
108 118
 			hash_table_1[i] = hash_table_2[i] = (struct domain_list *)0;
109 119
 		}
110
-		current_hash_table = hash_table_1;
120
+
121
+		hash_table = (struct domain_list ***)shm_malloc(sizeof(struct domain_list *));
122
+		*hash_table = hash_table_1;
123
+
111 124
 		if (reload_domain_table() == -1) {
112 125
 			LOG(L_CRIT, "domain:mod_init(): Domain table reload failed\n");
113 126
 			return -1;
... ...
@@ -1,4 +1,4 @@
1
-/* domain_mod.h v 0.2 2002/12/27
1
+/* domain_mod.h v 0.2 2003/1/19
2 2
  *
3 3
  * Domain module headers
4 4
  *
... ...
@@ -60,9 +60,9 @@ extern char* domain_domain_col;   /* Domain column name */
60 60
  * Other module variables
61 61
  */
62 62
 extern db_con_t* db_handle; /* Database connection handle */
63
-extern struct domain_list *hash_table_1[HASH_SIZE]; /* Hash table for domains */
64
-extern struct domain_list *hash_table_2[HASH_SIZE]; /* Hash table for domains */
65
-extern struct domain_list **current_hash_table;
63
+extern struct domain_list **hash_table_1; /* Hash table for domains */
64
+extern struct domain_list **hash_table_2; /* Hash table for domains */
65
+extern struct domain_list ***hash_table;  /* Current hash table */
66 66
 
67 67
 
68 68
 #endif /* DOMAIN_MOD_H */
... ...
@@ -1,4 +1,4 @@
1
-/* fifo.c v 0.1 2002/12/28
1
+/* fifo.c v 0.2 2003/1/19
2 2
  *
3 3
  * Domain fifo functions
4 4
  *
... ...
@@ -59,13 +59,13 @@ int reload_domain_table ( void )
59 59
 	VAL_TYPE(vals) = DB_STR;
60 60
 	VAL_NULL(vals) = 0;
61 61
     
62
-	if (db_query(db_handle, NULL, 0, NULL, cols, 0, 1, 0, &res) < 0) {
62
+	if (db_query(db_handle, NULL, NULL, cols, 0, 1, 0, &res) < 0) {
63 63
 		LOG(L_ERR, "reload_domain_table(): Error while querying database\n");
64 64
 		return -1;
65 65
 	}
66 66
 
67 67
 	/* Choose new hash table and free its old contents */
68
-	if (current_hash_table == hash_table_1) {
68
+	if (*hash_table == hash_table_1) {
69 69
 		hash_table_free(hash_table_2);
70 70
 		new_hash_table = hash_table_2;
71 71
 	} else {
... ...
@@ -96,7 +96,7 @@ int reload_domain_table ( void )
96 96
 	}
97 97
 	db_free_query(db_handle, res);
98 98
 
99
-	current_hash_table = new_hash_table;
99
+	*hash_table = new_hash_table;
100 100
 	
101 101
 	return 1;
102 102
 }
... ...
@@ -130,7 +130,7 @@ static int domain_dump ( FILE* pipe, char* response_file )
130 130
 		return -1;
131 131
 	}
132 132
 	fputs( "200 OK\n", reply_file );
133
-	hash_table_print( current_hash_table, reply_file );
133
+	hash_table_print( *hash_table, reply_file );
134 134
 	fclose(reply_file);
135 135
 	return 1;
136 136
 }
... ...
@@ -1,4 +1,4 @@
1
-/* hash.c v 0.1 2002/12/28
1
+/* hash.c v 0.2 2003/1/19
2 2
  *
3 3
  * Hash functions for cached domain table
4 4
  *
... ...
@@ -29,6 +29,7 @@
29 29
 
30 30
 #include "domain_mod.h"
31 31
 #include "../../dprint.h"
32
+#include "../../mem/shm_mem.h"
32 33
 #include <stdlib.h>
33 34
 #include <string.h>
34 35
 #include <stdio.h>
... ...
@@ -53,16 +54,18 @@ int hash_table_install (struct domain_list **hash_table, char *domain)
53 54
 	struct domain_list *np;
54 55
 	unsigned int hash_val;
55 56
 
56
-	np = (struct domain_list *) malloc(sizeof(*np));
57
+	np = (struct domain_list *) shm_malloc(sizeof(*np));
57 58
 	if (np == NULL) {
58 59
 		LOG(L_CRIT, "hash_install(): Cannot allocate memory for table entry\n");
59 60
 		return -1;
60 61
 	}
61
-	np->domain = strdup(domain);
62
+
63
+	np->domain = (char *) shm_malloc(strlen(domain) + 1);
62 64
 	if (np->domain == NULL) {
63 65
 		LOG(L_CRIT, "hash_install(): Cannot allocate memory for domain string\n");
64 66
 		return -1;
65 67
 	}
68
+	(void) strcpy(np->domain, domain);
66 69
 
67 70
 	hash_val = hash(np->domain);
68 71
 	np->next = hash_table[hash_val];
... ...
@@ -77,7 +80,7 @@ int hash_table_lookup (char *domain, int len)
77 80
 {
78 81
 	struct domain_list *np;
79 82
 
80
-	for (np = current_hash_table[hash(domain)]; np != NULL; np = np->next)
83
+	for (np = (*hash_table)[hash(domain)]; np != NULL; np = np->next)
81 84
 		if (strncasecmp(domain, np->domain, len) == 0) return 1;
82 85
 	return -1;
83 86
 
... ...
@@ -109,9 +112,9 @@ void hash_table_free (struct domain_list **hash_table)
109 112
 	for (i = 0; i < HASH_SIZE; i++) {
110 113
 		np = hash_table[i];
111 114
 		while (np) {
112
-			free(np->domain);
115
+			shm_free(np->domain);
113 116
 			next = np->next;
114
-			free(np);
117
+			shm_free(np);
115 118
 			np = next;
116 119
 		}
117 120
 		hash_table[i] = NULL;