Skip to content

Commit

Permalink
Re-format code
Browse files Browse the repository at this point in the history
  • Loading branch information
justinethier committed Jan 18, 2024
1 parent b441987 commit 3b921e7
Show file tree
Hide file tree
Showing 12 changed files with 2,596 additions and 2,367 deletions.
241 changes: 120 additions & 121 deletions ck-polyfill.c

Large diffs are not rendered by default.

141 changes: 63 additions & 78 deletions ck-polyfill.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,70 @@

void ck_polyfill_init();

struct ck_malloc {
void *(*malloc)(size_t);
void *(*realloc)(void *, size_t, size_t, bool);
void (*free)(void *, size_t, bool);
};
struct ck_malloc {
void *(*malloc)(size_t);
void *(*realloc)(void *, size_t, size_t, bool);
void (*free)(void *, size_t, bool);
};

///////////////////////////////////////////////////////////////////////////////
// Simple hashset (hashset with string support)
/* hash function */
typedef size_t(*hash_func_t)(const char*, size_t);

struct simple_hashset_item_st {
size_t hash;
symbol_type* item;
};

struct simple_hashset_st {
size_t nbits;
size_t mask;

size_t capacity;
struct simple_hashset_item_st *items;
size_t nitems;
size_t n_deleted_items;

hash_func_t hash_func;
};
// struct simple_hashset_st;
typedef struct simple_hashset_st *simple_hashset_t;
typedef size_t (*hash_func_t)(const char *, size_t);

struct simple_hashset_item_st {
size_t hash;
symbol_type *item;
};

struct simple_hashset_st {
size_t nbits;
size_t mask;

size_t capacity;
struct simple_hashset_item_st *items;
size_t nitems;
size_t n_deleted_items;

struct hashmap_st;
typedef struct hashmap_st *hashmap_t;
hash_func_t hash_func;
};
// struct simple_hashset_st;
typedef struct simple_hashset_st *simple_hashset_t;

struct hashmap_st;
typedef struct hashmap_st *hashmap_t;

/*
* HASHSET FUNCTIONS
*/

/* create hashset instance */
simple_hashset_t simple_hashset_create(void);
simple_hashset_t simple_hashset_create(void);

/* destroy hashset instance */
void simple_hashset_destroy(simple_hashset_t set);
void simple_hashset_destroy(simple_hashset_t set);

/* set hash function */
void simple_hashset_set_hash_function(simple_hashset_t set, hash_func_t func);
void simple_hashset_set_hash_function(simple_hashset_t set, hash_func_t func);

/* add item into the hashset.
*
* @note 0 and 1 is special values, meaning nil and deleted items. the
* function will return -1 indicating error.
*
* returns zero if the item already in the set and non-zero otherwise
*/
int simple_hashset_add(simple_hashset_t set, symbol_type* key);
int simple_hashset_add(simple_hashset_t set, symbol_type * key);

/* check if existence of the item
*
* returns non-zero if the item exists and zero otherwise
*/
int simple_hashset_is_member(simple_hashset_t set, symbol_type* key);
int simple_hashset_is_member(simple_hashset_t set, symbol_type * key);

