Skip to content

Commit b7088cb

Browse files
strlen elimination
1 parent 1c76f99 commit b7088cb

12 files changed

Lines changed: 53 additions & 57 deletions

File tree

.idea/.name

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

include/basic/array.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ int64_t basic_get_int_array_variable(const char* var, int64_t index, struct basi
6565
* @param var The variable name.
6666
* @param index The index in the array.
6767
* @param ctx The BASIC context.
68+
* @param out_len
6869
* @return The value at the specified index.
6970
*/
7071
const char* basic_get_string_array_variable(const char* var, int64_t index, struct basic_ctx* ctx, size_t* out_len);
@@ -75,6 +76,7 @@ const char* basic_get_string_array_variable(const char* var, int64_t index, stru
7576
* @param var The variable name.
7677
* @param size The size of the array.
7778
* @param ctx The BASIC context.
79+
* @param var_length
7880
* @return True if the array was successfully dimensioned, false otherwise.
7981
*/
8082
bool basic_dim_string_array(const char* var, int64_t size, struct basic_ctx* ctx, size_t var_length);
@@ -85,6 +87,7 @@ bool basic_dim_string_array(const char* var, int64_t size, struct basic_ctx* ctx
8587
* @param var The variable name.
8688
* @param size The size of the array.
8789
* @param ctx The BASIC context.
90+
* @param var_length
8891
* @return True if the array was successfully dimensioned, false otherwise.
8992
*/
9093
bool basic_dim_int_array(const char* var, int64_t size, struct basic_ctx* ctx, size_t var_length);
@@ -95,6 +98,7 @@ bool basic_dim_int_array(const char* var, int64_t size, struct basic_ctx* ctx, s
9598
* @param var The variable name.
9699
* @param size The size of the array.
97100
* @param ctx The BASIC context.
101+
* @param var_length
98102
* @return True if the array was successfully dimensioned, false otherwise.
99103
*/
100104
bool basic_dim_double_array(const char* var, int64_t size, struct basic_ctx* ctx, size_t var_length);

include/basic/context.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,13 @@ typedef struct basic_ctx {
356356
*/
357357
void* fn_return;
358358

359+
/**
360+
* @brief When the return value in fn_return is RT_STRING, this contains the length of that string.
361+
*
362+
* At all other times it contains 0.
363+
*/
364+
size_t fn_return_len;
365+
359366
/**
360367
* @brief Current graphics color for graphical operations (e.g., drawing lines, shapes).
361368
*/
@@ -613,9 +620,10 @@ void basic_eval_double_fn(const char* fn_name, struct basic_ctx* ctx, double* re
613620
*
614621
* @param fn_name The name of the function to evaluate.
615622
* @param ctx The BASIC context.
623+
* @param out_len
616624
* @return The evaluated string result of the function.
617625
*/
618-
const char* basic_eval_str_fn(const char* fn_name, struct basic_ctx* ctx);
626+
const char* basic_eval_str_fn(const char* fn_name, struct basic_ctx* ctx, size_t* out_len);
619627

620628
/**
621629
* @brief Check if a function name corresponds to a built-in double (floating-point) function.
@@ -664,9 +672,10 @@ void free_local_heap(struct basic_ctx* ctx);
664672
* @brief Check if a function name corresponds to a built-in double (floating-point) function.
665673
*
666674
* @param fn_name The name of the function to check.
675+
* @param L
667676
* @return True if the function is a built-in double function, false otherwise.
668677
*/
669-
bool is_builtin_double_fn(const char* fn_name);
678+
bool is_builtin_double_fn(const char* fn_name, size_t L);
670679

671680
/**
672681
* @brief Free function definitions and associated resources in the BASIC context.

include/basic/structs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ typedef enum ub_return_type {
157157
* containing the name of the parameter and a pointer to the next parameter.
158158
*/
159159
typedef struct ub_param {
160-
const char *name; ///< Name of the parameter
160+
const char *name; ///< Name of the parameter variable
161+
size_t name_len; ///< Length of parameter variable name
161162
struct ub_param *next; ///< Pointer to the next parameter
162163
} ub_param;
163164

include/rr-cpuid.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ typedef struct cpu_caps {
236236
char vendor[13];
237237
/** @brief CPU brand string */
238238
char brand[49];
239+
/** @brief Length of brand string */
240+
size_t brand_len;
239241
/** @brief True if a hypervisor is present */
240242
bool hypervisor_present;
241243
/** @brief Hypervisor vendor string */

src/basic/function.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
extern bool debug;
1111

12-
/* Add near the top of basic/function.c, after extern bool debug */
13-
1412
struct builtin_int_entry {
1513
const char *name;
1614
size_t name_length;
@@ -395,7 +393,7 @@ bool extract_comma_list(struct ub_proc_fn_def* def, struct basic_ctx* ctx, int*
395393
ctx->current_token = get_next_token(ctx);
396394
*oldptr = 0;
397395
if (*param && (*param)->name) {
398-
size_t len = strlen((*param)->name);
396+
size_t len = (*param)->name_len;
399397
if ((*param)->name[len - 1] == '$') {
400398
const char* save_ptr = ctx->ptr;
401399
size_t v_len;
@@ -429,10 +427,11 @@ bool extract_comma_list(struct ub_proc_fn_def* def, struct basic_ctx* ctx, int*
429427
return true;
430428
}
431429

432-
const char* basic_eval_str_fn(const char* fn_name, struct basic_ctx* ctx)
430+
const char* basic_eval_str_fn(const char* fn_name, struct basic_ctx* ctx, size_t* out_len)
433431
{
434432
struct ub_proc_fn_def* def = basic_find_fn(fn_name + 2, ctx);
435433
const char* rv = "";
434+
*out_len = 0;
436435
if (def) {
437436
if (!new_stack_frame(ctx)) {
438437
return "";
@@ -480,6 +479,7 @@ const char* basic_eval_str_fn(const char* fn_name, struct basic_ctx* ctx)
480479
if (!rv) {
481480
return "";
482481
}
482+
*out_len = atomic->fn_return_len;
483483
}
484484

485485
ctx->int_variables = atomic->int_variables;
@@ -819,7 +819,7 @@ bool basic_parse_fn(struct basic_ctx* ctx)
819819
tokenizer_error_printf(ctx, "Out of memory parsing functions");
820820
return false;
821821
}
822-
def.name_length = strlen(name);
822+
def.name_length = ni;
823823
def.type = type;
824824
def.line = currentline;
825825
def.params = NULL;
@@ -842,6 +842,7 @@ bool basic_parse_fn(struct basic_ctx* ctx)
842842
return false;
843843
}
844844
par->next = NULL;
845+
par->name_len = pni;
845846
par->name = buddy_strdup(ctx->allocator, pname);
846847
if (!par->name) {
847848
tokenizer_error_printf(ctx, "Out of memory parsing function parameters");
@@ -926,15 +927,9 @@ void basic_free_defs(struct basic_ctx* ctx)
926927
ctx->defs = NULL;
927928
}
928929

929-
bool is_builtin_double_fn(const char* fn_name)
930+
bool is_builtin_double_fn(const char* fn_name, size_t L)
930931
{
931-
struct builtin_double_entry key = {
932-
.name = fn_name,
933-
.name_length = strlen(fn_name),
934-
.handler = NULL
935-
};
936-
937-
return hashmap_get(builtin_double_map, &key) != NULL;
932+
return hashmap_get(builtin_double_map, &(struct builtin_double_entry){ .name = fn_name, .name_length = L, .handler = NULL }) != NULL;
938933
}
939934

940935
void proc_statement(struct basic_ctx* ctx)
@@ -1017,9 +1012,10 @@ void eq_statement(struct basic_ctx* ctx)
10171012
basic_debug("eq_statement\n");
10181013
accept_or_return(EQUALS, ctx);
10191014

1015+
ctx->fn_return_len = 0;
10201016
if (ctx->fn_type == RT_STRING) {
10211017
basic_debug("eq_statement return string\n");
1022-
ctx->fn_return = (void*)str_expr(ctx, NULL);
1018+
ctx->fn_return = (void*)str_expr(ctx, &ctx->fn_return_len);
10231019
} else if (ctx->fn_type == RT_FLOAT) {
10241020
basic_debug("eq_statement return double\n");
10251021
double_expr(ctx, (void*)&ctx->fn_return);
@@ -1033,8 +1029,6 @@ void eq_statement(struct basic_ctx* ctx)
10331029
dprintf("EQ statement: fn type??? %d\n", ctx->fn_type);
10341030
}
10351031

1036-
//accept_or_return(NEWLINE, ctx);
1037-
10381032
ctx->ended = true;
10391033
}
10401034

src/basic/lowlevel.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,8 @@ int64_t basic_memfind(struct basic_ctx* ctx)
172172

173173
char* basic_cpugetbrand(struct basic_ctx* ctx, size_t* out_len)
174174
{
175-
PARAMS_START;
176-
PARAMS_GET_ITEM(BIP_INT);
177-
bool trim = intval;
178-
PARAMS_END("CPUGETBRAND$", "");
179-
180-
const char *bufferp = cpu_caps.brand;
181-
182-
if (trim) {
183-
while (*bufferp == ' ') {
184-
bufferp++;
185-
}
186-
}
187-
188-
*out_len = strlen(bufferp);
189-
return (char *)gc_strdup(ctx, bufferp);
175+
*out_len = cpu_caps.brand_len;
176+
return (char *)gc_strdup(ctx, cpu_caps.brand);
190177
}
191178

192179
char* basic_cpugetvendor(struct basic_ctx* ctx, size_t* out_len)

src/basic/main.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,11 @@ struct basic_ctx *basic_init(const char *program, uint32_t pid, const char *file
326326
ctx->double_array_variables = hashmap_new_with_allocator(varmap_malloc, varmap_realloc, varmap_free, sizeof(struct ub_var_double_array), 0, SEED0, SEED1, varmap_hash, varmap_compare, varmap_elfree_double_array, ctx->allocator);
327327
ctx->oldlen = 0;
328328
ctx->fn_return = NULL;
329+
ctx->fn_return_len = 0;
329330
memset(ctx->fn_type_stack, 0, sizeof(ctx->fn_type_stack));
330-
memset(ctx->local_int_variables, NULL, sizeof(ctx->local_int_variables));
331-
memset(ctx->local_string_variables, NULL, sizeof(ctx->local_string_variables));
332-
memset(ctx->local_double_variables, NULL, sizeof(ctx->local_double_variables));
331+
memset(ctx->local_int_variables, 0, sizeof(ctx->local_int_variables));
332+
memset(ctx->local_string_variables, 0, sizeof(ctx->local_string_variables));
333+
memset(ctx->local_double_variables, 0, sizeof(ctx->local_double_variables));
333334
// We allocate 5000 bytes extra on the end of the program for EVAL space,
334335
// as EVAL appends to the program on lines EVAL_LINE and EVAL_LINE + 1.
335336
ctx->program_ptr = buddy_malloc(ctx->allocator, strlen(program) + 5000);
@@ -589,6 +590,7 @@ struct basic_ctx *basic_clone(struct basic_ctx *old) {
589590
ctx->sounds = old->sounds;
590591
ctx->oldlen = old->oldlen;
591592
ctx->fn_return = NULL;
593+
ctx->fn_return_len = 0;
592594
ctx->program_ptr = old->program_ptr;
593595
ctx->for_stack_ptr = old->for_stack_ptr;
594596
ctx->call_stack_ptr = old->call_stack_ptr;

src/basic/map.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ struct hashmap *basic_get_map_by_handle(struct basic_ctx *ctx, int64_t handle)
8888
return found->map;
8989
}
9090

91-
map_value_t *basic_get_map_value(struct hashmap *map, const char *key)
91+
static map_value_t *basic_get_map_value(struct hashmap *map, const char *key, size_t key_len)
9292
{
93-
return hashmap_get(map, &(map_value_t){ .name = key, .name_length = strlen(key) });
93+
return hashmap_get(map, &(map_value_t){ .name = key, .name_length = key_len });
9494
}
9595

9696
void basic_free_map_up_value(struct basic_ctx *ctx, up_value *value)
@@ -161,7 +161,7 @@ int64_t basic_maphas(struct basic_ctx *ctx)
161161
return 0;
162162
}
163163

164-
return basic_get_map_value(map, key) ? 1 : 0;
164+
return basic_get_map_value(map, key, strlength) ? 1 : 0;
165165
}
166166

167167
int64_t basic_mapget(struct basic_ctx *ctx)
@@ -181,7 +181,7 @@ int64_t basic_mapget(struct basic_ctx *ctx)
181181
return 0;
182182
}
183183

184-
entry = basic_get_map_value(map, key);
184+
entry = basic_get_map_value(map, key, strlength);
185185
if (!entry) {
186186
tokenizer_error_printf(ctx, "No such MAP key '%s'", key);
187187
return 0;
@@ -213,7 +213,7 @@ void basic_mapgetr(struct basic_ctx *ctx, double* rv)
213213
return;
214214
}
215215

216-
entry = basic_get_map_value(map, key);
216+
entry = basic_get_map_value(map, key, strlength);
217217
if (!entry) {
218218
tokenizer_error_printf(ctx, "No such MAP key '%s'", key);
219219
*rv = 0;
@@ -250,7 +250,7 @@ char *basic_mapgets(struct basic_ctx *ctx, size_t* out_len)
250250
return "";
251251
}
252252

253-
map_value_t* entry = basic_get_map_value(map, key);
253+
map_value_t* entry = basic_get_map_value(map, key, strlength);
254254
if (!entry) {
255255
tokenizer_error_printf(ctx, "No such MAP key '%s'", key);
256256
return "";
@@ -267,23 +267,19 @@ char *basic_mapgets(struct basic_ctx *ctx, size_t* out_len)
267267

268268
void mapset_statement(struct basic_ctx *ctx)
269269
{
270-
int64_t handle;
271-
const char *key;
272-
struct hashmap *map;
273-
map_value_t *found;
274270
map_value_t new_entry;
275271
up_value value;
276272

277273
accept_or_return(MAPSET, ctx);
278-
handle = expr(ctx);
274+
int64_t handle = expr(ctx);
279275
accept_or_return(COMMA, ctx);
280276
size_t elen;
281-
key = str_expr(ctx, &elen);
277+
const char* key = str_expr(ctx, &elen);
282278
accept_or_return(COMMA, ctx);
283279
up_eval_value(ctx, &value);
284280
accept_or_return(NEWLINE, ctx);
285281

286-
map = basic_get_map_by_handle(ctx, handle);
282+
struct hashmap* map = basic_get_map_by_handle(ctx, handle);
287283
if (!map) {
288284
tokenizer_error_print(ctx, "Invalid MAP");
289285
return;
@@ -298,7 +294,7 @@ void mapset_statement(struct basic_ctx *ctx)
298294
value.v.s.ptr = dup;
299295
}
300296

301-
found = basic_get_map_value(map, key);
297+
map_value_t* found = basic_get_map_value(map, key, elen);
302298
if (found) {
303299
if (found->value.kind != value.kind) {
304300
if (!(found->value.kind == UP_INT && value.kind == UP_REAL)) {
@@ -330,7 +326,7 @@ void mapset_statement(struct basic_ctx *ctx)
330326

331327
memset(&new_entry, 0, sizeof(new_entry));
332328
new_entry.name = buddy_strdup(ctx->allocator, key);
333-
new_entry.name_length = strlen(key);
329+
new_entry.name_length = elen;
334330
if (!new_entry.name) {
335331
basic_free_map_up_value(ctx, &value);
336332
tokenizer_error_print(ctx, "Out of memory");
@@ -356,7 +352,6 @@ void mapset_statement(struct basic_ctx *ctx)
356352

357353
void mapfree_statement(struct basic_ctx *ctx)
358354
{
359-
basic_map_handle_entry *found;
360355

361356
accept_or_return(MAPFREE, ctx);
362357
int64_t handle = expr(ctx);
@@ -367,7 +362,7 @@ void mapfree_statement(struct basic_ctx *ctx)
367362
return;
368363
}
369364

370-
found = hashmap_delete(ctx->maps, &(basic_map_handle_entry){ .id = handle});
365+
basic_map_handle_entry* found = hashmap_delete(ctx->maps, &(basic_map_handle_entry){.id = handle});
371366
if (!found) {
372367
tokenizer_error_print(ctx, "Invalid MAP");
373368
return;

src/basic/unified_expression.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static up_value up_factor(struct basic_ctx *ctx) {
129129
return up_make_real(d);
130130
}
131131

132-
if (is_builtin_double_fn(name)) {
132+
if (is_builtin_double_fn(name, L)) {
133133
/* Unsuffixed builtin known to return REAL */
134134
double d = 0.0;
135135
basic_get_numeric_variable(name, ctx, &d);

0 commit comments

Comments
 (0)