@@ -41,6 +41,7 @@ void unit_mgr_close() {
4141 unit_count = 0 ;
4242 if (units ) {
4343 free (units );
44+ units = NULL ;
4445 }
4546}
4647
@@ -105,7 +106,7 @@ int find_unit(const char *name, char *file) {
105106 * @return the unit handle or -1 on error
106107 */
107108int open_unit (const char * file ) {
108- int h , i ;
109+ int h ;
109110 unit_t u ;
110111 int uid = -1 ;
111112
@@ -161,34 +162,23 @@ int open_unit(const char *file) {
161162 }
162163
163164 // load symbol-table
164- u .symbols = (unit_sym_t * ) malloc (u .hdr .sym_count * sizeof (unit_sym_t ));
165- read (h , u .symbols , u .hdr .sym_count * sizeof (unit_sym_t ));
165+ if (u .hdr .sym_count ) {
166+ u .symbols = (unit_sym_t * )malloc (u .hdr .sym_count * sizeof (unit_sym_t ));
167+ read (h , u .symbols , u .hdr .sym_count * sizeof (unit_sym_t ));
168+ }
166169
167170 // setup the rest
168171 strcpy (u .name , unitname );
169172 u .status = unit_loaded ;
170173
171174 // add unit
172- if (unit_count == 0 ) {
173- // this is the first unit
174- uid = unit_count ;
175- unit_count ++ ;
176- units = (unit_t * ) malloc (unit_count * sizeof (unit_t ));
177- } else {
178- // search for an empty entry
179- for (i = 0 ; i < unit_count ; i ++ ) {
180- if (units [i ].status == unit_undefined ) {
181- uid = i ;
182- break ;
183- }
184- }
175+ uid = unit_count ;
176+ unit_count ++ ;
185177
186- // resize the table
187- if (uid == -1 ) {
188- uid = unit_count ;
189- unit_count ++ ;
190- units = (unit_t * ) realloc (units , unit_count * sizeof (unit_t ));
191- }
178+ if (units == NULL ) {
179+ units = (unit_t * )malloc (unit_count * sizeof (unit_t ));
180+ } else {
181+ units = (unit_t * )realloc (units , unit_count * sizeof (unit_t ));
192182 }
193183
194184 // copy unit's data
@@ -207,9 +197,7 @@ int open_unit(const char *file) {
207197 */
208198int close_unit (int uid ) {
209199 if (uid >= 0 ) {
210- unit_t * u ;
211-
212- u = & units [uid ];
200+ unit_t * u = & units [uid ];
213201 if (u -> status == unit_loaded ) {
214202 u -> status = unit_undefined ;
215203 free (u -> symbols );
@@ -233,11 +221,8 @@ int import_unit(int uid) {
233221 char buf [SB_KEYWORD_SIZE + 1 ];int i ;
234222
235223 if (uid >= 0 ) {
236- unit_t * u ;
237-
238- u = & units [uid ];
224+ unit_t * u = & units [uid ];
239225 if (u -> status == unit_loaded ) {
240-
241226 for (i = 0 ; i < u -> hdr .sym_count ; i ++ ) {
242227 // build the name
243228 // with any path component removed from the name
@@ -246,13 +231,13 @@ int import_unit(int uid) {
246231 dir_sep ? dir_sep + 1 : u -> hdr .base , u -> symbols [i ].symbol );
247232
248233 switch (u -> symbols [i ].type ) {
249- case stt_function :
234+ case stt_function :
250235 comp_add_external_func (buf , uid | UID_UNIT_BIT );
251236 break ;
252- case stt_procedure :
237+ case stt_procedure :
253238 comp_add_external_proc (buf , uid | UID_UNIT_BIT );
254239 break ;
255- case stt_variable :
240+ case stt_variable :
256241 comp_add_external_var (buf , uid | UID_UNIT_BIT );
257242 break ;
258243 };
@@ -272,7 +257,7 @@ int import_unit(int uid) {
272257/**
273258 * execute a call to a unit
274259 */
275- int unit_exec (int lib_id , int index , var_t * ret ) {
260+ int unit_exec (int lib_id , int index , var_t * ret ) {
276261 unit_sym_t * us ; // unit's symbol data
277262 bc_symbol_rec_t * ps ; // program's symbol data
278263 int my_tid ;
@@ -282,15 +267,12 @@ int unit_exec(int lib_id, int index, var_t * ret) {
282267 ps = & prog_symtable [index ];
283268 us = & (taskinfo (ps -> task_id )-> sbe .exec .exptable [ps -> exp_idx ]);
284269
285- //
286270 switch (ps -> type ) {
287271 case stt_variable :
288272 break ;
289273 case stt_procedure :
290274 exec_sync_variables (1 );
291-
292275 cmd_call_unit_udp (kwPROC , ps -> task_id , us -> address , 0 );
293-
294276 activate_task (ps -> task_id );
295277 if (prog_error ) {
296278 gsb_last_error = prog_error ;
@@ -309,9 +291,7 @@ int unit_exec(int lib_id, int index, var_t * ret) {
309291
310292 case stt_function :
311293 exec_sync_variables (1 );
312-
313294 cmd_call_unit_udp (kwFUNC , ps -> task_id , us -> address , us -> vid );
314-
315295 activate_task (ps -> task_id );
316296 if (prog_error ) {
317297 gsb_last_error = prog_error ;
0 commit comments