Skip to content

Commit

Permalink
Fix memory leaks and optimize memory usage (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
OJddJO authored Nov 6, 2024
2 parents a70e464 + 2ba2f8d commit 37ca3ed
Show file tree
Hide file tree
Showing 34 changed files with 361 additions and 212 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
> [!IMPORTANT]
> Word is still in development, some functionalities may not work as expected or may be missing. Please report any issues you encounter.
> [!WARNING]
> There are currently many memory leaks, high memory usage is expected. I'm currently working on them.
WordLanguage is a programming language designed to minimize the use of symbols, making it easy to read and write. Its syntax is simple and closely resembles natural language, aiming to be both easy to learn and powerful enough for complex programming tasks.

**Key Features**:
Expand Down
16 changes: 6 additions & 10 deletions bin/test.w
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
int a assign 0
def null test
infloop
if a gequal 5
break
endif
change a to a plus 1
endinf
enddef

call test
infloop
if a equal 1000000
break
endif
change a to a plus 1
endinf
print a
Binary file modified bin/word.exe
Binary file not shown.
1 change: 1 addition & 0 deletions include/dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define __DICT_H__

#include <string.h>
#include "w_alloc.h"
#include "list.h"

typedef struct _dict {
Expand Down
2 changes: 2 additions & 0 deletions include/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdlib.h>
#include <stdio.h>
#include "w_alloc.h"
#include "lexer.h"
#include "parser.h"
#include "scope.h"
Expand All @@ -17,6 +18,7 @@ void *execute(list *parsed_code, Scope *current_scope, W_Type return_type, bool
// Utility

void eval_parsed_lines(list_element *parsed_line, Scope *scope, list *stack);
void destroy_stack(list *stack);
char *remove_dot(W_Word *word);
bool is_type_keyword(char *word);
bool is_float(char *str);
Expand Down
2 changes: 2 additions & 0 deletions include/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "w_alloc.h"
#include "list.h"

extern char *keywords[];
Expand All @@ -23,6 +24,7 @@ typedef struct _w_word {
char *value;
int line;
bool parsed;
bool is_generated;
} W_Word;

list *word_tokenize(FILE *source);
Expand Down
2 changes: 2 additions & 0 deletions include/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define __LIST_H__

#include <stdlib.h>
#include "w_alloc.h"

typedef struct _list_element {
void *value;
Expand All @@ -29,5 +30,6 @@ void list_insert(list *l, int index, void *value);
int list_size(list *l);
void *list_pop(list *l);
void list_destroy(list *l);
void list_destroy_no_free(list *l);

#endif
1 change: 1 addition & 0 deletions include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdlib.h>
#include <stdio.h>
#include "w_alloc.h"
#include "lexer.h"
#include "w_list.h"

Expand Down
1 change: 1 addition & 0 deletions include/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __SCOPE_H__

#include <stdlib.h>
#include "w_alloc.h"
#include "w_type.h"
#include "w_dict.h"

Expand Down
30 changes: 30 additions & 0 deletions include/w_alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// DEBUG ONLY

#ifndef __W_ALLOC_H__
#define __W_ALLOC_H__

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "w_alloc.h"

#define MONITOR_MEMORY false

#if MONITOR_MEMORY

extern int w_alloc_count;
extern long long int w_alloc_total;

void *w_malloc(size_t size);
void w_free(void *ptr);
void w_alloc_print();

#elif !MONITOR_MEMORY

#define w_malloc malloc
#define w_free free
#define w_alloc_print() {}

#endif

#endif
1 change: 1 addition & 0 deletions include/w_bool.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __W_BOOL_H__

#include <stdbool.h>
#include "w_alloc.h"
#include "w_type.h"

typedef struct _w_bool {
Expand Down
1 change: 1 addition & 0 deletions include/w_dict.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __W_DICT_H__
#define __W_DICT_H__

#include "w_alloc.h"
#include "w_type.h"
#include "w_list.h"
#include "list.h"
Expand Down
1 change: 1 addition & 0 deletions include/w_float.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __W_FLOAT_H__
#define __W_FLOAT_H__

#include "w_alloc.h"
#include "w_type.h"

typedef struct _w_float {
Expand Down
1 change: 1 addition & 0 deletions include/w_function.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __W_FUNCTION_H__
#define __W_FUNCTION_H__

#include "w_alloc.h"
#include "w_type.h"
#include "list.h"
#include "dict.h"
Expand Down
1 change: 1 addition & 0 deletions include/w_int.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __W_INT_H__
#define __W_INT_H__

#include "w_alloc.h"
#include "w_type.h"

typedef struct _w_int {
Expand Down
1 change: 1 addition & 0 deletions include/w_list.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __W_LIST_H__
#define __W_LIST_H__

#include "w_alloc.h"
#include "w_type.h"

typedef struct _w_list_element {
Expand Down
1 change: 1 addition & 0 deletions include/w_stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "w_alloc.h"
#include "w_bool.h"
#include "w_dict.h"
#include "w_float.h"
Expand Down
1 change: 1 addition & 0 deletions include/w_str.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __W_STR_H__
#define __W_STR_H__

#include "w_alloc.h"
#include "w_type.h"

typedef struct _w_str {
Expand Down
1 change: 1 addition & 0 deletions include/w_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "w_alloc.h"

typedef enum _w_type {
NULL_TYPE,
Expand Down
12 changes: 6 additions & 6 deletions src/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
#include "dict.h"

/**
* \brief Initializes a new dictionary. (malloc)
* \brief Initializes a new dictionary. (w_malloc)
* \return A new dictionary.
*/
dict *dict_init() {
dict *d = (dict *)malloc(sizeof(dict));
dict *d = (dict *)w_malloc(sizeof(dict));
d->keys = list_init(); //list of keys, is a normal list
d->values = list_init(); //list of values, is a list
return d;
}

/**
* \brief Sets the given key and value in the given dictionary. (malloc)
* \brief Sets the given key and value in the given dictionary. (w_malloc)
* \param d The dictionary to set the key and value in.
* \param key The key to set. Must be a string.
* \param value The value to set.
Expand All @@ -29,7 +29,7 @@ void dict_set(dict *d, char *key, void *value) {
for (int i = 0; i < d->keys->size; i++) {
if (strcmp((char *)current_key->value, key) == 0) {
if (current_value->value != NULL) {
free(current_value->value);
w_free(current_value->value);
}
current_value->value = value;
return;
Expand All @@ -38,7 +38,7 @@ void dict_set(dict *d, char *key, void *value) {
current_value = current_value->next;
}
} else {
char *key_copy = (char *)malloc(strlen(key) + 1);
char *key_copy = (char *)w_malloc(strlen(key) + 1);
strcpy(key_copy, key);
list_append(d->keys, key_copy);
list_append(d->values, value);
Expand Down Expand Up @@ -123,5 +123,5 @@ void dict_remove(dict *d, char *key) {
void dict_destroy(dict *d) {
list_destroy(d->keys);
list_destroy(d->values);
free(d);
w_free(d);
}
Loading

0 comments on commit 37ca3ed

Please sign in to comment.