Skip to content

Commit

Permalink
#74 allocator is now a parameter so that it can be controlled by the …
Browse files Browse the repository at this point in the history
…caller
  • Loading branch information
dibyendumajumdar committed Oct 23, 2021
1 parent 21836c8 commit 74d2aed
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 76 deletions.
17 changes: 13 additions & 4 deletions include/ravi_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@

#include <stdlib.h>

struct Ravi_CompilerInterface {
typedef struct Ravi_MemoryAllocatorInterface {
void *arena;
void *(*realloc)(void *arena, void* mem, size_t newsize);
void *(*calloc)(void *arena, size_t n_elements, size_t elem_size);
void (*free)(void *arena, void *p);
} Ravi_MemoryAllocatorInterface;

typedef struct Ravi_CompilerInterface {
/* ------------------------ Inputs ------------------------------ */
void *context; /* Ravi supplied context */

Expand All @@ -39,21 +46,23 @@ struct Ravi_CompilerInterface {

char main_func_name[31]; /* Name of the generated function that when called will set up the Lua closure */

Ravi_MemoryAllocatorInterface *memory_allocator;

/* ------------------------- Outputs ------------------------------ */
const char* generated_code; /* Output of the compiler, must be freed by caller. */

/* ------------------------ Debugging and error handling ----------------------------------------- */
void (*debug_message)(void *context, const char *filename, long long line, const char *message);
void (*error_message)(void *context, const char *message);
};
} Ravi_CompilerInterface;

/**
* This is the API exposed by the Compiler itself. This function is invoked by
* Ravi when it is necessary to compile some Ravi code.
* @param compiler_interface The interface expected by the compiler must be setup
* @return 0 for success, non-zero for failure
*/
RAVICOMP_EXPORT int raviX_compile(struct Ravi_CompilerInterface *compiler_interface);
RAVICOMP_EXPORT void raviX_release(struct Ravi_CompilerInterface *compiler_interface);
RAVICOMP_EXPORT int raviX_compile(Ravi_CompilerInterface *compiler_interface);
RAVICOMP_EXPORT void raviX_release(Ravi_CompilerInterface *compiler_interface);

#endif
17 changes: 13 additions & 4 deletions src/chibicc/chibicc.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ typedef struct C_Member C_Member;
typedef struct C_Relocation C_Relocation;
typedef struct C_Hideset C_Hideset;
typedef struct C_Parser C_Parser;
typedef struct C_MemoryAllocator C_MemoryAllocator;

struct C_MemoryAllocator {
void *arena;
void *(*realloc)(void *arena, void* mem, size_t newsize);
void *(*calloc)(void *arena, size_t n_elements, size_t elem_size);
void (*free)(void *arena, void *p);
};


//
// strings.c
Expand All @@ -75,7 +84,7 @@ typedef struct {
int len;
} StringArray;

void strarray_push(mspace arena, StringArray *arr, char *s);
void strarray_push(C_MemoryAllocator *allocator, StringArray *arr, char *s);
char *format(char *fmt, ...) __attribute__((format(printf, 1, 2)));

//
Expand Down Expand Up @@ -322,7 +331,7 @@ typedef struct {
HashEntry *buckets;
int capacity;
int used;
mspace arena;
C_MemoryAllocator *allocator;
} HashMap;

// Represents a block scope.
Expand Down Expand Up @@ -387,7 +396,7 @@ struct C_Parser {

HashMap keywords; // used by tokenizer
HashMap typewords; // used by parser
mspace arena; // pointer to memory arena handle
C_MemoryAllocator *memory_allocator;

jmp_buf env; /* For error handling */

Expand All @@ -399,7 +408,7 @@ struct C_Parser {

};

void C_parser_init(C_Parser *parser);
void C_parser_init(C_Parser *parser, C_MemoryAllocator *memory_allocator);
C_Scope *C_global_scope(C_Parser *parser);
C_Node *C_new_cast(C_Parser *parser, C_Node *expr, C_Type *ty);
int64_t C_const_expr(C_Parser *parser, C_Token **rest, C_Token *tok);
Expand Down
10 changes: 5 additions & 5 deletions src/chibicc/chibicc_hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ static void rehash(HashMap *map) {

// Create a new hashmap and copy all key-values.
HashMap map2 = {0};
map2.arena = map->arena;
map2.buckets = mspace_calloc(map->arena, cap, sizeof(HashEntry));
map2.allocator = map->allocator;
map2.buckets = map->allocator->calloc(map->allocator->arena, cap, sizeof(HashEntry));
map2.capacity = cap;

for (int i = 0; i < map->capacity; i++) {
Expand All @@ -76,7 +76,7 @@ static void rehash(HashMap *map) {
}

assert(map2.used == nkeys);
mspace_free(map->arena, map->buckets);
map->allocator->free(map->allocator->arena, map->buckets);
*map = map2;
}

Expand Down Expand Up @@ -105,8 +105,8 @@ static HashEntry *get_entry(HashMap *map, char *key, int keylen) {

static HashEntry *get_or_insert_entry(HashMap *map, char *key, int keylen) {
if (!map->buckets) {
assert(map->arena != NULL);
map->buckets = mspace_calloc(map->arena, INIT_SIZE, sizeof(HashEntry));
assert(map->allocator != NULL);
map->buckets = map->allocator->calloc(map->allocator->arena, INIT_SIZE, sizeof(HashEntry));
map->capacity = INIT_SIZE;
} else if ((map->used * 100) / map->capacity >= HIGH_WATERMARK) {
rehash(map);
Expand Down

0 comments on commit 74d2aed

Please sign in to comment.