Multi-ApplicationOnlineProfiling  2.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Trie.h
Go to the documentation of this file.
1 #ifndef TRIE_H
2 #define TRIE_H
3 
4 #include <stdlib.h>
5 
6 #include "Spinlock.h"
7 
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 
14 struct Trie_node
15 {
16  void *payload;
17 
18  size_t size;
20 
21  /* From ascii 32 to ascii 126 */
22  struct Trie_node *children[94];
23 };
24 
25 
27 struct Trie_node * Trie_node_init_noalloc( void *payload, size_t size );
28 void Trie_node_release( struct Trie_node * tn );
29 
30 int Trie_node_set( struct Trie_node *tn, void *payload, size_t size );
31 void Trie_node_free_payload( struct Trie_node * tn );
32 
33 
34 struct Trie
35 {
36  struct Trie_node *root;
37 };
38 
39 static inline struct Trie_node * Trie_acquire_node( struct Trie *tr, char *key )
40 {
41  struct Trie_node *current = tr->root;
42  struct Trie_node *prev = NULL;
43 
44  if(!key)
45  return NULL;
46 
47  do
48  {
49  /* From ascii 32 to ascii 126 */
50  if( *key < 32 || 126 < *key )
51  {
52  return NULL;
53  }
54 
55  MALP_Spinlock_lock(&current->lock);
56 
57  int val = *key - 32;
58 
59  if( !current->children[ val ] )
60  {
61  current->children[ val ] = Trie_node_init_empty();
62  }
63 
64  prev = current;
65  current = current->children[ val ];
66 
67  MALP_Spinlock_unlock(&prev->lock);
68 
69  key++;
70  }while( *key );
71 
72  return current;
73 }
74 
75 static inline void Trie_relax_node( struct Trie_node *tn )
76 {
77  if( tn )
79 }
80 
81 
82 int Trie_init( struct Trie *tr );
83 int Trie_release( struct Trie *tr );
84 
85 
86 int Trie_set( struct Trie *tr, char *key, void *payload, size_t size );
87 
88 static inline struct Trie_node * Trie_get( struct Trie *tr, char *key )
89 {
90  struct Trie_node *ret = Trie_acquire_node( tr, key );
91 
92  if( !ret->payload )
93  {
94  Trie_relax_node( ret );
95  return NULL;
96  }
97 
98  return ret;
99 }
100 
101 
102 int Trie_set_no_alloc( struct Trie *tr, char *key, void *payload, size_t size );
103 void Trie_delete( struct Trie *tr, char *key );
104 
105 
106 #ifdef __cplusplus
107 }
108 #endif
109 
110 #endif
static struct Trie_node * Trie_acquire_node(struct Trie *tr, char *key)
Definition: Trie.h:39
struct Trie_node * Trie_node_init_noalloc(void *payload, size_t size)
Definition: Trie.c:21
int Trie_release(struct Trie *tr)
Definition: Trie.c:89
struct Trie_node * root
Definition: Trie.h:36
int Trie_set_no_alloc(struct Trie *tr, char *key, void *payload, size_t size)
Definition: Trie.c:112
size_t size
Definition: Trie.h:18
int MALP_Spinlock_unlock(MALP_Spinlock *mutex)
Unlocks the given MALP_Spinlock.
Definition: Spinlock.c:41
struct Trie_node * children[94]
Definition: Trie.h:22
static void Trie_relax_node(struct Trie_node *tn)
Definition: Trie.h:75
int Trie_set(struct Trie *tr, char *key, void *payload, size_t size)
Definition: Trie.c:97
int Trie_node_set(struct Trie_node *tn, void *payload, size_t size)
Definition: Trie.c:34
void * payload
Definition: Trie.h:16
Definition: Trie.h:14
void Trie_node_release(struct Trie_node *tn)
Definition: Trie.c:64
int MALP_Spinlock_lock(MALP_Spinlock *mutex)
Locks the given MALP_Spinlock.
Definition: Spinlock.c:29
void Trie_delete(struct Trie *tr, char *key)
Definition: Trie.c:175
volatile unsigned int MALP_Spinlock
The type of spinlocks in MALP.
Definition: Spinlock.h:63
MALP_Spinlock lock
Definition: Trie.h:19
static struct Trie_node * Trie_get(struct Trie *tr, char *key)
Definition: Trie.h:88
int Trie_init(struct Trie *tr)
Definition: Trie.c:83
struct Trie_node * Trie_node_init_empty()
Definition: Trie.c:6
void Trie_node_free_payload(struct Trie_node *tn)
Definition: Trie.c:53
Definition: Trie.h:34