bc: BC_STATUS_VEC_ITEM_EXISTS is not a real error code, its message was never used

It was only used to indicate rusult of bc_map_insert() - did we insert, or
did we find that this key is already in the map?

function                                             old     new   delta
bc_map_insert                                        142     145      +3
bc_program_addFunc                                   226     225      -1
bc_err_msgs                                          184     180      -4
bc_program_search                                    152     143      -9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/3 up/down: 3/-14)             Total: -11 bytes
   text	   data	    bss	    dec	    hex	filename
 987904	    485	   7296	 995685	  f3165	busybox_old
 987873	    485	   7296	 995654	  f3146	busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko
2018-12-03 20:35:16 +01:00
parent bc5ce66617
commit a02f84472a

View File

@@ -217,7 +217,8 @@ typedef enum BcStatus {
BC_STATUS_EXEC_STACK, BC_STATUS_EXEC_STACK,
// BC_STATUS_VEC_OUT_OF_BOUNDS, // BC_STATUS_VEC_OUT_OF_BOUNDS,
BC_STATUS_VEC_ITEM_EXISTS, // BC_STATUS_VEC_ITEM_EXISTS,
BC_STATUS_BEFORE_POSIX = BC_STATUS_EXEC_STACK,
#if ENABLE_BC #if ENABLE_BC
BC_STATUS_POSIX_NAME_LEN, BC_STATUS_POSIX_NAME_LEN,
BC_STATUS_POSIX_COMMENT, BC_STATUS_POSIX_COMMENT,
@@ -288,7 +289,7 @@ static const char *const bc_err_msgs[] = {
"stack has too few elements", "stack has too few elements",
// "index is out of bounds", // "index is out of bounds",
"item already exists", // "item already exists",
#if ENABLE_BC #if ENABLE_BC
"POSIX only allows one character names; the following is bad:", "POSIX only allows one character names; the following is bad:",
"POSIX does not allow '#' script comments", "POSIX does not allow '#' script comments",
@@ -1276,20 +1277,17 @@ static size_t bc_map_find(const BcVec *v, const void *ptr)
return low; return low;
} }
static BcStatus bc_map_insert(BcVec *v, const void *ptr, size_t *i) static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
{ {
BcStatus s = BC_STATUS_SUCCESS; size_t n = *i = bc_map_find(v, ptr);
*i = bc_map_find(v, ptr); if (n == v->len)
if (*i == v->len)
bc_vec_push(v, ptr); bc_vec_push(v, ptr);
else if (!bc_id_cmp(ptr, bc_vec_item(v, *i))) else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
s = BC_STATUS_VEC_ITEM_EXISTS; return 0; // "was not inserted"
else else
bc_vec_pushAt(v, ptr, *i); bc_vec_pushAt(v, ptr, n);
return 1; // "was inserted"
return s;
} }
static size_t bc_map_index(const BcVec *v, const void *ptr) static size_t bc_map_index(const BcVec *v, const void *ptr)
@@ -5240,20 +5238,18 @@ static BcStatus common_parse_expr(BcParse *p, uint8_t flags)
static BcVec* bc_program_search(char *id, bool var) static BcVec* bc_program_search(char *id, bool var)
{ {
BcStatus s;
BcId e, *ptr; BcId e, *ptr;
BcVec *v, *map; BcVec *v, *map;
size_t i; size_t i;
BcResultData data; BcResultData data;
bool new; int new;
v = var ? &G.prog.vars : &G.prog.arrs; v = var ? &G.prog.vars : &G.prog.arrs;
map = var ? &G.prog.var_map : &G.prog.arr_map; map = var ? &G.prog.var_map : &G.prog.arr_map;
e.name = id; e.name = id;
e.idx = v->len; e.idx = v->len;
s = bc_map_insert(map, &e, &i); new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
new = s != BC_STATUS_VEC_ITEM_EXISTS;
if (new) { if (new) {
bc_array_init(&data.v, var); bc_array_init(&data.v, var);
@@ -6465,20 +6461,20 @@ static void bc_program_pushGlobal(char inst)
static void bc_program_addFunc(char *name, size_t *idx) static void bc_program_addFunc(char *name, size_t *idx)
{ {
BcStatus s;
BcId entry, *entry_ptr; BcId entry, *entry_ptr;
BcFunc f; BcFunc f;
int inserted;
entry.name = name; entry.name = name;
entry.idx = G.prog.fns.len; entry.idx = G.prog.fns.len;
s = bc_map_insert(&G.prog.fn_map, &entry, idx); inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
if (s) free(name); if (!inserted) free(name);
entry_ptr = bc_vec_item(&G.prog.fn_map, *idx); entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
*idx = entry_ptr->idx; *idx = entry_ptr->idx;
if (s == BC_STATUS_VEC_ITEM_EXISTS) { if (!inserted) {
BcFunc *func = bc_vec_item(&G.prog.fns, entry_ptr->idx); BcFunc *func = bc_vec_item(&G.prog.fns, entry_ptr->idx);
@@ -6850,7 +6846,7 @@ static void bc_vm_info(void)
static BcStatus bc_vm_error(BcStatus s, const char *file, size_t line) static BcStatus bc_vm_error(BcStatus s, const char *file, size_t line)
{ {
if (!s || s > BC_STATUS_VEC_ITEM_EXISTS) return s; if (!s || s > BC_STATUS_BEFORE_POSIX) return s;
fprintf(stderr, bc_err_fmt, bc_err_msgs[s]); fprintf(stderr, bc_err_fmt, bc_err_msgs[s]);
fprintf(stderr, " %s", file); fprintf(stderr, " %s", file);