Cleaned up a lot of printing.
[distributedratelimiting.git] / drl / util.h
1 /* See the DRL-LICENSE file for this file's software license. */
2
3 #ifndef _UTIL_H_
4 #define _UTIL_H_
5
6 #define _XOPEN_SOURCE 600 /* for pthread_rwlock_t */
7
8 /*
9  * Jenkins hash support
10  */
11 typedef uint8_t u8;
12 typedef uint16_t u16;
13 typedef uint32_t u32;
14 #include <linux/jhash.h>
15
16 #define MAP_KEY_SIZE 4 /* all 4 bytes for now */
17 #define GENERIC_HASH_SIZE 16 /* must be powers of 2 */
18
19 enum bool {false=0,true};
20
21 struct map{
22     struct map_entry *table[GENERIC_HASH_SIZE];
23     int iterator_row;
24     struct map_entry *iterator;
25     int size;
26 };
27
28 struct map_entry {
29     struct map_entry *nxt;
30     char *key;
31     void *value;
32 };
33
34 /* typedef's */
35 typedef struct map *map_handle;
36
37 /* function prototypes */
38
39 char* get_local_ip();
40
41 void ip_from_bytes(uint32_t addr, char *buf);
42 uint32_t myrand();
43 int myrand_boolean();
44 double myrand_double();
45 int my_lg(int x);
46
47 /* hash map interface */
48 void init_hashing(void);
49 map_handle allocate_map(void);
50 void free_map(map_handle map, int dealloc);
51 void** map_to_array(map_handle map, int *length);
52 void* map_search(map_handle map,void *key, int keylen);
53 void map_reset_iterate(map_handle map);
54 void* map_next(map_handle table);
55 void* map_remove(map_handle map,void *key, int keylen);
56 void map_insert(map_handle map, void *key, int keylen, void *value);
57 int map_size(map_handle map);
58
59 #endif