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,10 +16,10 @@
16 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 21
 	int err;
22
-	table->freeval = freeval?1:0;
22
+	//table->freeval = freeval?1:0;
23 23
 	table->alloc = ASMSH_SYMALLOC;
24 24
 	table->syms = malloc(table->alloc*sizeof(*table->syms));
25 25
 	if(!table->syms)
@@ -39,6 +39,7 @@ err:
39 39
 
40 40
 void asmsh_symtable_clean(asmsh_symtable_t *table)
41 41
 {
42
+	/*
42 43
 	if(table->freeval)
43 44
 	{
44 45
 		for(int i=0; i<table->syms_sz; i++)
@@ -46,12 +47,13 @@ void asmsh_symtable_clean(asmsh_symtable_t *table)
46 47
 			free(table->syms[i].val);
47 48
 		}
48 49
 	}
50
+	*/
49 51
 	free(table->syms);
50 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 58
 	int i;
57 59
 	if(strlen(name)>ASMSH_VARNAME_MAX)
@@ -75,11 +77,13 @@ int asmsh_symtable_add(asmsh_symtable_t *table, const char *name, void *val)
75 77
 		if(cmp == 0)
76 78
 		{
77 79
 			//update value
80
+			/*
78 81
 			if(table->freeval)
79 82
 			{
80 83
 				free(table->syms[i].val);
81 84
 			}
82
-			table->syms[i].val = val;
85
+			*/
86
+			table->syms[i].addr = val;
83 87
 			return 0;
84 88
 		}
85 89
 		else if(cmp > 0)
@@ -95,7 +99,7 @@ int asmsh_symtable_add(asmsh_symtable_t *table, const char *name, void *val)
95 99
 
96 100
 	strncpy(table->syms[i].name, name, ASMSH_VARNAME_MAX);
97 101
 	table->syms[i].name[ASMSH_VARNAME_MAX] = '\0';
98
-	table->syms[i].val = val;
102
+	table->syms[i].addr = val;
99 103
 	table->syms_sz++;
100 104
 	return 0;
101 105
 }
@@ -117,7 +121,7 @@ int asmsh_symtable_del(asmsh_symtable_t *table, const char *name)
117 121
 	{
118 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 125
 	if(table->syms_sz > i+1)
122 126
 	{
123 127
 		memmove(&table->syms[i], &table->syms[i+1],

+ 8
- 5
shell_sym.h View File

@@ -21,7 +21,7 @@
21 21
 #include <stdlib.h>
22 22
 #include <string.h>
23 23
 
24
-#define ASMSH_VARNAME_MAX 31
24
+#define ASMSH_VARNAME_MAX 256
25 25
 #define ASMSH_SYMALLOC 64
26 26
 
27 27
 typedef struct asmsh_sym_s asmsh_sym_t;
@@ -30,22 +30,25 @@ typedef struct asmsh_symtable_s asmsh_symtable_t;
30 30
 struct asmsh_sym_s
31 31
 {
32 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 40
 struct asmsh_symtable_s
37 41
 {
38
-	short freeval;
39 42
 	asmsh_sym_t *syms;
40 43
 	size_t syms_sz;
41 44
 	size_t alloc;
42 45
 };
43 46
 
44 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 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 52
 int asmsh_symtable_del(asmsh_symtable_t *table, const char *name);
50 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,7 +11,7 @@
11 11
 START_TEST(init)
12 12
 {
13 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 15
 	asmsh_symtable_clean(&st);
16 16
 }
17 17
 END_TEST
@@ -19,14 +19,14 @@ END_TEST
19 19
 START_TEST(add)
20 20
 {
21 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 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 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 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 30
 	ck_assert_int_eq(st.syms_sz, 3);
31 31
 	asmsh_symtable_clean(&st);
32 32
 }
@@ -36,18 +36,18 @@ START_TEST(del)
36 36
 {
37 37
 
38 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 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 42
 	ck_assert_int_eq(asmsh_symtable_del(&st, "a"), 1);
43 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 47
 	ck_assert_int_eq(asmsh_symtable_del(&st, "d"), 0);
48 48
 	ck_assert_int_eq(asmsh_symtable_del(&st, "b"), 1);
49 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 51
 	ck_assert_int_eq(asmsh_symtable_del(&st, "a"), 1);
52 52
 	ck_assert_int_eq(asmsh_symtable_del(&st, "a"), 0);
53 53
 	asmsh_symtable_clean(&st);
@@ -59,16 +59,16 @@ START_TEST(get)
59 59
 {
60 60
 	asmsh_symtable_t st;
61 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 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 65
 	s = asmsh_symtable_get(&st, "a");
66 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 69
 	s = asmsh_symtable_get(&st, "a");
70 70
 	ck_assert_ptr_nonnull(s);
71
-	ck_assert_ptr_eq(s->val, (void*)42);
71
+	ck_assert_int_eq(s->addr, 42);
72 72
 	asmsh_symtable_clean(&st);
73 73
 }
74 74
 END_TEST
@@ -76,12 +76,12 @@ END_TEST
76 76
 START_TEST(_realloc)
77 77
 {
78 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 80
 	for(int i=0; i<ASMSH_SYMALLOC*3; i++)
81 81
 	{
82 82
 		char buf[16];
83 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 86
 	for(int i=0; i<ASMSH_SYMALLOC*3; i++)
87 87
 	{

Loading…
Cancel
Save