shithub: femtolisp

Download patch

ref: 77eabfb31feddd18ecf6fcb2da6bb5b13ae853d0
parent: af68820de45fb5675495e70c38d2688138701936
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Tue Dec 24 22:34:34 EST 2024

mk_symbol: call strlen less, use MEM_ALLOC

--- a/flisp.c
+++ b/flisp.c
@@ -165,7 +165,7 @@
 
 // symbol table ---------------------------------------------------------------
 
-bool
+static inline bool
 fl_is_keyword_name(const char *str, size_t len)
 {
 	return (str[0] == ':' || str[len-1] == ':') && str[1] != '\0';
@@ -172,21 +172,22 @@
 }
 
 static symbol_t *
-mk_symbol(const char *str, bool copy)
+mk_symbol(const char *str, int len, bool copy)
 {
 	symbol_t *sym;
-	int len = strlen(str);
 
-	sym = calloc(1, sizeof(*sym) + (copy ? len+1 : 0));
+	sym = MEM_ALLOC(sizeof(*sym) + (copy ? len+1 : 0));
 	assert(((uintptr_t)sym & 0x7) == 0); // make sure malloc aligns 8
 	sym->numtype = NONNUMERIC;
 	if(fl_is_keyword_name(str, len)){
 		value_t s = tagptr(sym, TAG_SYM);
 		setc(s, s);
-		sym->flags |= FLAG_KEYWORD;
+		sym->flags = FLAG_KEYWORD;
 	}else{
 		sym->binding = UNBOUND;
+		sym->flags = 0;
 	}
+	sym->type = nil;
 	sym->hash = memhash32(str, len)^0xAAAAAAAA;
 	if(copy){
 		sym->name = (const char*)(sym+1);
@@ -194,6 +195,7 @@
 	}else{
 		sym->name = str;
 	}
+	sym->size = 0;
 	return sym;
 }
 
@@ -204,7 +206,7 @@
 	symbol_t *v;
 	const char *k;
 	if(!Tgetkv(FL(symtab), str, len, &k, (void**)&v)){
-		v = mk_symbol(str, copy);
+		v = mk_symbol(str, len, copy);
 		FL(symtab) = Tsetl(FL(symtab), v->name, len, v);
 	}
 	return tagptr(v, TAG_SYM);
--- a/flisp.h
+++ b/flisp.h
@@ -57,7 +57,7 @@
 	value_t cdr;
 }cons_t;
 
-typedef struct _symbol_t {
+typedef struct {
 	const char *name;
 	value_t binding;   // global value binding
 	fltype_t *type;
@@ -193,7 +193,6 @@
 value_t fl_list2(value_t a, value_t b);
 value_t fl_listn(size_t n, ...);
 bool fl_isnumber(value_t v);
-bool fl_is_keyword_name(const char *str, size_t len);
 value_t alloc_vector(size_t n, int init);
 
 /* safe casts */