Browse Source

Simplify the symtable implementation

Now it is only dedicated to address storage !
If needed the code to free an allocated pointer is commented.
Yann Weber 1 year ago
parent
commit
32ffdfbd8a
3 changed files with 37 additions and 30 deletions
  1. 10
    6
      shell_sym.c
  2. 8
    5
      shell_sym.h
  3. 19
    19
      tests/tests_shell_sym.c

+ 10
- 6
shell_sym.c View File

16
 #include "shell_sym.h"
16
 #include "shell_sym.h"
17
 
17
 
18
 
18
 
19
-int asmsh_symtable_init(asmsh_symtable_t *table, short freeval)
19
+int asmsh_symtable_init(asmsh_symtable_t *table)
20
 {
20
 {
21
 	int err;
21
 	int err;
22
-	table->freeval = freeval?1:0;
22
+	//table->freeval = freeval?1:0;
23
 	table->alloc = ASMSH_SYMALLOC;
23
 	table->alloc = ASMSH_SYMALLOC;
24
 	table->syms = malloc(table->alloc*sizeof(*table->syms));
24
 	table->syms = malloc(table->alloc*sizeof(*table->syms));
25
 	if(!table->syms)
25
 	if(!table->syms)
39
 
39
 
40
 void asmsh_symtable_clean(asmsh_symtable_t *table)
40
 void asmsh_symtable_clean(asmsh_symtable_t *table)
41
 {
41
 {
42
+	/*
42
 	if(table->freeval)
43
 	if(table->freeval)
43
 	{
44
 	{
44
 		for(int i=0; i<table->syms_sz; i++)
45
 		for(int i=0; i<table->syms_sz; i++)
46
 			free(table->syms[i].val);
47
 			free(table->syms[i].val);
47
 		}
48
 		}
48
 	}
49
 	}
50
+	*/
49
 	free(table->syms);
51
 	free(table->syms);
50
 	bzero(table, sizeof(*table));
52
 	bzero(table, sizeof(*table));
51
 }
53
 }
52
 
54
 
53
 
55
 
54
-int asmsh_symtable_add(asmsh_symtable_t *table, const char *name, void *val)
56
+int asmsh_symtable_add(asmsh_symtable_t *table, const char *name, unsigned long val)
55
 {
57
 {
56
 	int i;
58
 	int i;
57
 	if(strlen(name)>ASMSH_VARNAME_MAX)
59
 	if(strlen(name)>ASMSH_VARNAME_MAX)
75
 		if(cmp == 0)
77
 		if(cmp == 0)
76
 		{
78
 		{
77
 			//update value
79
 			//update value
80
+			/*
78
 			if(table->freeval)
81
 			if(table->freeval)
79
 			{
82
 			{
80
 				free(table->syms[i].val);
83
 				free(table->syms[i].val);
81
 			}
84
 			}
82
-			table->syms[i].val = val;
85
+			*/
86
+			table->syms[i].addr = val;
83
 			return 0;
87
 			return 0;
84
 		}
88
 		}
85
 		else if(cmp > 0)
89
 		else if(cmp > 0)
95
 
99
 
96
 	strncpy(table->syms[i].name, name, ASMSH_VARNAME_MAX);
100
 	strncpy(table->syms[i].name, name, ASMSH_VARNAME_MAX);
97
 	table->syms[i].name[ASMSH_VARNAME_MAX] = '\0';
101
 	table->syms[i].name[ASMSH_VARNAME_MAX] = '\0';
98
-	table->syms[i].val = val;
102
+	table->syms[i].addr = val;
99
 	table->syms_sz++;
103
 	table->syms_sz++;
100
 	return 0;
104
 	return 0;
101
 }
105
 }
117
 	{
121
 	{
118
 		return 0; // not found
122
 		return 0; // not found
119
 	}
123
 	}
120
-	if(table->freeval) { free(table->syms[i].val); }
124
+	//if(table->freeval) { free(table->syms[i].val); }
121
 	if(table->syms_sz > i+1)
125
 	if(table->syms_sz > i+1)
122
 	{
126
 	{
123
 		memmove(&table->syms[i], &table->syms[i+1],
127
 		memmove(&table->syms[i], &table->syms[i+1],

+ 8
- 5
shell_sym.h View File

21
 #include <stdlib.h>
21
 #include <stdlib.h>
22
 #include <string.h>
22
 #include <string.h>
23
 
23
 
24
-#define ASMSH_VARNAME_MAX 31
24
+#define ASMSH_VARNAME_MAX 256
25
 #define ASMSH_SYMALLOC 64
25
 #define ASMSH_SYMALLOC 64
26
 
26
 
27
 typedef struct asmsh_sym_s asmsh_sym_t;
27
 typedef struct asmsh_sym_s asmsh_sym_t;
30
 struct asmsh_sym_s
30
 struct asmsh_sym_s
31
 {
31
 {
32
 	char name[ASMSH_VARNAME_MAX + 1];
32
 	char name[ASMSH_VARNAME_MAX + 1];
33
-	void *val;
33
+	unsigned long addr;
34
 };
34
 };
35
 
35
 
36
+/** Stores the list of symbols associated with their address/value
37
+ *
38
+ * @note Symboles are ordered by name, ascending
39
+ */
36
 struct asmsh_symtable_s
40
 struct asmsh_symtable_s
37
 {
41
 {
38
-	short freeval;
39
 	asmsh_sym_t *syms;
42
 	asmsh_sym_t *syms;
40
 	size_t syms_sz;
43
 	size_t syms_sz;
41
 	size_t alloc;
44
 	size_t alloc;
42
 };
45
 };
43
 
46
 
44
 /** if freeval value given to add are freed on clean */
47
 /** if freeval value given to add are freed on clean */
45
-int asmsh_symtable_init(asmsh_symtable_t *table, short freeval);
48
+int asmsh_symtable_init(asmsh_symtable_t *table);
46
 void asmsh_symtable_clean(asmsh_symtable_t *table);
49
 void asmsh_symtable_clean(asmsh_symtable_t *table);
47
 
50
 
48
-int asmsh_symtable_add(asmsh_symtable_t *table, const char *name, void *val);
51
+int asmsh_symtable_add(asmsh_symtable_t *table, const char *name, unsigned long val);
49
 int asmsh_symtable_del(asmsh_symtable_t *table, const char *name);
52
 int asmsh_symtable_del(asmsh_symtable_t *table, const char *name);
50
 const asmsh_sym_t *asmsh_symtable_get(asmsh_symtable_t *table, const char *name);
53
 const asmsh_sym_t *asmsh_symtable_get(asmsh_symtable_t *table, const char *name);
51
 
54
 

+ 19
- 19
tests/tests_shell_sym.c View File

11
 START_TEST(init)
11
 START_TEST(init)
12
 {
12
 {
13
 	asmsh_symtable_t st;
13
 	asmsh_symtable_t st;
14
-	ck_assert_int_eq(asmsh_symtable_init(&st,0), 0);
14
+	ck_assert_int_eq(asmsh_symtable_init(&st), 0);
15
 	asmsh_symtable_clean(&st);
15
 	asmsh_symtable_clean(&st);
16
 }
16
 }
17
 END_TEST
17
 END_TEST
19
 START_TEST(add)
19
 START_TEST(add)
20
 {
20
 {
21
 	asmsh_symtable_t st;
21
 	asmsh_symtable_t st;
22
-	ck_assert_int_eq(asmsh_symtable_init(&st,0), 0);
23
-	ck_assert_int_eq(asmsh_symtable_add(&st, "b", NULL), 0);
22
+	ck_assert_int_eq(asmsh_symtable_init(&st), 0);
23
+	ck_assert_int_eq(asmsh_symtable_add(&st, "b", 0), 0);
24
 	ck_assert_int_eq(st.syms_sz, 1);
24
 	ck_assert_int_eq(st.syms_sz, 1);
25
-	ck_assert_int_eq(asmsh_symtable_add(&st, "a", NULL), 0);
25
+	ck_assert_int_eq(asmsh_symtable_add(&st, "a", 0), 0);
26
 	ck_assert_int_eq(st.syms_sz, 2);
26
 	ck_assert_int_eq(st.syms_sz, 2);
27
-	ck_assert_int_eq(asmsh_symtable_add(&st, "a", NULL), 0);
27
+	ck_assert_int_eq(asmsh_symtable_add(&st, "a", 0), 0);
28
 	ck_assert_int_eq(st.syms_sz, 2);
28
 	ck_assert_int_eq(st.syms_sz, 2);
29
-	ck_assert_int_eq(asmsh_symtable_add(&st, "ab", NULL), 0);
29
+	ck_assert_int_eq(asmsh_symtable_add(&st, "ab", 0), 0);
30
 	ck_assert_int_eq(st.syms_sz, 3);
30
 	ck_assert_int_eq(st.syms_sz, 3);
31
 	asmsh_symtable_clean(&st);
31
 	asmsh_symtable_clean(&st);
32
 }
32
 }
36
 {
36
 {
37
 
37
 
38
 	asmsh_symtable_t st;
38
 	asmsh_symtable_t st;
39
-	ck_assert_int_eq(asmsh_symtable_init(&st,0), 0);
39
+	ck_assert_int_eq(asmsh_symtable_init(&st), 0);
40
 	ck_assert_int_eq(asmsh_symtable_del(&st, "a"), 0);
40
 	ck_assert_int_eq(asmsh_symtable_del(&st, "a"), 0);
41
-	ck_assert_int_eq(asmsh_symtable_add(&st, "a", NULL), 0);
41
+	ck_assert_int_eq(asmsh_symtable_add(&st, "a", 0), 0);
42
 	ck_assert_int_eq(asmsh_symtable_del(&st, "a"), 1);
42
 	ck_assert_int_eq(asmsh_symtable_del(&st, "a"), 1);
43
 	ck_assert_int_eq(asmsh_symtable_del(&st, "a"), 0);
43
 	ck_assert_int_eq(asmsh_symtable_del(&st, "a"), 0);
44
-	ck_assert_int_eq(asmsh_symtable_add(&st, "a", NULL), 0);
45
-	ck_assert_int_eq(asmsh_symtable_add(&st, "b", NULL), 0);
46
-	ck_assert_int_eq(asmsh_symtable_add(&st, "c", NULL), 0);
44
+	ck_assert_int_eq(asmsh_symtable_add(&st, "a", 0), 0);
45
+	ck_assert_int_eq(asmsh_symtable_add(&st, "b", 0), 0);
46
+	ck_assert_int_eq(asmsh_symtable_add(&st, "c", 0), 0);
47
 	ck_assert_int_eq(asmsh_symtable_del(&st, "d"), 0);
47
 	ck_assert_int_eq(asmsh_symtable_del(&st, "d"), 0);
48
 	ck_assert_int_eq(asmsh_symtable_del(&st, "b"), 1);
48
 	ck_assert_int_eq(asmsh_symtable_del(&st, "b"), 1);
49
 	ck_assert_int_eq(asmsh_symtable_del(&st, "b"), 0);
49
 	ck_assert_int_eq(asmsh_symtable_del(&st, "b"), 0);
50
-	ck_assert_int_eq(asmsh_symtable_add(&st, "b", NULL), 0);
50
+	ck_assert_int_eq(asmsh_symtable_add(&st, "b", 0), 0);
51
 	ck_assert_int_eq(asmsh_symtable_del(&st, "a"), 1);
51
 	ck_assert_int_eq(asmsh_symtable_del(&st, "a"), 1);
52
 	ck_assert_int_eq(asmsh_symtable_del(&st, "a"), 0);
52
 	ck_assert_int_eq(asmsh_symtable_del(&st, "a"), 0);
53
 	asmsh_symtable_clean(&st);
53
 	asmsh_symtable_clean(&st);
59
 {
59
 {
60
 	asmsh_symtable_t st;
60
 	asmsh_symtable_t st;
61
 	const asmsh_sym_t *s;
61
 	const asmsh_sym_t *s;
62
-	ck_assert_int_eq(asmsh_symtable_init(&st,0), 0);
62
+	ck_assert_int_eq(asmsh_symtable_init(&st), 0);
63
 	ck_assert_ptr_null(asmsh_symtable_get(&st, "a"));
63
 	ck_assert_ptr_null(asmsh_symtable_get(&st, "a"));
64
-	ck_assert_int_eq(asmsh_symtable_add(&st, "a", NULL), 0);
64
+	ck_assert_int_eq(asmsh_symtable_add(&st, "a", 0), 0);
65
 	s = asmsh_symtable_get(&st, "a");
65
 	s = asmsh_symtable_get(&st, "a");
66
 	ck_assert_ptr_nonnull(s);
66
 	ck_assert_ptr_nonnull(s);
67
-	ck_assert_ptr_null(s->val);
68
-	ck_assert_int_eq(asmsh_symtable_add(&st, "a", (void*)42), 0);
67
+	ck_assert_int_eq(s->addr, 0);
68
+	ck_assert_int_eq(asmsh_symtable_add(&st, "a", 42), 0);
69
 	s = asmsh_symtable_get(&st, "a");
69
 	s = asmsh_symtable_get(&st, "a");
70
 	ck_assert_ptr_nonnull(s);
70
 	ck_assert_ptr_nonnull(s);
71
-	ck_assert_ptr_eq(s->val, (void*)42);
71
+	ck_assert_int_eq(s->addr, 42);
72
 	asmsh_symtable_clean(&st);
72
 	asmsh_symtable_clean(&st);
73
 }
73
 }
74
 END_TEST
74
 END_TEST
76
 START_TEST(_realloc)
76
 START_TEST(_realloc)
77
 {
77
 {
78
 	asmsh_symtable_t st;
78
 	asmsh_symtable_t st;
79
-	ck_assert_int_eq(asmsh_symtable_init(&st,0), 0);
79
+	ck_assert_int_eq(asmsh_symtable_init(&st), 0);
80
 	for(int i=0; i<ASMSH_SYMALLOC*3; i++)
80
 	for(int i=0; i<ASMSH_SYMALLOC*3; i++)
81
 	{
81
 	{
82
 		char buf[16];
82
 		char buf[16];
83
 		snprintf(buf, 15, "a%d", i);
83
 		snprintf(buf, 15, "a%d", i);
84
-		ck_assert_int_eq(asmsh_symtable_add(&st, buf, NULL), 0);
84
+		ck_assert_int_eq(asmsh_symtable_add(&st, buf, 0), 0);
85
 	}
85
 	}
86
 	for(int i=0; i<ASMSH_SYMALLOC*3; i++)
86
 	for(int i=0; i<ASMSH_SYMALLOC*3; i++)
87
 	{
87
 	{

Loading…
Cancel
Save