Mercurial > audlegacy-plugins
annotate src/paranormal/libcalc/dict.c @ 1181:2f3397e53b05 trunk
[svn] - memory cleanups, purify-assisted.
author | nenolod |
---|---|
date | Sat, 09 Jun 2007 06:16:03 -0700 |
parents | e9b2d21cf078 |
children | 6d749f573b3b |
rev | line source |
---|---|
282 | 1 /* dict.c -- symbol dictionary structures |
2 * | |
3 * Copyright (C) 2001 Janusz Gregorczyk <jgregor@kki.net.pl> | |
4 * | |
5 * This file is part of xvs. | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
20 */ | |
21 | |
22 #define V_SPACE_INIT 8 | |
23 #define V_SPACE_INCR 8 | |
24 | |
25 #include <glib.h> | |
26 #include <string.h> | |
27 | |
28 #include "dict.h" | |
29 | |
1175
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
30 static int global_dict_initialized = 0; |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
31 static symbol_dict_t global_dict; |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
32 |
282 | 33 static void more_variables (symbol_dict_t *dict) { |
34 var_t *new_var; | |
35 | |
36 dict->v_space += V_SPACE_INCR; | |
37 | |
1181 | 38 new_var = g_new(var_t, dict->v_space + 1); |
282 | 39 memcpy (new_var, dict->variables, dict->v_count * sizeof(var_t)); |
40 g_free (dict->variables); | |
41 | |
42 dict->variables = new_var; | |
43 } | |
44 | |
1175
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
45 static int dict_define_variable (symbol_dict_t *dict, const char *name) { |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
46 var_t *var; |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
47 |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
48 if (dict->v_count >= dict->v_space) |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
49 more_variables (dict); |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
50 |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
51 var = &dict->variables[dict->v_count]; |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
52 |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
53 var->value = 0.0; |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
54 var->name = g_strdup (name); |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
55 |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
56 return dict->v_count++; |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
57 } |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
58 |
282 | 59 symbol_dict_t *dict_new (void) { |
60 symbol_dict_t *dict; | |
1175
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
61 |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
62 if (global_dict_initialized != 1) { |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
63 int i; |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
64 |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
65 global_dict.v_count = 0; |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
66 global_dict.v_space = V_SPACE_INIT; |
1181 | 67 global_dict.variables = (var_t *) g_new(var_t, global_dict.v_space + 1); |
1175
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
68 global_dict_initialized = 1; |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
69 |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
70 for (i = 0; i < 100; i++) { |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
71 gchar tmpbuf[40]; |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
72 snprintf(tmpbuf, 40, "global_reg%d", i); |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
73 dict_define_variable(&global_dict, tmpbuf); |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
74 } |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
75 } |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
76 |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
77 dict = g_new(symbol_dict_t, 1); |
282 | 78 |
79 /* Allocate space for variables. */ | |
80 dict->v_count = 0; | |
81 dict->v_space = V_SPACE_INIT; | |
1181 | 82 dict->variables = (var_t *) g_new (var_t, dict->v_space + 1); |
282 | 83 |
84 return dict; | |
85 } | |
86 | |
87 void dict_free (symbol_dict_t *dict) { | |
88 int i; | |
89 | |
90 if (!dict) | |
91 return; | |
92 | |
93 /* Free memory used by variables. */ | |
94 for (i = 0; i < dict->v_count; i++) | |
95 g_free (dict->variables[i].name); | |
96 g_free (dict->variables); | |
97 | |
98 g_free (dict); | |
99 } | |
100 | |
101 int dict_lookup (symbol_dict_t *dict, const char *name) { | |
102 int i; | |
103 | |
1175
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
104 for (i = 0; i < global_dict.v_count; i++) { |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
105 if (strcmp (global_dict.variables[i].name, name) == 0) |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
106 return -i; |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
107 } |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
108 |
282 | 109 for (i = 0; i < dict->v_count; i++) { |
110 if (strcmp (dict->variables[i].name, name) == 0) | |
111 return i; | |
112 } | |
113 | |
114 /* Not defined -- define a new variable. */ | |
115 return dict_define_variable (dict, name); | |
116 } | |
117 | |
118 double *dict_variable (symbol_dict_t *dict, const char *var_name) { | |
119 int id = dict_lookup (dict, var_name); | |
1175
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
120 |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
121 /* global variables are presented as negative offset. negating |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
122 * a negative number results in a positive offset. --nenolod |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
123 */ |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
124 if (id < 0) |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
125 return &global_dict.variables[-id].value; |
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
282
diff
changeset
|
126 |
282 | 127 return &dict->variables[id].value; |
128 } |