static inline uint64_t MurmurHash64A(const void *key, int len, uint64_t seed)
{
return 0;
{
return 0;
}

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -81,30 +80,31 @@ static inline uint64_t MurmurHash64A(const void *key, int len, uint64_t seed)
#define CK_HS_MODE_OBJECT 0
#define CK_HS_MODE_SPMC 0

struct ck_hs {
struct ck_hs {
pthread_mutex_t lock;
simple_hashset_t hs;
};
};

typedef struct ck_hs ck_hs_t;
typedef struct ck_hs ck_hs_t;

/*
* Hash callback function.
*/
typedef unsigned long ck_hs_hash_cb_t(const void *, unsigned long);
*/
typedef unsigned long ck_hs_hash_cb_t(const void *, unsigned long);

/*
* Returns pointer to object if objects are equivalent.
*/
typedef bool ck_hs_compare_cb_t(const void *, const void *);
*/
typedef bool ck_hs_compare_cb_t(const void *, const void *);

#define CK_HS_HASH(hs, hs_hash, value) 0

bool ck_hs_init(ck_hs_t *, unsigned int, ck_hs_hash_cb_t *,
ck_hs_compare_cb_t *, struct ck_malloc *, unsigned long, unsigned long);
bool ck_hs_init(ck_hs_t *, unsigned int, ck_hs_hash_cb_t *,
ck_hs_compare_cb_t *, struct ck_malloc *, unsigned long,
unsigned long);

void *ck_hs_get(ck_hs_t *, unsigned long, const void *);
bool ck_hs_put(ck_hs_t *, unsigned long, const void *);
void *ck_hs_get(ck_hs_t *, unsigned long, const void *);
bool ck_hs_put(ck_hs_t *, unsigned long, const void *);

/*
struct ck_hs {
Expand Down Expand Up @@ -150,8 +150,8 @@ typedef struct ck_array_iterator ck_array_iterator_t;
// returns false if the creation failed. Failure may occur due to internal
// memory allocation failures or invalid arguments.
bool
ck_array_init(ck_array_t *array, unsigned int mode,
struct ck_malloc *allocator, unsigned int initial_length);
ck_array_init(ck_array_t * array, unsigned int mode,
struct ck_malloc *allocator, unsigned int initial_length);

// DESCRIPTION
// The ck_array_put_unique(3) function will attempt to insert the value of
Expand All @@ -166,8 +166,7 @@ ck_array_init(ck_array_t *array, unsigned int mode,
// This function returns 1 if the pointer already exists in the array. It
// returns 0 if the put operation succeeded. It returns -1 on error due to
// internal memory allocation failures.
int
ck_array_put_unique(ck_array_t *array, void *pointer);
int ck_array_put_unique(ck_array_t * array, void *pointer);

// DESCRIPTION
// The ck_array_remove(3) function will attempt to remove the value of
Expand All @@ -180,9 +179,7 @@ ck_array_put_unique(ck_array_t *array, void *pointer);
// This function returns true if the remove operation succeeded. It will
// return false otherwise due to internal allocation failures or because the
// value did not exist.
bool
ck_array_remove(ck_array_t *array, void *pointer);

bool ck_array_remove(ck_array_t * array, void *pointer);

// DESCRIPTION
// The ck_array_commit(3) function will commit any pending put or remove
Expand All @@ -193,9 +190,7 @@ ck_array_remove(ck_array_t *array, void *pointer);
// RETURN VALUES
// This function returns true if the commit operation succeeded. It will
// return false otherwise, and pending operations will not be applied.
bool
ck_array_commit(ck_array_t *array);

bool ck_array_commit(ck_array_t * array);

// TODO:

Expand All @@ -209,37 +204,27 @@ ck_array_commit(ck_array_t *array);
if (tmpc > 0) { (*b) = tmp[0]; } \
for (unsigned int _ck_i = 0; \
_ck_i < tmpc; \
_ck_i++, (*b) = tmp[_ck_i])
_ck_i++, (*b) = tmp[_ck_i])

///////////////////////////////////////////////////////////////////////////////
// CK PR section
bool
ck_pr_cas_ptr(void *target, void *old_value, void *new_value);
bool ck_pr_cas_ptr(void *target, void *old_value, void *new_value);

bool
ck_pr_cas_int(int *target, int old_value, int new_value);

bool
ck_pr_cas_8(uint8_t *target, uint8_t old_value, uint8_t new_value);
bool ck_pr_cas_int(int *target, int old_value, int new_value);

bool ck_pr_cas_8(uint8_t * target, uint8_t old_value, uint8_t new_value);

void
ck_pr_add_ptr(void *target, uintptr_t delta);
void ck_pr_add_ptr(void *target, uintptr_t delta);

void
ck_pr_add_int(int *target, int delta);
void ck_pr_add_int(int *target, int delta);

void
ck_pr_add_8(uint8_t *target, uint8_t delta);
void ck_pr_add_8(uint8_t * target, uint8_t delta);

void *
ck_pr_load_ptr(const void *target);
void *ck_pr_load_ptr(const void *target);

int
ck_pr_load_int(const int *target);
int ck_pr_load_int(const int *target);

uint8_t
ck_pr_load_8(const uint8_t *target);
uint8_t ck_pr_load_8(const uint8_t * target);

void ck_pr_store_ptr(void *target, void *value);
#endif /* CYCLONE_CK_POLYFILL_H */
40 changes: 22 additions & 18 deletions ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
#include <ck_pr.h>
#include <unistd.h>

void *Cyc_init_thread(object thread_and_thunk, int argc, object *args);
void *Cyc_init_thread(object thread_and_thunk, int argc, object * args);

/**
* After the Scheme call finishes, we wind down the GC / Heap used
* for the call and perform a minor GC to ensure any returned object
* is on the heap and safe to use.
*/
static void Cyc_return_from_scm_call(void *data, object _, int argc, object *args)
static void Cyc_return_from_scm_call(void *data, object _, int argc,
object * args)
{
gc_thread_data *thd = data;
object result = args[0];
Expand All @@ -41,12 +42,13 @@ static void Cyc_return_from_scm_call(void *data, object _, int argc, object *arg
* We store results and longjmp back to where we started, at the
* bottom of the trampoline (we only jump once).
*/
static void Cyc_after_scm_call(void *data, object _, int argc, object *args)
static void Cyc_after_scm_call(void *data, object _, int argc, object * args)
{
gc_thread_data *thd = data;
object result = args[0];
mclosure0(clo, Cyc_return_from_scm_call);
object buf[1]; buf[0] = result;
object buf[1];
buf[0] = result;
GC(thd, &clo, buf, 1);
}

Expand All @@ -58,21 +60,22 @@ static void Cyc_after_scm_call(void *data, object _, int argc, object *args)
* can do anything "normal" Scheme code does, and any returned
* objects will be on the heap and available for use by the caller.
*/
object Cyc_scm_call(gc_thread_data *parent_thd, object fnc, int argc, object *args)
object Cyc_scm_call(gc_thread_data * parent_thd, object fnc, int argc,
object * args)
{
jmp_buf l;
gc_thread_data local;
local.gc_cont = NULL;
local.jmp_start = &l;

gc_thread_data *td = malloc(sizeof(gc_thread_data));
gc_add_new_unrunning_mutator(td); /* Register this thread */
gc_add_new_unrunning_mutator(td); /* Register this thread */
make_c_opaque(co, td);
make_utf8_string(NULL, name_str, "");

make_c_opaque(co_parent_thd, parent_thd);
make_c_opaque(co_this_thd, &local);
mclosure0(after, (function_type)Cyc_after_scm_call);
mclosure0(after, (function_type) Cyc_after_scm_call);

make_empty_vector(vec);
vec.num_elements = 7;
Expand All @@ -81,11 +84,11 @@ object Cyc_scm_call(gc_thread_data *parent_thd, object fnc, int argc, object *ar
vec.elements[1] = fnc;
vec.elements[2] = &co;
vec.elements[3] = &name_str;
vec.elements[4] = &co_this_thd; //boolean_f;
vec.elements[4] = &co_this_thd; //boolean_f;
vec.elements[5] = &co_parent_thd;
vec.elements[6] = &after;

make_pair(thread_and_thunk, &vec, fnc); // TODO: OK we are not clearing vec[5]? I think so...
make_pair(thread_and_thunk, &vec, fnc); // TODO: OK we are not clearing vec[5]? I think so...

if (!setjmp(*(local.jmp_start))) {
Cyc_init_thread(&thread_and_thunk, argc, args);
Expand All @@ -105,7 +108,8 @@ object Cyc_scm_call(gc_thread_data *parent_thd, object fnc, int argc, object *ar
* We store results and longjmp back to where we started, at the
* bottom of the trampoline (we only jump once).
*/
static void no_gc_after_call_scm(gc_thread_data *thd, object _, int argc, object *args)
static void no_gc_after_call_scm(gc_thread_data * thd, object _, int argc,
object * args)
{
object result = args[0];
thd->gc_cont = result;
Expand All @@ -115,11 +119,11 @@ static void no_gc_after_call_scm(gc_thread_data *thd, object _, int argc, object
/**
* Call into Scheme function
*/
static void no_gc_call_scm(gc_thread_data *thd, object fnc, object obj)
static void no_gc_call_scm(gc_thread_data * thd, object fnc, object obj)
{
mclosure0(after, (function_type)no_gc_after_call_scm);
object buf[2] = {&after, obj};
((closure)fnc)->fn(thd, fnc, 2, buf);
mclosure0(after, (function_type) no_gc_after_call_scm);
object buf[2] = { &after, obj };
((closure) fnc)->fn(thd, fnc, 2, buf);
}

/**
Expand All @@ -134,12 +138,12 @@ static void no_gc_call_scm(gc_thread_data *thd, object fnc, object obj)
* or re-allocated (EG: malloc) before returning it
* to the C layer.
*/
object Cyc_scm_call_no_gc(gc_thread_data *parent_thd, object fnc, object arg)
object Cyc_scm_call_no_gc(gc_thread_data * parent_thd, object fnc, object arg)
{
long stack_size = 100000;
char *stack_base = (char *)&stack_size;
char *stack_traces[MAX_STACK_TRACES];
gc_thread_data thd = {0};
gc_thread_data thd = { 0 };
jmp_buf jmp;
thd.jmp_start = &jmp;
thd.stack_start = stack_base;
Expand All @@ -154,7 +158,7 @@ object Cyc_scm_call_no_gc(gc_thread_data *parent_thd, object fnc, object arg)
thd.thread_state = CYC_THREAD_STATE_RUNNABLE;

// Copy parameter objects from the calling thread
object parent = parent_thd->param_objs; // Unbox parent thread's data
object parent = parent_thd->param_objs; // Unbox parent thread's data
object child = NULL;
while (parent) {
if (thd.param_objs == NULL) {
Expand Down Expand Up @@ -184,5 +188,5 @@ object Cyc_scm_call_no_gc(gc_thread_data *parent_thd, object fnc, object arg)
no_gc_call_scm(&thd, fnc, arg);
}

return(thd.gc_cont);
return (thd.gc_cont);
}
Loading

0 comments on commit 3b921e7

Please sign in to comment.