• R/O
  • SSH
  • HTTPS

avrdude: Commit


Commit MetaInfo

Revision1470 (tree)
Time2020-02-17 07:03:01
Authorgottfried

Log Message

string_list and number_list from LISTID -> std::vector

Change Summary

Incremental Difference

--- trunk/avrpart.cpp (revision 1469)
+++ trunk/avrpart.cpp (revision 1470)
@@ -529,7 +529,7 @@
529529 }
530530 }
531531
532-AVRPART *locate_part(std::vector<AVRPART> &parts, char *partdesc)
532+AVRPART *locate_part(std::vector<AVRPART> &parts, const char *partdesc)
533533 {
534534 for(std::vector<AVRPART>::iterator it = parts.begin(); it != parts.end(); ++it)
535535 {
--- trunk/config.cpp (revision 1469)
+++ trunk/config.cpp (revision 1470)
@@ -29,6 +29,8 @@
2929 #include "libavrdude.h"
3030 #include "config.h"
3131
32+#include <cassert>
33+
3234 //#ifdef __cplusplus
3335 //extern "C" {
3436 //#endif
@@ -54,8 +56,8 @@
5456 char string_buf[MAX_STR_CONST];
5557 char *string_buf_ptr;
5658
57-LISTID string_list;
58-LISTID number_list;
59+std::vector<TOKEN*> string_list;
60+std::vector<TOKEN*> number_list;
5961 PROGRAMMER * current_prog;
6062 AVRPART * current_part;
6163 AVRMEM * current_mem;
@@ -69,17 +71,13 @@
6971
7072 #define DEBUG 0
7173
72-void cleanup_config(void)
74+void cleanup_config()
7375 {
7476 avr_free_parts();
75- ldestroy_cb(string_list, (void(*)(void*))free_token);
76- ldestroy_cb(number_list, (void(*)(void*))free_token);
7777 }
7878
7979 int init_config(void)
8080 {
81- string_list = lcreat(NULL, 0);
82- number_list = lcreat(NULL, 0);
8381 current_prog = NULL;
8482 current_part = NULL;
8583 current_mem = NULL;
@@ -136,33 +134,21 @@
136134 {
137135 TOKEN * tkn;
138136
139- tkn = (TOKEN *)malloc(sizeof(TOKEN));
140- if (tkn == NULL) {
137+ tkn = new TOKEN();
138+ if(tkn == NULL)
139+ {
141140 yyerror("new_token(): out of memory");
142141 return NULL;
143142 }
144143
145- memset(tkn, 0, sizeof(TOKEN));
146-
147144 tkn->primary = primary;
148145
149146 return tkn;
150147 }
151148
152-
153-void free_token(TOKEN * tkn)
149+void free_token(TOKEN *tkn)
154150 {
155- if (tkn) {
156- switch (tkn->value.type) {
157- case V_STR:
158- if (tkn->value.string)
159- free(tkn->value.string);
160- tkn->value.string = NULL;
161- break;
162- }
163-
164- free(tkn);
165- }
151+ delete tkn;
166152 }
167153
168154
@@ -179,6 +165,14 @@
179165 va_end(ap);
180166 }
181167
168+void free_tokenlist(std::vector<TOKEN*> &tokenlist)
169+{
170+ for(std::vector<TOKEN*>::iterator it = tokenlist.begin(); it != tokenlist.end(); ++it)
171+ {
172+ free_token(*it);
173+ }
174+ tokenlist.clear();
175+}
182176
183177
184178 TOKEN * number(char * text)
@@ -238,24 +232,15 @@
238232 }
239233
240234
241-TOKEN * string(char * text)
235+TOKEN *string(const char *text)
242236 {
243- struct token_t * tkn;
244- int len;
245-
246- tkn = new_token(TKN_STRING);
247- if (tkn == NULL) {
248- return NULL; /* yyerror already called */
237+ struct token_t *tkn = new_token(TKN_STRING);
238+ if(tkn == NULL)
239+ {
240+ return NULL; /* yyerror already called */
249241 }
250242
251- len = strlen(text);
252-
253243 tkn->value.type = V_STR;
254- tkn->value.string = (char *) malloc(len+1);
255- if (tkn->value.string == NULL) {
256- yyerror("string(): out of memory");
257- return NULL;
258- }
259244 strcpy(tkn->value.string, text);
260245
261246 #if DEBUG
@@ -312,19 +297,6 @@
312297 }
313298
314299
315-char * dup_string(const char * str)
316-{
317- char * s;
318-
319- s = strdup(str);
320- if (s == NULL) {
321- yyerror("dup_string(): out of memory");
322- return NULL;
323- }
324-
325- return s;
326-}
327-
328300 #ifdef HAVE_YYLEX_DESTROY
329301 /* reset lexer and free any allocated memory */
330302 extern int yylex_destroy(void);
@@ -336,7 +308,8 @@
336308 int r;
337309
338310 f = fopen(file, "r");
339- if (f == NULL) {
311+ if(f == NULL)
312+ {
340313 avrdude_message(MSG_INFO, "%s: can't open config file \"%s\": %s\n",
341314 progname, file, strerror(errno));
342315 return -1;
@@ -353,6 +326,9 @@
353326 yylex_destroy();
354327 #endif
355328
329+ assert(string_list.size() == 0);
330+ assert(number_list.size() == 0);
331+
356332 fclose(f);
357333
358334 return r;
--- trunk/config.h (revision 1469)
+++ trunk/config.h (revision 1470)
@@ -28,17 +28,20 @@
2828 #define MAX_STR_CONST 1024
2929
3030 enum { V_NONE, V_NUM, V_NUM_REAL, V_STR };
31-typedef struct value_t {
31+typedef struct value_t
32+{
3233 int type;
33- /*union { TODO: use an anonymous union here ? */
34+ union
35+ {
3436 int number;
3537 double number_real;
36- char * string;
37- /*};*/
38+ char string[MAX_STR_CONST];
39+ };
3840 } VALUE;
3941
4042
41-typedef struct token_t {
43+typedef struct token_t
44+{
4245 int primary;
4346 VALUE value;
4447 } TOKEN;
@@ -55,8 +58,8 @@
5558 extern AVRMEM * current_mem;
5659 extern int lineno;
5760 extern const char * infile;
58-extern LISTID string_list;
59-extern LISTID number_list;
61+extern std::vector<TOKEN*> string_list;
62+extern std::vector<TOKEN*> number_list;
6063
6164
6265 extern char string_buf[MAX_STR_CONST];
@@ -74,12 +77,14 @@
7477
7578 int yywarning(char * errmsg, ...);
7679
77-TOKEN * new_token(int primary);
80+TOKEN *new_token(int primary);
7881
79-void free_token(TOKEN * tkn);
82+void free_token(TOKEN *tkn);
8083
8184 void free_tokens(int n, ...);
8285
86+void free_tokenlist(std::vector<TOKEN*> &tokenlist);
87+
8388 TOKEN * number(char * text);
8489
8590 TOKEN * number_real(char * text);
@@ -86,7 +91,7 @@
8691
8792 TOKEN * hexnumber(char * text);
8893
89-TOKEN * string(char * text);
94+TOKEN *string(const char *text);
9095
9196 TOKEN * keyword(int primary);
9297
@@ -94,8 +99,6 @@
9499
95100 void pyytext(void);
96101
97-char * dup_string(const char * str);
98-
99102 //#ifdef __cplusplus
100103 //}
101104 //#endif
--- trunk/config_gram.y (revision 1469)
+++ trunk/config_gram.y (revision 1470)
@@ -425,14 +425,14 @@
425425 ;
426426
427427 string_list :
428- TKN_STRING { ladd(string_list, $1); } |
429- string_list TKN_COMMA TKN_STRING { ladd(string_list, $3); }
428+ TKN_STRING { string_list.push_back($1); } |
429+ string_list TKN_COMMA TKN_STRING { string_list.push_back($3); }
430430 ;
431431
432432
433433 num_list :
434- TKN_NUMBER { ladd(number_list, $1); } |
435- num_list TKN_COMMA TKN_NUMBER { ladd(number_list, $3); }
434+ TKN_NUMBER { number_list.push_back($1); } |
435+ num_list TKN_COMMA TKN_NUMBER { number_list.push_back($3); }
436436 ;
437437
438438 prog_parms :
@@ -443,11 +443,10 @@
443443 prog_parm :
444444 K_ID TKN_EQUAL string_list {
445445 {
446- TOKEN * t;
447446 int do_yyabort = 0;
448- while (lsize(string_list))
447+ for(std::vector<TOKEN*>::iterator it = string_list.begin(); it != string_list.end(); ++it)
449448 {
450- t = static_cast<TOKEN*>(lrmv_n(string_list, 1));
449+ TOKEN *t = *it;
451450 if (!do_yyabort)
452451 {
453452 const char *s = t->value.string;
@@ -461,8 +460,8 @@
461460 }
462461 }
463462 /* if do_yyabort == 1 just make the list empty */
464- free_token(t);
465463 }
464+ free_tokenlist(string_list);
466465 if (do_yyabort) {
467466 YYABORT;
468467 }
@@ -704,9 +703,9 @@
704703
705704 K_SIGNATURE TKN_EQUAL TKN_NUMBER TKN_NUMBER TKN_NUMBER {
706705 {
707- current_part->signature[0] = $3->value.number;
708- current_part->signature[1] = $4->value.number;
709- current_part->signature[2] = $5->value.number;
706+ current_part->signature[0] = (uint8_t)$3->value.number;
707+ current_part->signature[1] = (uint8_t)$4->value.number;
708+ current_part->signature[2] = (uint8_t)$5->value.number;
710709 free_token($3);
711710 free_token($4);
712711 free_token($5);
@@ -722,7 +721,6 @@
722721
723722 K_PP_CONTROLSTACK TKN_EQUAL num_list {
724723 {
725- TOKEN * t;
726724 unsigned nbytes;
727725 int ok;
728726
@@ -731,29 +729,29 @@
731729 ok = 1;
732730
733731 memset(current_part->controlstack, 0, CTL_STACK_SIZE);
734- while (lsize(number_list)) {
735- t = static_cast<TOKEN*>(lrmv_n(number_list, 1));
736- if (nbytes < CTL_STACK_SIZE)
737- {
738- current_part->controlstack[nbytes] = t->value.number;
739- nbytes++;
740- }
741- else
742- {
743- ok = 0;
744- }
745- free_token(t);
732+ for(std::vector<TOKEN*>::iterator it = number_list.begin(); it != number_list.end(); ++it)
733+ {
734+ TOKEN *t = *it;
735+ if (nbytes < CTL_STACK_SIZE)
736+ {
737+ current_part->controlstack[nbytes] = t->value.number;
738+ nbytes++;
739+ }
740+ else
741+ {
742+ ok = 0;
743+ }
746744 }
745+ free_tokenlist(number_list);
747746 if (!ok)
748- {
749- yywarning("too many bytes in control stack");
750- }
747+ {
748+ yywarning("too many bytes in control stack");
749+ }
751750 }
752751 } |
753752
754753 K_HVSP_CONTROLSTACK TKN_EQUAL num_list {
755754 {
756- TOKEN * t;
757755 unsigned nbytes;
758756 int ok;
759757
@@ -762,29 +760,29 @@
762760 ok = 1;
763761
764762 memset(current_part->controlstack, 0, CTL_STACK_SIZE);
765- while (lsize(number_list)) {
766- t = static_cast<TOKEN*>(lrmv_n(number_list, 1));
767- if (nbytes < CTL_STACK_SIZE)
768- {
769- current_part->controlstack[nbytes] = t->value.number;
770- nbytes++;
771- }
772- else
773- {
774- ok = 0;
775- }
776- free_token(t);
763+ for(std::vector<TOKEN*>::iterator it = number_list.begin(); it != number_list.end(); ++it)
764+ {
765+ TOKEN *t = *it;
766+ if (nbytes < CTL_STACK_SIZE)
767+ {
768+ current_part->controlstack[nbytes] = t->value.number;
769+ nbytes++;
770+ }
771+ else
772+ {
773+ ok = 0;
774+ }
777775 }
776+ free_tokenlist(number_list);
778777 if (!ok)
779- {
780- yywarning("too many bytes in control stack");
781- }
778+ {
779+ yywarning("too many bytes in control stack");
780+ }
782781 }
783782 } |
784783
785784 K_FLASH_INSTR TKN_EQUAL num_list {
786785 {
787- TOKEN * t;
788786 unsigned nbytes;
789787 int ok;
790788
@@ -792,19 +790,20 @@
792790 ok = 1;
793791
794792 memset(current_part->flash_instr, 0, FLASH_INSTR_SIZE);
795- while (lsize(number_list)) {
796- t = static_cast<TOKEN*>(lrmv_n(number_list, 1));
797- if (nbytes < FLASH_INSTR_SIZE)
798- {
799- current_part->flash_instr[nbytes] = t->value.number;
800- nbytes++;
801- }
802- else
803- {
804- ok = 0;
805- }
806- free_token(t);
793+ for(std::vector<TOKEN*>::iterator it = number_list.begin(); it != number_list.end(); ++it)
794+ {
795+ TOKEN *t = *it;
796+ if (nbytes < FLASH_INSTR_SIZE)
797+ {
798+ current_part->flash_instr[nbytes] = t->value.number;
799+ nbytes++;
800+ }
801+ else
802+ {
803+ ok = 0;
804+ }
807805 }
806+ free_tokenlist(number_list);
808807 if (!ok)
809808 {
810809 yywarning("too many bytes in flash instructions");
@@ -814,7 +813,6 @@
814813
815814 K_EEPROM_INSTR TKN_EQUAL num_list {
816815 {
817- TOKEN * t;
818816 unsigned nbytes;
819817 int ok;
820818
@@ -822,19 +820,20 @@
822820 ok = 1;
823821
824822 memset(current_part->eeprom_instr, 0, EEPROM_INSTR_SIZE);
825- while (lsize(number_list)) {
826- t = static_cast<TOKEN*>(lrmv_n(number_list, 1));
827- if (nbytes < EEPROM_INSTR_SIZE)
828- {
829- current_part->eeprom_instr[nbytes] = t->value.number;
830- nbytes++;
831- }
832- else
833- {
834- ok = 0;
835- }
836- free_token(t);
823+ for(std::vector<TOKEN*>::iterator it = number_list.begin(); it != number_list.end(); ++it)
824+ {
825+ TOKEN *t = *it;
826+ if (nbytes < EEPROM_INSTR_SIZE)
827+ {
828+ current_part->eeprom_instr[nbytes] = t->value.number;
829+ nbytes++;
830+ }
831+ else
832+ {
833+ ok = 0;
834+ }
837835 }
836+ free_tokenlist(number_list);
838837 if (!ok)
839838 {
840839 yywarning("too many bytes in EEPROM instructions");
@@ -1445,13 +1444,13 @@
14451444
14461445 static int assign_pin_list(int invert)
14471446 {
1448- TOKEN * t;
14491447 int pin;
14501448 int rv = 0;
14511449
14521450 current_prog->pinno[pin_name] = 0;
1453- while (lsize(number_list)) {
1454- t = static_cast<TOKEN*>(lrmv_n(number_list, 1));
1451+ for(std::vector<TOKEN*>::iterator it = number_list.begin(); it != number_list.end(); ++it)
1452+ {
1453+ TOKEN *t = *it;
14551454 if (rv == 0) {
14561455 pin = t->value.number;
14571456 if ((pin < PIN_MIN) || (pin > PIN_MAX)) {
@@ -1461,8 +1460,8 @@
14611460 }
14621461 pin_set_value(&(current_prog->pin[pin_name]), pin, invert);
14631462 }
1464- free_token(t);
14651463 }
1464+ free_tokenlist(number_list);
14661465 return rv;
14671466 }
14681467
@@ -1491,7 +1490,6 @@
14911490
14921491 static int parse_cmdbits(OPCODE * op)
14931492 {
1494- TOKEN * t;
14951493 int bitno;
14961494 char ch;
14971495 char * e;
@@ -1501,10 +1499,10 @@
15011499 int rv = 0;
15021500
15031501 bitno = 32;
1504- while (lsize(string_list)) {
1502+ for(std::vector<TOKEN*>::iterator it = string_list.begin(); it != string_list.end(); ++it)
1503+ {
1504+ TOKEN *t = *it;
15051505
1506- t = static_cast<TOKEN*>(lrmv_n(string_list, 1));
1507-
15081506 s = strtok_r(t->value.string, " ", &brkt);
15091507 while (rv == 0 && s != NULL) {
15101508
@@ -1585,9 +1583,9 @@
15851583 s = strtok_r(NULL, " ", &brkt);
15861584 } /* while */
15871585
1588- free_token(t);
1589-
15901586 } /* while */
15911587
1588+ free_tokenlist(string_list);
1589+
15921590 return rv;
15931591 }
--- trunk/libavrdude.h (revision 1469)
+++ trunk/libavrdude.h (revision 1470)
@@ -315,7 +315,7 @@
315315 void avr_free_part(AVRPART * d);
316316 void avr_free_parts();
317317 void remove_part(std::vector<AVRPART> &parts, AVRPART *data_ptr);
318-AVRPART * locate_part(std::vector<AVRPART> &parts, char *partdesc);
318+AVRPART * locate_part(std::vector<AVRPART> &parts, const char *partdesc);
319319 AVRPART * locate_part_by_avr910_devcode(std::vector<AVRPART> &parts, int devcode);
320320 AVRPART * locate_part_by_signature(std::vector<AVRPART> &parts, unsigned char * sig,
321321 int sigsize);
@@ -919,7 +919,7 @@
919919
920920 int init_config(void);
921921
922-void cleanup_config(void);
922+void cleanup_config();
923923
924924 int read_config(const char * file);
925925
Show on old repository browser