RPA Toolkit
f978432444b17cedd9196cfe264add6a93a5a67a
[rpatk.git] / rvm / rvmcodemap.c
1 #include "rvmcodemap.h"
2 #include "rstring.h"
3 #include "rmem.h"
4
5
6 rvm_codemap_t *rvm_codemap_create()
7 {
8         rvm_codemap_t *codemap;
9
10         codemap = (rvm_codemap_t*)r_malloc(sizeof(*codemap));
11         if (!codemap)
12                 return NULL;
13         r_memset(codemap, 0, sizeof(*codemap));
14         codemap->labels = r_array_create(sizeof(rvm_codelabel_t*));
15         codemap->hash = r_hash_create(5, r_hash_strnequal, r_hash_strnhash);
16         return codemap;
17 }
18
19
20 void rvm_codemap_destroy(rvm_codemap_t *codemap)
21 {
22         int i;
23         rvm_codelabel_t *label;
24         int len = codemap->labels->len;
25
26         for (i = 0; i < len; i++) {
27                 label = r_array_index(codemap->labels, i, rvm_codelabel_t*);
28                 r_free(label->name);
29                 r_free(label);
30         }
31
32         r_array_destroy(codemap->labels);
33         r_hash_destroy(codemap->hash);
34         r_free(codemap);
35 }
36
37
38 void rvm_codemap_add(rvm_codemap_t *codemap, const rchar *name, ruint namesize, rulong index)
39 {
40         rvm_codelabel_t *label;
41
42         label = rvm_codemap_lookup(codemap, name, namesize);
43         if (!label) {
44                 label = r_malloc(sizeof(*label));
45                 label->name = r_stringdup(name, namesize);
46                 r_hash_insert(codemap->hash, label->name, label);
47                 r_array_add(codemap->labels, &label);
48         }
49         label->index = index;
50 }
51
52
53 void rvm_codemap_add_str(rvm_codemap_t *codemap, const rchar *name, rulong index)
54 {
55         rvm_codemap_add(codemap, name, r_strlen(name), index);
56 }
57
58
59 void rvm_codemap_invalid_add(rvm_codemap_t *codemap, const rchar *name, ruint namesize)
60 {
61         rvm_codemap_add(codemap, name, namesize, RVM_CODELABEL_INVALID);
62 }
63
64
65 void rvm_codemap_invalid_add_str(rvm_codemap_t *codemap, const rchar *name)
66 {
67         rvm_codemap_invalid_add(codemap, name, r_strlen(name));
68 }
69
70
71 rvm_codelabel_t *rvm_codemap_lookup(rvm_codemap_t *codemap, const rchar *name, ruint namesize)
72 {
73         rstring_t lookupstr = {(char*)name, namesize};
74         return (rvm_codelabel_t*) r_hash_lookup(codemap->hash, &lookupstr);
75 }
76
77
78
79 rvm_codelabel_t *rvm_codemap_lookup_str(rvm_codemap_t *codemap, const rchar *name)
80 {
81         return rvm_codemap_lookup(codemap, name, r_strlen(name));
82 }
83
84