BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file NCDMethodIndex.h
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #ifndef BADVPN_NCDMETHODINDEX_H
31 #define BADVPN_NCDMETHODINDEX_H
32  
33 #include <misc/debug.h>
34 #include <structure/CHash.h>
35 #include <ncd/NCDModule.h>
36 #include <ncd/NCDStringIndex.h>
37  
38 #define NCDMETHODINDEX_NUM_EXPECTED_METHOD_NAMES 64
39 #define NCDMETHODINDEX_NUM_EXPECTED_ENTRIES 64
40  
41 struct NCDMethodIndex__method_name {
42 char *method_name;
43 int first_entry;
44 int hash_next;
45 };
46  
47 struct NCDMethodIndex__entry {
48 NCD_string_id_t obj_type;
49 const struct NCDInterpModule *module;
50 int next;
51 };
52  
53 typedef struct NCDMethodIndex__method_name NCDMethodIndex__hashentry;
54 typedef const char *NCDMethodIndex__hashkey;
55 typedef struct NCDMethodIndex__method_name *NCDMethodIndex__hasharg;
56  
57 #include "NCDMethodIndex_hash.h"
58 #include <structure/CHash_decl.h>
59  
60 /**
61 * The method index associates (object_type, method_name) pairs to pointers
62 * to corresponding \link NCDInterpModule structures (whose type strings would
63 * be "object_type::method_name").
64 * More precisely, the method names are represented as indices into an
65 * internal array, which allows very efficient lookup when the method names
66 * are known in advance, but not the object types.
67 */
68 typedef struct {
69 struct NCDMethodIndex__method_name *names;
70 struct NCDMethodIndex__entry *entries;
71 int names_capacity;
72 int entries_capacity;
73 int num_names;
74 int num_entries;
75 NCDMethodIndex__Hash hash;
76 NCDStringIndex *string_index;
77 } NCDMethodIndex;
78  
79 /**
80 * Initializes the method index.
81 *
82 * @return 1 on success, 0 on failure
83 */
84 int NCDMethodIndex_Init (NCDMethodIndex *o, NCDStringIndex *string_index) WARN_UNUSED;
85  
86 /**
87 * Frees the method index.
88 */
89 void NCDMethodIndex_Free (NCDMethodIndex *o);
90  
91 /**
92 * Adds a method to the index.
93 * Duplicate methods will not be detected here.
94 *
95 * @param obj_type object type of method, e.g. "cat" in "cat::meow".
96 * Must not be NULL. Does not have to be null-terminated.
97 * @param obj_type_len number of characters in obj_type
98 * @param method_name name of method, e.g. "meow" in "cat::meow".
99 * Must not be NULL.
100 * @param module pointer to module structure. Must not be NULL.
101 * @return on success, a non-negative identifier; on failure, -1
102 */
103 int NCDMethodIndex_AddMethod (NCDMethodIndex *o, const char *obj_type, size_t obj_type_len, const char *method_name, const struct NCDInterpModule *module);
104  
105 /**
106 * Removes a method from the index.
107 *
108 * @param method_name_id method name identifier
109 */
110 void NCDMethodIndex_RemoveMethod (NCDMethodIndex *o, int method_name_id);
111  
112 /**
113 * Obtains an internal integer identifier for a method name. The intention is that
114 * this is stored and later passed to \link NCDMethodIndex_GetMethodModule for
115 * efficient lookup of modules corresponding to methods.
116 *
117 * @param method_name name of method, e.g. "meow" in "cat::meow".
118 * Must not be NULL.
119 * @return non-negative integer on success, -1 on failure
120 */
121 int NCDMethodIndex_GetMethodNameId (NCDMethodIndex *o, const char *method_name);
122  
123 /**
124 * Looks up the module corresponding to a method. The method name is passed as an
125 * identifier obtained from \link NCDMethodIndex_GetMethodNameId.
126 *
127 * @param obj_type object type of method, e.g. "cat" in "cat::meow", as a string
128 * identifier via {@link NCDStringIndex}
129 * @param method_name_id method name identifier. Must have been previously returned
130 * by a successfull call of \link NCDMethodIndex_GetMethodNameId.
131 * @return module pointer, or NULL if no such method exists
132 */
133 const struct NCDInterpModule * NCDMethodIndex_GetMethodModule (NCDMethodIndex *o, NCD_string_id_t obj_type, int method_name_id);
134  
135 #endif