Skip to content

Commit

Permalink
Fixed multiple cache misses.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfriend99 committed Jun 3, 2021
1 parent 2185e66 commit 9a46172
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/b_asprintf.h
Expand Up @@ -41,7 +41,7 @@ int vasprintf(char **strp, const char *format, va_list ap) {
int len = vscprintf(format, ap);
if (len == -1)
return -1;
char *str = (char *)malloc((len + 1) * sizeof(char));
char *str = (char *)calloc(len + 1, sizeof(char));
if (!str)
return -1;
int retval = vsnprintf(str, len + 1, format, ap);
Expand Down
24 changes: 18 additions & 6 deletions src/b_string.c
Expand Up @@ -657,8 +657,12 @@ DECLARE_STRING_METHOD(match) {
for (int i = 0; i < (int)name_count; i++) {
int n = (tab_ptr[0] << 8) | tab_ptr[1];

char *_key = malloc(sizeof(char *));
char *_val = malloc(sizeof(char *));
char *_key = malloc(sizeof(char));
memset(_key, 0, sizeof(char));

char *_val = malloc(sizeof(char));
memset(_val, 0, sizeof(char));

sprintf(_key, "%*s", name_entry_size - 3, tab_ptr + 2);
sprintf(_val, "%*s", (int)(o_vector[2 * n + 1] - o_vector[2 * n]),
subject + o_vector[2 * n]);
Expand Down Expand Up @@ -779,8 +783,12 @@ DECLARE_STRING_METHOD(matches) {
for (i = 0; i < (int)name_count; i++) {
int n = (tab_ptr[0] << 8) | tab_ptr[1];

char *_key = malloc(sizeof(char *));
char *_val = malloc(sizeof(char *));
char *_key = malloc(sizeof(char));
memset(_key, 0, sizeof(char));

char *_val = malloc(sizeof(char));
memset(_val, 0, sizeof(char));

sprintf(_key, "%*s", name_entry_size - 3, tab_ptr + 2);
sprintf(_val, "%*s", (int)(o_vector[2 * n + 1] - o_vector[2 * n]),
subject + o_vector[2 * n]);
Expand Down Expand Up @@ -896,8 +904,12 @@ DECLARE_STRING_METHOD(matches) {
for (i = 0; i < (int)name_count; i++) {
int n = (tab_ptr[0] << 8) | tab_ptr[1];

char *_key = malloc(sizeof(char *));
char *_val = malloc(sizeof(char *));
char *_key = malloc(sizeof(char));
memset(_key, 0, sizeof(char));

char *_val = malloc(sizeof(char));
memset(_val, 0, sizeof(char));

sprintf(_key, "%*s", name_entry_size - 3, tab_ptr + 2);
sprintf(_val, "%*s", (int)(o_vector[2 * n + 1] - o_vector[2 * n]),
subject + o_vector[2 * n]);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler.c
Expand Up @@ -1038,7 +1038,7 @@ static int read_unicode_escape(b_parser *p, char *string, char *real_string,
}

static char *compile_string(b_parser *p) {
char *str = (char *)malloc(sizeof(char) * ((p->previous.length - (p->had_error ? 1 : 2)) + 1));
char *str = (char *)calloc((p->previous.length - (p->had_error ? 1 : 2)) + 1, sizeof(char));
char *real = (char *)(p->previous.start + (p->had_error ? 0 : 1));

int real_length = p->previous.length - (p->had_error ? 1 : 2);
Expand Down
3 changes: 2 additions & 1 deletion src/memory.c
Expand Up @@ -229,7 +229,7 @@ static void free_object(b_vm *vm, b_obj *object) {
case OBJ_FUNCTION: {
b_obj_func *function = (b_obj_func *)object;
free_blob(vm, &function->blob);
// free((void *)function->file);
function->file = NULL;
FREE(b_obj_func, object);
break;
}
Expand Down Expand Up @@ -320,6 +320,7 @@ void free_objects(b_vm *vm) {
}

free(vm->gray_stack);
vm->gray_stack = NULL;
}

void collect_garbage(b_vm *vm) {
Expand Down
4 changes: 3 additions & 1 deletion src/module.c
Expand Up @@ -27,7 +27,9 @@ void bind_native_modules(b_vm *vm, b_obj_string *module_name,

if (is_core_library_file((char *) module_path, module_name->chars)) {
for (int i = 0; modules[i].name != NULL; i++) {
if (memcmp(modules[i].name, module_name->chars, module_name->length) ==
int _module_name_length = (int)strlen(modules[i].name);
if (_module_name_length == module_name->length &&
memcmp(modules[i].name, module_name->chars, _module_name_length) ==
0) {
b_module_reg module = modules[i].module_func(vm);

Expand Down
7 changes: 4 additions & 3 deletions src/object.c
Expand Up @@ -326,7 +326,7 @@ static inline char *function_to_string(b_obj_func *func) {
if (func->name == NULL) {
return "<script 0x00>";
}
char *str = (char *)malloc(sizeof(char *));
char *str = (char *)malloc(sizeof(char) * (snprintf(NULL, 0, "<function %s>", func->name->chars)));
sprintf(str, "<function %s>", func->name->chars);
return str;
}
Expand All @@ -346,7 +346,7 @@ static inline char *list_to_string(b_vm *vm, b_value_arr *array) {
static inline char *bytes_to_string(b_vm *vm, b_byte_arr *array) {
char *str = "(";
for (int i = 0; i < array->count; i++) {
char *chars = (char *)malloc(sizeof(char *));
char *chars = (char *)malloc(sizeof(char) * (snprintf(NULL, 0, "0x%x", array->bytes[i])));
sprintf(chars, "0x%x", array->bytes[i]);

if (i != array->count - 1) {
Expand Down Expand Up @@ -378,7 +378,8 @@ static char *dict_to_string(b_vm *vm, b_obj_dict *dict) {
}

char *object_to_string(b_vm *vm, b_value value) {
char *str = (char *)malloc(sizeof(char *));
char *str = (char *)malloc(sizeof(char));
memset(str, 0, sizeof(char));

switch (OBJ_TYPE(value)) {
case OBJ_SWITCH: {
Expand Down
4 changes: 3 additions & 1 deletion src/pathinfo.c
Expand Up @@ -73,7 +73,7 @@ char *get_exe_path() {

char *get_exe_path() {
char raw_path[PATH_MAX];
char *real_path = malloc(PATH_MAX * sizeof(char));
char *real_path = (char *)malloc(PATH_MAX * sizeof(char));
uint32_t raw_size = (uint32_t) sizeof(raw_path);

if (!_NSGetExecutablePath(raw_path, &raw_size)) {
Expand Down Expand Up @@ -126,6 +126,8 @@ char *get_filename(char *filepath) {
}
length = length - start;
char *string = malloc(sizeof(char));
memset(string, 0, sizeof(char));

strncat(string, filepath + start, length);
return string;
}
Expand Down
2 changes: 1 addition & 1 deletion src/scanner.c
Expand Up @@ -30,7 +30,7 @@ static b_token error_token(b_scanner *s, const char *message, ...) {
va_list args;
va_start(args, message);
int length = vsnprintf(NULL, 0, message, args);
char *err = (char*) malloc(sizeof(char) * (length + 1));
char *err = (char*) calloc(length + 1, sizeof(char));
vsprintf(err, message, args);
va_end(args);

Expand Down
7 changes: 5 additions & 2 deletions src/util.c
Expand Up @@ -24,7 +24,7 @@ int utf8_number_bytes(int value) {
char *utf8_encode(unsigned int code) {
int count = utf8_number_bytes((int)code);
if (count > 0) {
char *chars = (char *) malloc((count + 1) * sizeof(char));
char *chars = (char *) calloc(count + 1, sizeof(char));
if (code <= 0x7F) {
chars[0] = (char)(code & 0x7F);
chars[1] = '\0';
Expand Down Expand Up @@ -126,7 +126,7 @@ char *append_strings(const char *old, const char *new_str) {
const size_t out_len = old_len + new_len;

// allocate a pointer to the new string
char *out = malloc(out_len + sizeof(char));
char *out = calloc(out_len + 1, sizeof(char));

// concat both strings and return
if (out != NULL) {
Expand Down Expand Up @@ -206,13 +206,16 @@ char *read_file(const char *path) {

// the system might not have enough memory to read the file.
if (buffer == NULL) {
fclose(fp);
return NULL;
}

size_t bytes_read = fread(buffer, sizeof(char), file_size, fp);

// if we couldn't read the entire file
if (bytes_read < file_size) {
fclose(fp);
free(buffer);
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion src/value.c
Expand Up @@ -16,7 +16,7 @@ void init_value_arr(b_value_arr *array) {

void init_byte_arr(b_byte_arr *array, int length) {
array->count = length;
array->bytes = (unsigned char *) calloc(length, sizeof(unsigned char *));
array->bytes = (unsigned char *) calloc(length, sizeof(unsigned char));
}

void write_value_arr(b_vm *vm, b_value_arr *array, b_value value) {
Expand Down
2 changes: 1 addition & 1 deletion src/vm.c
Expand Up @@ -53,7 +53,7 @@ static b_value get_stack_trace(b_vm *vm){
const char* trace_start = " File: %s, Line: %d, In: ";
size_t trace_start_length = snprintf(NULL, 0, trace_start, function->file, line);

char *trace_part = (char*)malloc(trace_start_length + sizeof(char));
char *trace_part = (char*)calloc(trace_start_length + 1, sizeof(char));
sprintf(trace_part, trace_start, function->file, line);
trace_part[(int)trace_start_length] = '\0';

Expand Down

0 comments on commit 9a46172

Please sign in to comment.