Re-format code

This commit is contained in:
Justin Ethier 2024-01-17 19:43:47 -08:00
parent b44198744b
commit 3b921e7389
12 changed files with 2586 additions and 2357 deletions

View file

@ -28,7 +28,8 @@ void ck_polyfill_init()
// CK Hashset section // CK Hashset section
bool ck_hs_init(ck_hs_t * hs, unsigned int mode, ck_hs_hash_cb_t * hash_func, bool ck_hs_init(ck_hs_t * hs, unsigned int mode, ck_hs_hash_cb_t * hash_func,
ck_hs_compare_cb_t *cmp, struct ck_malloc *alloc, unsigned long capacity, unsigned long seed) ck_hs_compare_cb_t * cmp, struct ck_malloc *alloc,
unsigned long capacity, unsigned long seed)
{ {
(*hs).hs = simple_hashset_create(); (*hs).hs = simple_hashset_create();
if (pthread_mutex_init(&((*hs).lock), NULL) != 0) { if (pthread_mutex_init(&((*hs).lock), NULL) != 0) {
@ -101,8 +102,7 @@ ck_array_init(ck_array_t *array, unsigned int mode,
// This function returns 1 if the pointer already exists in the array. It // 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 // returns 0 if the put operation succeeded. It returns -1 on error due to
// internal memory allocation failures. // internal memory allocation failures.
int int ck_array_put_unique(ck_array_t * array, void *pointer)
ck_array_put_unique(ck_array_t *array, void *pointer)
{ {
pthread_mutex_lock(&(array->lock)); pthread_mutex_lock(&(array->lock));
hashset_add(array->hs, pointer); hashset_add(array->hs, pointer);
@ -121,8 +121,8 @@ ck_array_put_unique(ck_array_t *array, void *pointer)
// This function returns true if the remove operation succeeded. It will // This function returns true if the remove operation succeeded. It will
// return false otherwise due to internal allocation failures or because the // return false otherwise due to internal allocation failures or because the
// value did not exist. // value did not exist.
bool bool ck_array_remove(ck_array_t * array, void *pointer)
ck_array_remove(ck_array_t *array, void *pointer){ {
pthread_mutex_lock(&(array->lock)); pthread_mutex_lock(&(array->lock));
hashset_remove(array->hs, pointer); hashset_remove(array->hs, pointer);
pthread_mutex_unlock(&(array->lock)); pthread_mutex_unlock(&(array->lock));
@ -138,12 +138,12 @@ ck_array_remove(ck_array_t *array, void *pointer){
// RETURN VALUES // RETURN VALUES
// This function returns true if the commit operation succeeded. It will // This function returns true if the commit operation succeeded. It will
// return false otherwise, and pending operations will not be applied. // 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)
{
// Nothing to do in this polyfill // Nothing to do in this polyfill
return true; return true;
} }
// TODO: global pthread mutex lock for this? obviously not ideal but the // TODO: global pthread mutex lock for this? obviously not ideal but the
// whole purpose of this module is a minimal interface for compatibility // whole purpose of this module is a minimal interface for compatibility
// not speed // not speed
@ -185,8 +185,7 @@ bool ck_pr_cas_8(uint8_t *target, uint8_t old_value, uint8_t new_value)
return result; return result;
} }
void void ck_pr_add_ptr(void *target, uintptr_t delta)
ck_pr_add_ptr(void *target, uintptr_t delta)
{ {
pthread_mutex_lock(&glock); pthread_mutex_lock(&glock);
size_t value = (size_t)target; size_t value = (size_t)target;
@ -197,24 +196,21 @@ ck_pr_add_ptr(void *target, uintptr_t delta)
pthread_mutex_unlock(&glock); pthread_mutex_unlock(&glock);
} }
void void ck_pr_add_int(int *target, int delta)
ck_pr_add_int(int *target, int delta)
{ {
pthread_mutex_lock(&glock); pthread_mutex_lock(&glock);
(*target) += delta; (*target) += delta;
pthread_mutex_unlock(&glock); pthread_mutex_unlock(&glock);
} }
void void ck_pr_add_8(uint8_t * target, uint8_t delta)
ck_pr_add_8(uint8_t *target, uint8_t delta)
{ {
pthread_mutex_lock(&glock); pthread_mutex_lock(&glock);
(*target) += delta; (*target) += delta;
pthread_mutex_unlock(&glock); pthread_mutex_unlock(&glock);
} }
void * void *ck_pr_load_ptr(const void *target)
ck_pr_load_ptr(const void *target)
{ {
void *result; void *result;
pthread_mutex_lock(&glock); pthread_mutex_lock(&glock);
@ -223,8 +219,7 @@ ck_pr_load_ptr(const void *target)
return result; return result;
} }
int int ck_pr_load_int(const int *target)
ck_pr_load_int(const int *target)
{ {
int result; int result;
pthread_mutex_lock(&glock); pthread_mutex_lock(&glock);
@ -233,8 +228,7 @@ ck_pr_load_int(const int *target)
return result; return result;
} }
uint8_t uint8_t ck_pr_load_8(const uint8_t * target)
ck_pr_load_8(const uint8_t *target)
{ {
uint8_t result; uint8_t result;
pthread_mutex_lock(&glock); pthread_mutex_lock(&glock);
@ -250,13 +244,13 @@ void ck_pr_store_ptr(void *target, void *value)
pthread_mutex_unlock(&glock); pthread_mutex_unlock(&glock);
} }
// Simple hashset // Simple hashset
static const size_t prime_1 = 73; static const size_t prime_1 = 73;
static const size_t prime_2 = 5009; static const size_t prime_2 = 5009;
size_t hash_function(const char* str, size_t len) { size_t hash_function(const char *str, size_t len)
{
unsigned long hash = 5381; unsigned long hash = 5381;
int c; int c;
@ -269,7 +263,8 @@ size_t hash_function(const char* str, size_t len) {
simple_hashset_t simple_hashset_create() simple_hashset_t simple_hashset_create()
{ {
simple_hashset_t set = (simple_hashset_t)calloc(1, sizeof(struct simple_hashset_st)); simple_hashset_t set =
(simple_hashset_t) calloc(1, sizeof(struct simple_hashset_st));
if (set == NULL) { if (set == NULL) {
return NULL; return NULL;
@ -279,7 +274,10 @@ simple_hashset_t simple_hashset_create()
set->nbits = 3; set->nbits = 3;
set->capacity = (size_t)(1 << set->nbits); set->capacity = (size_t)(1 << set->nbits);
set->mask = set->capacity - 1; set->mask = set->capacity - 1;
set->items = (struct simple_hashset_item_st*)calloc(set->capacity, sizeof(struct simple_hashset_item_st)); set->items =
(struct simple_hashset_item_st *)calloc(set->capacity,
sizeof(struct
simple_hashset_item_st));
if (set->items == NULL) { if (set->items == NULL) {
simple_hashset_destroy(set); simple_hashset_destroy(set);
return NULL; return NULL;
@ -302,7 +300,8 @@ void simple_hashset_set_hash_function(simple_hashset_t set, hash_func_t func)
set->hash_func = func; set->hash_func = func;
} }
static int simple_hashset_add_member(simple_hashset_t set, symbol_type* key, size_t hash) static int simple_hashset_add_member(simple_hashset_t set, symbol_type * key,
size_t hash)
{ {
size_t index; size_t index;
@ -315,8 +314,7 @@ static int simple_hashset_add_member(simple_hashset_t set, symbol_type* key, siz
while (set->items[index].hash != 0 && set->items[index].hash != 1) { while (set->items[index].hash != 0 && set->items[index].hash != 1) {
if (set->items[index].hash == hash) { if (set->items[index].hash == hash) {
return 0; return 0;
} } else {
else {
/* search free slot */ /* search free slot */
index = set->mask & (index + prime_2); index = set->mask & (index + prime_2);
} }
@ -337,19 +335,22 @@ static void set_maybe_rehash(simple_hashset_t set)
struct simple_hashset_item_st *old_items; struct simple_hashset_item_st *old_items;
size_t old_capacity, index; size_t old_capacity, index;
if (set->nitems + set->n_deleted_items >= (double)set->capacity * 0.85) { if (set->nitems + set->n_deleted_items >= (double)set->capacity * 0.85) {
old_items = set->items; old_items = set->items;
old_capacity = set->capacity; old_capacity = set->capacity;
++set->nbits; ++set->nbits;
set->capacity = (size_t)(1 << set->nbits); set->capacity = (size_t)(1 << set->nbits);
set->mask = set->capacity - 1; set->mask = set->capacity - 1;
set->items = (struct simple_hashset_item_st*)calloc(set->capacity, sizeof(struct simple_hashset_item_st)); set->items =
(struct simple_hashset_item_st *)calloc(set->capacity,
sizeof(struct
simple_hashset_item_st));
set->nitems = 0; set->nitems = 0;
set->n_deleted_items = 0; set->n_deleted_items = 0;
//assert(set->items); //assert(set->items);
for (index = 0; index < old_capacity; ++index) { for (index = 0; index < old_capacity; ++index) {
simple_hashset_add_member(set, old_items[index].item, old_items[index].hash); simple_hashset_add_member(set, old_items[index].item,
old_items[index].hash);
} }
free(old_items); free(old_items);
} }
@ -379,5 +380,3 @@ int simple_hashset_is_member(simple_hashset_t set, symbol_type* key)
} }
return 0; return 0;
} }

View file

@ -38,7 +38,6 @@ struct ck_malloc {
// struct simple_hashset_st; // struct simple_hashset_st;
typedef struct simple_hashset_st *simple_hashset_t; typedef struct simple_hashset_st *simple_hashset_t;
struct hashmap_st; struct hashmap_st;
typedef struct hashmap_st *hashmap_t; typedef struct hashmap_st *hashmap_t;
@ -101,7 +100,8 @@ typedef bool ck_hs_compare_cb_t(const void *, const void *);
#define CK_HS_HASH(hs, hs_hash, value) 0 #define CK_HS_HASH(hs, hs_hash, value) 0
bool ck_hs_init(ck_hs_t *, unsigned int, ck_hs_hash_cb_t *, 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); ck_hs_compare_cb_t *, struct ck_malloc *, unsigned long,
unsigned long);
void *ck_hs_get(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 *); bool ck_hs_put(ck_hs_t *, unsigned long, const void *);
@ -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 // 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 // returns 0 if the put operation succeeded. It returns -1 on error due to
// internal memory allocation failures. // internal memory allocation failures.
int int ck_array_put_unique(ck_array_t * array, void *pointer);
ck_array_put_unique(ck_array_t *array, void *pointer);
// DESCRIPTION // DESCRIPTION
// The ck_array_remove(3) function will attempt to remove the value of // The ck_array_remove(3) function will attempt to remove the value of
@ -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 // This function returns true if the remove operation succeeded. It will
// return false otherwise due to internal allocation failures or because the // return false otherwise due to internal allocation failures or because the
// value did not exist. // value did not exist.
bool bool ck_array_remove(ck_array_t * array, void *pointer);
ck_array_remove(ck_array_t *array, void *pointer);
// DESCRIPTION // DESCRIPTION
// The ck_array_commit(3) function will commit any pending put or remove // The ck_array_commit(3) function will commit any pending put or remove
@ -193,9 +190,7 @@ ck_array_remove(ck_array_t *array, void *pointer);
// RETURN VALUES // RETURN VALUES
// This function returns true if the commit operation succeeded. It will // This function returns true if the commit operation succeeded. It will
// return false otherwise, and pending operations will not be applied. // return false otherwise, and pending operations will not be applied.
bool bool ck_array_commit(ck_array_t * array);
ck_array_commit(ck_array_t *array);
// TODO: // TODO:
@ -213,33 +208,23 @@ ck_array_commit(ck_array_t *array);
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// CK PR section // CK PR section
bool bool ck_pr_cas_ptr(void *target, void *old_value, void *new_value);
ck_pr_cas_ptr(void *target, void *old_value, void *new_value);
bool bool ck_pr_cas_int(int *target, int old_value, int new_value);
ck_pr_cas_int(int *target, int old_value, int new_value);
bool bool ck_pr_cas_8(uint8_t * target, uint8_t old_value, uint8_t new_value);
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 void ck_pr_add_int(int *target, int delta);
ck_pr_add_ptr(void *target, uintptr_t delta);
void void ck_pr_add_8(uint8_t * target, uint8_t delta);
ck_pr_add_int(int *target, int delta);
void void *ck_pr_load_ptr(const void *target);
ck_pr_add_8(uint8_t *target, uint8_t delta);
void * int ck_pr_load_int(const int *target);
ck_pr_load_ptr(const void *target);
int uint8_t ck_pr_load_8(const uint8_t * target);
ck_pr_load_int(const int *target);
uint8_t
ck_pr_load_8(const uint8_t *target);
void ck_pr_store_ptr(void *target, void *value); void ck_pr_store_ptr(void *target, void *value);
#endif /* CYCLONE_CK_POLYFILL_H */ #endif /* CYCLONE_CK_POLYFILL_H */

12
ffi.c
View file

@ -20,7 +20,8 @@ void *Cyc_init_thread(object thread_and_thunk, int argc, object *args);
* for the call and perform a minor GC to ensure any returned object * for the call and perform a minor GC to ensure any returned object
* is on the heap and safe to use. * 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; gc_thread_data *thd = data;
object result = args[0]; object result = args[0];
@ -46,7 +47,8 @@ static void Cyc_after_scm_call(void *data, object _, int argc, object *args)
gc_thread_data *thd = data; gc_thread_data *thd = data;
object result = args[0]; object result = args[0];
mclosure0(clo, Cyc_return_from_scm_call); mclosure0(clo, Cyc_return_from_scm_call);
object buf[1]; buf[0] = result; object buf[1];
buf[0] = result;
GC(thd, &clo, buf, 1); GC(thd, &clo, buf, 1);
} }
@ -58,7 +60,8 @@ static void Cyc_after_scm_call(void *data, object _, int argc, object *args)
* can do anything "normal" Scheme code does, and any returned * can do anything "normal" Scheme code does, and any returned
* objects will be on the heap and available for use by the caller. * 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; jmp_buf l;
gc_thread_data local; gc_thread_data local;
@ -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 * We store results and longjmp back to where we started, at the
* bottom of the trampoline (we only jump once). * 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]; object result = args[0];
thd->gc_cont = result; thd->gc_cont = result;

149
gc.c
View file

@ -158,13 +158,17 @@ static void mark_buffer_free(mark_buffer *mb)
const int NUM_ALLOC_SIZES = 10; const int NUM_ALLOC_SIZES = 10;
static double allocated_size_counts[10] = { static double allocated_size_counts[10] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,0,0,0,0}; 0, 0, 0, 0, 0
};
static double allocated_obj_counts[25] = { static double allocated_obj_counts[25] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,0,0,0,0}; 0, 0, 0, 0, 0
};
// TODO: allocated object sizes (EG: 32, 64, etc). // TODO: allocated object sizes (EG: 32, 64, etc).
static double allocated_heap_counts[4] = { 0, 0, 0, 0 }; static double allocated_heap_counts[4] = { 0, 0, 0, 0 };
@ -371,7 +375,8 @@ void gc_free_old_thread_data()
* @param gc_heap Root of the heap * @param gc_heap Root of the heap
* @return Free space in bytes * @return Free space in bytes
*/ */
uint64_t gc_heap_free_size(gc_heap *h) { uint64_t gc_heap_free_size(gc_heap * h)
{
uint64_t free_size = 0; uint64_t free_size = 0;
for (; h; h = h->next) { for (; h; h = h->next) {
if (h->is_unswept == 1) { // Assume all free prior to sweep if (h->is_unswept == 1) { // Assume all free prior to sweep
@ -494,13 +499,15 @@ void gc_print_fixed_size_free_list(gc_heap *h)
* @brief Essentially this is half of the sweep code, for sweeping bump&pop * @brief Essentially this is half of the sweep code, for sweeping bump&pop
* @param h Heap page to convert * @param h Heap page to convert
*/ */
static size_t gc_convert_heap_page_to_free_list(gc_heap *h, gc_thread_data *thd) static size_t gc_convert_heap_page_to_free_list(gc_heap * h,
gc_thread_data * thd)
{ {
size_t freed = 0; size_t freed = 0;
object p; object p;
gc_free_list *next; gc_free_list *next;
int remaining = h->size - (h->size % h->block_size); int remaining = h->size - (h->size % h->block_size);
if (h->data_end == NULL) return 0; // Already converted if (h->data_end == NULL)
return 0; // Already converted
next = h->free_list = NULL; next = h->free_list = NULL;
while (remaining > h->remaining) { while (remaining > h->remaining) {
@ -509,8 +516,7 @@ static size_t gc_convert_heap_page_to_free_list(gc_heap *h, gc_thread_data *thd)
int color = mark(p); int color = mark(p);
// printf("found object %d color %d at %p with remaining=%lu\n", tag, color, p, remaining); // printf("found object %d color %d at %p with remaining=%lu\n", tag, color, p, remaining);
// free space, add it to the free list // free space, add it to the free list
if (color != thd->gc_alloc_color && if (color != thd->gc_alloc_color && color != thd->gc_trace_color) { //gc_color_clear)
color != thd->gc_trace_color) { //gc_color_clear)
// Run any finalizers // Run any finalizers
if (type_of(p) == mutex_tag) { if (type_of(p) == mutex_tag) {
#if GC_DEBUG_VERBOSE #if GC_DEBUG_VERBOSE
@ -546,8 +552,7 @@ static size_t gc_convert_heap_page_to_free_list(gc_heap *h, gc_thread_data *thd)
freed += h->block_size; freed += h->block_size;
if (next == NULL) { if (next == NULL) {
next = h->free_list = p; next = h->free_list = p;
} } else {
else {
next->next = p; next->next = p;
next = next->next; next = next->next;
} }
@ -562,8 +567,7 @@ static size_t gc_convert_heap_page_to_free_list(gc_heap *h, gc_thread_data *thd)
// printf("no object at %p fill with free list\n", p); // printf("no object at %p fill with free list\n", p);
if (next == NULL) { if (next == NULL) {
next = h->free_list = p; next = h->free_list = p;
} } else {
else {
next->next = p; //(gc_free_list *)(((char *) next) + h->block_size); next->next = p; //(gc_free_list *)(((char *) next) + h->block_size);
next = next->next; next = next->next;
} }
@ -622,7 +626,8 @@ static gc_heap *gc_sweep_fixed_size(gc_heap * h, gc_thread_data *thd)
q = h->free_list; q = h->free_list;
while (p < end) { while (p < end) {
// find preceding/succeeding free list pointers for p // find preceding/succeeding free list pointers for p
for (r = (q?q->next:NULL); r && ((char *)r < (char *)p); q = r, r = r->next) ; for (r = (q ? q->next : NULL); r && ((char *)r < (char *)p);
q = r, r = r->next) ;
if ((char *)q == (char *)p || (char *)r == (char *)p) { // this is a free block, skip it if ((char *)q == (char *)p || (char *)r == (char *)p) { // this is a free block, skip it
//printf("Sweep skip free block %p remaining=%lu\n", p, remaining); //printf("Sweep skip free block %p remaining=%lu\n", p, remaining);
p = (object) (((char *)p) + h->block_size); p = (object) (((char *)p) + h->block_size);
@ -638,8 +643,7 @@ static gc_heap *gc_sweep_fixed_size(gc_heap * h, gc_thread_data *thd)
exit(1); exit(1);
} }
#endif #endif
if (mark(p) != thd->gc_alloc_color && if (mark(p) != thd->gc_alloc_color && mark(p) != thd->gc_trace_color) { //gc_color_clear)
mark(p) != thd->gc_trace_color) { //gc_color_clear)
#if GC_DEBUG_VERBOSE #if GC_DEBUG_VERBOSE
fprintf(stderr, "sweep is freeing unmarked obj: %p with tag %d\n", p, fprintf(stderr, "sweep is freeing unmarked obj: %p with tag %d\n", p,
type_of(p)); type_of(p));
@ -738,7 +742,8 @@ gc_heap *gc_heap_free(gc_heap *page, gc_heap *prev_page)
return NULL; return NULL;
} }
#if GC_DEBUG_TRACE #if GC_DEBUG_TRACE
fprintf(stderr, "DEBUG freeing heap type %d page at addr: %p\n", page->type, page); fprintf(stderr, "DEBUG freeing heap type %d page at addr: %p\n", page->type,
page);
#endif #endif
prev_page->next = page->next; prev_page->next = page->next;
@ -754,16 +759,19 @@ gc_heap *gc_heap_free(gc_heap *page, gc_heap *prev_page)
static int gc_is_heap_empty(gc_heap * h) static int gc_is_heap_empty(gc_heap * h)
{ {
gc_free_list *f; gc_free_list *f;
if (!h) return 0; if (!h)
return 0;
if (h->data_end) { // Fixed-size bump&pop if (h->data_end) { // Fixed-size bump&pop
return (h->remaining == (h->size - (h->size % h->block_size))); return (h->remaining == (h->size - (h->size % h->block_size)));
} }
if (!h->free_list) return 0; if (!h->free_list)
return 0;
f = h->free_list; f = h->free_list;
if (f->size != 0 || !f->next) return 0; if (f->size != 0 || !f->next)
return 0;
f = f->next; f = f->next;
return (f->size + gc_heap_align(gc_free_chunk_size)) == h->size; return (f->size + gc_heap_align(gc_free_chunk_size)) == h->size;
@ -798,7 +806,8 @@ void gc_print_stats(gc_heap * h)
heap_is_empty = gc_is_heap_empty(h); heap_is_empty = gc_is_heap_empty(h);
fprintf(stderr, fprintf(stderr,
"Heap type=%d, page size=%u, is empty=%d, used=%u, free=%u, free chunks=%u, min=%u, max=%u\n", "Heap type=%d, page size=%u, is empty=%d, used=%u, free=%u, free chunks=%u, min=%u, max=%u\n",
h->type, h->size, heap_is_empty, h->size - free, free, free_chunks, free_min, free_max); h->type, h->size, heap_is_empty, h->size - free, free, free_chunks,
free_min, free_max);
} }
} }
@ -827,7 +836,8 @@ char *gc_copy_obj(object dest, char *obj, gc_thread_data * thd)
hp->num_args = ((closureN) obj)->num_args; hp->num_args = ((closureN) obj)->num_args;
hp->num_elements = ((closureN) obj)->num_elements; hp->num_elements = ((closureN) obj)->num_elements;
hp->elements = (object *) (((char *)hp) + sizeof(closureN_type)); hp->elements = (object *) (((char *)hp) + sizeof(closureN_type));
memcpy(hp->elements, ((closureN)obj)->elements, sizeof(object *) * hp->num_elements); memcpy(hp->elements, ((closureN) obj)->elements,
sizeof(object *) * hp->num_elements);
return (char *)hp; return (char *)hp;
} }
case pair_tag:{ case pair_tag:{
@ -866,7 +876,8 @@ char *gc_copy_obj(object dest, char *obj, gc_thread_data * thd)
type_of(hp) = vector_tag; type_of(hp) = vector_tag;
hp->num_elements = ((vector) obj)->num_elements; hp->num_elements = ((vector) obj)->num_elements;
hp->elements = (object *) (((char *)hp) + sizeof(vector_type)); hp->elements = (object *) (((char *)hp) + sizeof(vector_type));
memcpy(hp->elements, ((vector)obj)->elements, sizeof(object *) * hp->num_elements); memcpy(hp->elements, ((vector) obj)->elements,
sizeof(object *) * hp->num_elements);
return (char *)hp; return (char *)hp;
} }
case bytevector_tag:{ case bytevector_tag:{
@ -1049,8 +1060,7 @@ gc_heap *gc_grow_heap(gc_heap * h, size_t size, gc_thread_data *thd)
new_size = HEAP_SIZE; new_size = HEAP_SIZE;
} }
#if GC_DEBUG_TRACE #if GC_DEBUG_TRACE
fprintf(stderr, "Growing heap %d new page size = %zu\n", h->type, fprintf(stderr, "Growing heap %d new page size = %zu\n", h->type, new_size);
new_size);
#endif #endif
} }
h_last = gc_heap_last(h_last); // Ensure we don't unlink any heaps h_last = gc_heap_last(h_last); // Ensure we don't unlink any heaps
@ -1074,8 +1084,7 @@ gc_heap *gc_grow_heap(gc_heap * h, size_t size, gc_thread_data *thd)
* This function will fail if there is no space on the heap for the * This function will fail if there is no space on the heap for the
* requested object. * requested object.
*/ */
void *gc_try_alloc(gc_heap * h, size_t size, char *obj, void *gc_try_alloc(gc_heap * h, size_t size, char *obj, gc_thread_data * thd)
gc_thread_data * thd)
{ {
gc_free_list *f1, *f2, *f3; gc_free_list *f1, *f2, *f3;
@ -1128,7 +1137,8 @@ int gc_num_unswept_heaps(gc_heap *h)
return count; return count;
} }
void gc_start_major_collection(gc_thread_data *thd){ void gc_start_major_collection(gc_thread_data * thd)
{
if (ck_pr_load_int(&gc_stage) == STAGE_RESTING) { if (ck_pr_load_int(&gc_stage) == STAGE_RESTING) {
#if GC_DEBUG_TRACE #if GC_DEBUG_TRACE
gc_log(stderr, "gc_start_major_collection - initiating collector"); gc_log(stderr, "gc_start_major_collection - initiating collector");
@ -1137,7 +1147,8 @@ void gc_start_major_collection(gc_thread_data *thd){
} }
} }
void *gc_try_alloc_slow(gc_heap *h_passed, gc_heap *h, size_t size, char *obj, gc_thread_data *thd) void *gc_try_alloc_slow(gc_heap * h_passed, gc_heap * h, size_t size, char *obj,
gc_thread_data * thd)
{ {
#ifdef CYC_HIGH_RES_TIMERS #ifdef CYC_HIGH_RES_TIMERS
long long tstamp = hrt_get_current(); long long tstamp = hrt_get_current();
@ -1215,7 +1226,8 @@ hrt_log_delta("gc sweep", tstamp);
* This function will fail if there is no space on the heap for the * This function will fail if there is no space on the heap for the
* requested object. * requested object.
*/ */
static void *gc_try_alloc_fixed_size(gc_heap * h, size_t size, char *obj, gc_thread_data * thd) static void *gc_try_alloc_fixed_size(gc_heap * h, size_t size, char *obj,
gc_thread_data * thd)
{ {
void *result; void *result;
@ -1245,7 +1257,8 @@ static void *gc_try_alloc_fixed_size(gc_heap * h, size_t size, char *obj, gc_thr
return NULL; return NULL;
} }
void *gc_try_alloc_slow_fixed_size(gc_heap *h_passed, gc_heap *h, size_t size, char *obj, gc_thread_data *thd) void *gc_try_alloc_slow_fixed_size(gc_heap * h_passed, gc_heap * h, size_t size,
char *obj, gc_thread_data * thd)
{ {
#ifdef CYC_HIGH_RES_TIMERS #ifdef CYC_HIGH_RES_TIMERS
long long tstamp = hrt_get_current(); long long tstamp = hrt_get_current();
@ -1322,11 +1335,11 @@ void *gc_alloc_bignum(gc_thread_data *data)
//tmp.hdr.mark = gc_color_red; //tmp.hdr.mark = gc_color_red;
//tmp.hdr.grayed = 0; //tmp.hdr.grayed = 0;
tmp.tag = bignum_tag; tmp.tag = bignum_tag;
bn = gc_alloc(((gc_thread_data *)data)->heap, sizeof(bignum_type), (char *)(&tmp), (gc_thread_data *)data, &heap_grown); bn = gc_alloc(((gc_thread_data *) data)->heap, sizeof(bignum_type),
(char *)(&tmp), (gc_thread_data *) data, &heap_grown);
if ((result = mp_init(&bignum_value(bn))) != MP_OKAY) { if ((result = mp_init(&bignum_value(bn))) != MP_OKAY) {
fprintf(stderr, "Error initializing number %s", fprintf(stderr, "Error initializing number %s", mp_error_to_string(result));
mp_error_to_string(result));
exit(1); exit(1);
} }
return bn; return bn;
@ -1341,10 +1354,10 @@ void *gc_alloc_bignum(gc_thread_data *data)
void *gc_alloc_from_bignum(gc_thread_data * data, bignum_type * src) void *gc_alloc_from_bignum(gc_thread_data * data, bignum_type * src)
{ {
int heap_grown; int heap_grown;
return gc_alloc(((gc_thread_data *)data)->heap, sizeof(bignum_type), (char *)(src), (gc_thread_data *)data, &heap_grown); return gc_alloc(((gc_thread_data *) data)->heap, sizeof(bignum_type),
(char *)(src), (gc_thread_data *) data, &heap_grown);
} }
/** /**
* @brief Allocate memory on the heap for an object * @brief Allocate memory on the heap for an object
* @param hrt The root of the heap to allocate from * @param hrt The root of the heap to allocate from
@ -1365,7 +1378,8 @@ void *gc_alloc(gc_heap_root * hrt, size_t size, char *obj, gc_thread_data * thd,
gc_heap *h_passed, *h = NULL; gc_heap *h_passed, *h = NULL;
int heap_type; int heap_type;
void *(*try_alloc)(gc_heap * h, size_t size, char *obj, gc_thread_data * thd); void *(*try_alloc)(gc_heap * h, size_t size, char *obj, gc_thread_data * thd);
void *(*try_alloc_slow)(gc_heap *h_passed, gc_heap *h, size_t size, char *obj, gc_thread_data *thd); void *(*try_alloc_slow)(gc_heap * h_passed, gc_heap * h, size_t size,
char *obj, gc_thread_data * thd);
size = gc_heap_align(size); size = gc_heap_align(size);
if (size <= (32 * (LAST_FIXED_SIZE_HEAP_TYPE + 1))) { if (size <= (32 * (LAST_FIXED_SIZE_HEAP_TYPE + 1))) {
heap_type = (size - 1) / 32; heap_type = (size - 1) / 32;
@ -1404,7 +1418,8 @@ fprintf(stderr, "slow alloc of %p\n", result);
if (result) { if (result) {
// Check if we need to start a major collection // Check if we need to start a major collection
if (heap_type != HEAP_HUGE && if (heap_type != HEAP_HUGE &&
(h_passed->num_unswept_children < GC_COLLECT_UNDER_UNSWEPT_HEAP_COUNT)) { (h_passed->num_unswept_children <
GC_COLLECT_UNDER_UNSWEPT_HEAP_COUNT)) {
gc_start_major_collection(thd); gc_start_major_collection(thd);
} }
} else { } else {
@ -1444,8 +1459,9 @@ fprintf(stderr, "slowest alloc of %p\n", result);
#endif #endif
#if GC_DEBUG_VERBOSE #if GC_DEBUG_VERBOSE
fprintf(stderr, "alloc %p size = %zu, obj=%p, tag=%d, mark=%d, thd->alloc=%d, thd->trace=%d\n", result, fprintf(stderr,
size, obj, type_of(obj), mark(((object) result)), "alloc %p size = %zu, obj=%p, tag=%d, mark=%d, thd->alloc=%d, thd->trace=%d\n",
result, size, obj, type_of(obj), mark(((object) result)),
thd->gc_alloc_color, thd->gc_trace_color); thd->gc_alloc_color, thd->gc_trace_color);
// Debug check, should no longer be necessary // Debug check, should no longer be necessary
//if (is_value_type(result)) { //if (is_value_type(result)) {
@ -1641,13 +1657,11 @@ gc_heap *gc_sweep(gc_heap * h, gc_thread_data *thd)
// have the trace/clear color. We need to keep any of those to make sure // have the trace/clear color. We need to keep any of those to make sure
// the collector has a chance to trace the entire heap. // the collector has a chance to trace the entire heap.
if ( //mark(p) != markColor && if ( //mark(p) != markColor &&
mark(p) != thd->gc_alloc_color && mark(p) != thd->gc_alloc_color && mark(p) != thd->gc_trace_color) { //gc_color_clear)
mark(p) != thd->gc_trace_color) { //gc_color_clear)
#if GC_DEBUG_VERBOSE #if GC_DEBUG_VERBOSE
fprintf(stderr, "sweep is freeing unmarked obj: %p with tag %d mark %d - alloc color %d trace color %d\n", p, fprintf(stderr,
type_of(p), "sweep is freeing unmarked obj: %p with tag %d mark %d - alloc color %d trace color %d\n",
mark(p), p, type_of(p), mark(p), thd->gc_alloc_color, thd->gc_trace_color);
thd->gc_alloc_color, thd->gc_trace_color);
#endif #endif
//mark(p) = gc_color_blue; // Needed? //mark(p) = gc_color_blue; // Needed?
if (type_of(p) == mutex_tag) { if (type_of(p) == mutex_tag) {
@ -1841,9 +1855,13 @@ static void mark_stack_or_heap_obj(gc_thread_data * thd, object obj, int locked)
grayed(obj) = 1; grayed(obj) = 1;
} else { } else {
// Value is on the heap, mark gray right now // Value is on the heap, mark gray right now
if (!locked) { pthread_mutex_lock(&(thd->lock)); } if (!locked) {
pthread_mutex_lock(&(thd->lock));
}
gc_mark_gray(thd, obj); gc_mark_gray(thd, obj);
if (!locked) { pthread_mutex_unlock(&(thd->lock)); } if (!locked) {
pthread_mutex_unlock(&(thd->lock));
}
} }
} }
@ -2010,7 +2028,8 @@ fprintf(stdout, "done tracing, cooperator is clearing full bits\n");
int heap_type, over_gc_collection_threshold = 0; int heap_type, over_gc_collection_threshold = 0;
for (heap_type = 0; heap_type < HEAP_HUGE; heap_type++) { for (heap_type = 0; heap_type < HEAP_HUGE; heap_type++) {
thd->cached_heap_free_sizes[heap_type] = gc_heap_free_size(thd->heap->heap[heap_type]); thd->cached_heap_free_sizes[heap_type] =
gc_heap_free_size(thd->heap->heap[heap_type]);
if (thd->cached_heap_free_sizes[heap_type] < if (thd->cached_heap_free_sizes[heap_type] <
thd->cached_heap_total_sizes[heap_type] * GC_COLLECTION_THRESHOLD) { thd->cached_heap_total_sizes[heap_type] * GC_COLLECTION_THRESHOLD) {
over_gc_collection_threshold = 1; over_gc_collection_threshold = 1;
@ -2020,9 +2039,12 @@ fprintf(stdout, "done tracing, cooperator is clearing full bits\n");
heap_type, heap_type,
thd->cached_heap_free_sizes[heap_type], thd->cached_heap_free_sizes[heap_type],
thd->cached_heap_total_sizes[heap_type]); thd->cached_heap_total_sizes[heap_type]);
if (thd->cached_heap_free_sizes[heap_type] > thd->cached_heap_total_sizes[heap_type]) { if (thd->cached_heap_free_sizes[heap_type] >
fprintf(stderr, "gc_mut_cooperate - Invalid cached heap sizes, free=%zu total=%zu\n", thd->cached_heap_total_sizes[heap_type]) {
thd->cached_heap_free_sizes[heap_type], thd->cached_heap_total_sizes[heap_type]); fprintf(stderr,
"gc_mut_cooperate - Invalid cached heap sizes, free=%zu total=%zu\n",
thd->cached_heap_free_sizes[heap_type],
thd->cached_heap_total_sizes[heap_type]);
exit(1); exit(1);
} }
#endif #endif
@ -2069,8 +2091,7 @@ void gc_mark_gray(gc_thread_data * thd, object obj)
// timing issues when incrementing colors and since if we ever reach a // timing issues when incrementing colors and since if we ever reach a
// purple object during tracing we would want to mark it. // purple object during tracing we would want to mark it.
// TODO: revisit if checking for gc_color_purple is truly necessary here and elsewhere. // TODO: revisit if checking for gc_color_purple is truly necessary here and elsewhere.
if (is_object_type(obj) && (mark(obj) == gc_color_clear || if (is_object_type(obj) && (mark(obj) == gc_color_clear || mark(obj) == gc_color_purple)) { // TODO: sync??
mark(obj) == gc_color_purple)) { // TODO: sync??
// Place marked object in a buffer to avoid repeated scans of the heap. // Place marked object in a buffer to avoid repeated scans of the heap.
// TODO: // TODO:
// Note that ideally this should be a lock-free data structure to make the // Note that ideally this should be a lock-free data structure to make the
@ -2096,7 +2117,8 @@ void gc_mark_gray2(gc_thread_data * thd, object obj)
{ {
if (is_object_type(obj) && (mark(obj) == gc_color_clear || if (is_object_type(obj) && (mark(obj) == gc_color_clear ||
mark(obj) == gc_color_purple)) { mark(obj) == gc_color_purple)) {
mark_buffer_set(thd->mark_buffer, thd->last_write + thd->pending_writes, obj); mark_buffer_set(thd->mark_buffer, thd->last_write + thd->pending_writes,
obj);
thd->pending_writes++; thd->pending_writes++;
} }
} }
@ -2119,8 +2141,9 @@ static void gc_collector_mark_gray(object parent, object obj)
fprintf(stderr, "mark gray parent = %p (%d) obj = %p\n", parent, fprintf(stderr, "mark gray parent = %p (%d) obj = %p\n", parent,
type_of(parent), obj); type_of(parent), obj);
} else if (is_object_type(obj)) { } else if (is_object_type(obj)) {
fprintf(stderr, "not marking gray, parent = %p (%d) obj = %p mark(obj) = %d, gc_color_clear = %d\n", parent, fprintf(stderr,
type_of(parent), obj, mark(obj), gc_color_clear); "not marking gray, parent = %p (%d) obj = %p mark(obj) = %d, gc_color_clear = %d\n",
parent, type_of(parent), obj, mark(obj), gc_color_clear);
} }
} }
#else #else
@ -2283,7 +2306,8 @@ void gc_collector_trace()
#if GC_DEBUG_VERBOSE #if GC_DEBUG_VERBOSE
fprintf(stderr, fprintf(stderr,
"gc_mark_black mark buffer %p, last_read = %d last_write = %d\n", "gc_mark_black mark buffer %p, last_read = %d last_write = %d\n",
mark_buffer_get(m->mark_buffer, m->last_read), m->last_read, last_write); mark_buffer_get(m->mark_buffer, m->last_read), m->last_read,
last_write);
#endif #endif
gc_mark_black(mark_buffer_get(m->mark_buffer, m->last_read)); gc_mark_black(mark_buffer_get(m->mark_buffer, m->last_read));
gc_empty_collector_stack(); gc_empty_collector_stack();
@ -2404,7 +2428,8 @@ void gc_wait_handshake()
//printf("DEBUG - update mutator GC status\n"); //printf("DEBUG - update mutator GC status\n");
ck_pr_cas_int(&(m->gc_status), statusm, statusc); ck_pr_cas_int(&(m->gc_status), statusm, statusc);
#if GC_DEBUG_TRACE #if GC_DEBUG_TRACE
fprintf(stderr, "DEBUG - collector is cooperating for blocked mutator\n"); fprintf(stderr,
"DEBUG - collector is cooperating for blocked mutator\n");
#endif #endif
buf_len = buf_len =
gc_minor(m, m->stack_limit, m->stack_start, m->gc_cont, NULL, gc_minor(m, m->stack_limit, m->stack_start, m->gc_cont, NULL,
@ -2551,8 +2576,7 @@ static pthread_t collector_thread;
*/ */
void gc_start_collector() void gc_start_collector()
{ {
if (pthread_create if (pthread_create(&collector_thread, NULL, collector_main, NULL)) {
(&collector_thread, NULL, collector_main, NULL)) {
fprintf(stderr, "Error creating collector thread\n"); fprintf(stderr, "Error creating collector thread\n");
exit(1); exit(1);
} }
@ -2589,7 +2613,6 @@ void gc_mark_globals(object globals, object global_table)
} }
} }
///////////////////////////////////////////// /////////////////////////////////////////////
// END tri-color marking section // END tri-color marking section
///////////////////////////////////////////// /////////////////////////////////////////////
@ -2626,8 +2649,7 @@ void gc_thread_data_init(gc_thread_data * thd, int mut_num, char *stack_base,
thd->mutations = NULL; thd->mutations = NULL;
thd->mutation_buflen = 128; thd->mutation_buflen = 128;
thd->mutation_count = 0; thd->mutation_count = 0;
thd->mutations = thd->mutations = vpbuffer_realloc(thd->mutations, &(thd->mutation_buflen));
vpbuffer_realloc(thd->mutations, &(thd->mutation_buflen));
thd->globals_changed = 1; thd->globals_changed = 1;
thd->param_objs = NULL; thd->param_objs = NULL;
thd->exception_handler_stack = NULL; thd->exception_handler_stack = NULL;
@ -2808,7 +2830,8 @@ void gc_recopy_obj(object obj, gc_thread_data *thd)
* it was blocking, the mutator will move any remaining stack objects to * it was blocking, the mutator will move any remaining stack objects to
* the heap and longjmp. * the heap and longjmp.
*/ */
void gc_mutator_thread_runnable(gc_thread_data * thd, object result, object maybe_copied) void gc_mutator_thread_runnable(gc_thread_data * thd, object result,
object maybe_copied)
{ {
char stack_limit; char stack_limit;
// Transition from blocked back to runnable using CAS. // Transition from blocked back to runnable using CAS.

View file

@ -86,7 +86,6 @@ static void maybe_rehash(hashset_t set)
size_t *old_items; size_t *old_items;
size_t old_capacity, ii; size_t old_capacity, ii;
if (set->nitems + set->n_deleted_items >= (double)set->capacity * 0.85) { if (set->nitems + set->n_deleted_items >= (double)set->capacity * 0.85) {
old_items = set->items; old_items = set->items;
old_capacity = set->capacity; old_capacity = set->capacity;
@ -154,4 +153,3 @@ void hashset_to_array(hashset_t set, void **items)
} }
} }
} }

View file

@ -178,10 +178,7 @@ typedef int mp_endian;
#ifndef MP_FIXED_CUTOFFS #ifndef MP_FIXED_CUTOFFS
extern int extern int
KARATSUBA_MUL_CUTOFF, KARATSUBA_MUL_CUTOFF, KARATSUBA_SQR_CUTOFF, TOOM_MUL_CUTOFF, TOOM_SQR_CUTOFF;
KARATSUBA_SQR_CUTOFF,
TOOM_MUL_CUTOFF,
TOOM_SQR_CUTOFF;
#endif #endif
/* define this to use lower memory usage routines (exptmods mostly) */ /* define this to use lower memory usage routines (exptmods mostly) */
@ -258,7 +255,8 @@ typedef struct {
/* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */ /* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */
typedef int private_mp_prime_callback(unsigned char *dst, int len, void *dat); typedef int private_mp_prime_callback(unsigned char *dst, int len, void *dat);
typedef private_mp_prime_callback MP_DEPRECATED(mp_rand_source) ltm_prime_callback; typedef private_mp_prime_callback MP_DEPRECATED(mp_rand_source)
ltm_prime_callback;
/* error code to char* string */ /* error code to char* string */
const char *mp_error_to_string(mp_err code) MP_WUR; const char *mp_error_to_string(mp_err code) MP_WUR;
@ -352,13 +350,19 @@ void mp_set(mp_int *a, mp_digit b);
mp_err mp_init_set(mp_int * a, mp_digit b) MP_WUR; mp_err mp_init_set(mp_int * a, mp_digit b) MP_WUR;
/* get integer, set integer and init with integer (deprecated) */ /* get integer, set integer and init with integer (deprecated) */
MP_DEPRECATED(mp_get_mag_u32/mp_get_u32) unsigned long mp_get_int(const mp_int *a) MP_WUR; MP_DEPRECATED(mp_get_mag_u32 /
MP_DEPRECATED(mp_get_mag_ul/mp_get_ul) unsigned long mp_get_long(const mp_int *a) MP_WUR; mp_get_u32) unsigned long mp_get_int(const mp_int * a) MP_WUR;
MP_DEPRECATED(mp_get_mag_ull/mp_get_ull) unsigned long long mp_get_long_long(const mp_int *a) MP_WUR; MP_DEPRECATED(mp_get_mag_ul /
mp_get_ul) unsigned long mp_get_long(const mp_int * a) MP_WUR;
MP_DEPRECATED(mp_get_mag_ull /
mp_get_ull) unsigned long long mp_get_long_long(const mp_int *
a) MP_WUR;
MP_DEPRECATED(mp_set_ul) mp_err mp_set_int(mp_int * a, unsigned long b); MP_DEPRECATED(mp_set_ul) mp_err mp_set_int(mp_int * a, unsigned long b);
MP_DEPRECATED(mp_set_ul) mp_err mp_set_long(mp_int * a, unsigned long b); MP_DEPRECATED(mp_set_ul) mp_err mp_set_long(mp_int * a, unsigned long b);
MP_DEPRECATED(mp_set_ull) mp_err mp_set_long_long(mp_int *a, unsigned long long b); MP_DEPRECATED(mp_set_ull) mp_err mp_set_long_long(mp_int * a,
MP_DEPRECATED(mp_init_ul) mp_err mp_init_set_int(mp_int *a, unsigned long b) MP_WUR; unsigned long long b);
MP_DEPRECATED(mp_init_ul) mp_err mp_init_set_int(mp_int * a,
unsigned long b) MP_WUR;
/* copy, b = a */ /* copy, b = a */
mp_err mp_copy(const mp_int * a, mp_int * b) MP_WUR; mp_err mp_copy(const mp_int * a, mp_int * b) MP_WUR;
@ -369,24 +373,27 @@ mp_err mp_init_copy(mp_int *a, const mp_int *b) MP_WUR;
/* trim unused digits */ /* trim unused digits */
void mp_clamp(mp_int * a); void mp_clamp(mp_int * a);
/* export binary data */ /* export binary data */
MP_DEPRECATED(mp_pack) mp_err mp_export(void *rop, size_t *countp, int order, size_t size, MP_DEPRECATED(mp_pack) mp_err mp_export(void *rop, size_t *countp, int order,
int endian, size_t nails, const mp_int *op) MP_WUR; size_t size, int endian,
size_t nails,
const mp_int * op) MP_WUR;
/* import binary data */ /* import binary data */
MP_DEPRECATED(mp_unpack) mp_err mp_import(mp_int *rop, size_t count, int order, MP_DEPRECATED(mp_unpack) mp_err mp_import(mp_int * rop, size_t count,
size_t size, int endian, size_t nails, int order, size_t size, int endian,
size_t nails,
const void *op) MP_WUR; const void *op) MP_WUR;
/* unpack binary data */ /* unpack binary data */
mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size, mp_endian endian, mp_err mp_unpack(mp_int * rop, size_t count, mp_order order, size_t size,
size_t nails, const void *op) MP_WUR; mp_endian endian, size_t nails, const void *op) MP_WUR;
/* pack binary data */ /* pack binary data */
size_t mp_pack_count(const mp_int * a, size_t nails, size_t size) MP_WUR; size_t mp_pack_count(const mp_int * a, size_t nails, size_t size) MP_WUR;
mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size_t size, mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order,
mp_endian endian, size_t nails, const mp_int *op) MP_WUR; size_t size, mp_endian endian, size_t nails,
const mp_int * op) MP_WUR;
/* ---> digit manipulation <--- */ /* ---> digit manipulation <--- */
@ -435,7 +442,8 @@ void mp_rand_source(mp_err(*source)(void *out, size_t size));
* implemented ways to gather entropy. * implemented ways to gather entropy.
* It is compatible with `rng_get_bytes()` from libtomcrypt so you could * It is compatible with `rng_get_bytes()` from libtomcrypt so you could
* provide that one and then set `ltm_rng = rng_get_bytes;` */ * provide that one and then set `ltm_rng = rng_get_bytes;` */
extern unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void)); extern unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen,
void(*callback)(void));
extern void (*ltm_rng_callback)(void); extern void (*ltm_rng_callback)(void);
#endif #endif
@ -448,22 +456,26 @@ extern void (*ltm_rng_callback)(void);
MP_DEPRECATED(s_mp_get_bit) int mp_get_bit(const mp_int * a, int b) MP_WUR; MP_DEPRECATED(s_mp_get_bit) int mp_get_bit(const mp_int * a, int b) MP_WUR;
/* c = a XOR b (two complement) */ /* c = a XOR b (two complement) */
MP_DEPRECATED(mp_xor) mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; MP_DEPRECATED(mp_xor) mp_err mp_tc_xor(const mp_int * a, const mp_int * b,
mp_int * c) MP_WUR;
mp_err mp_xor(const mp_int * a, const mp_int * b, mp_int * c) MP_WUR; mp_err mp_xor(const mp_int * a, const mp_int * b, mp_int * c) MP_WUR;
/* c = a OR b (two complement) */ /* c = a OR b (two complement) */
MP_DEPRECATED(mp_or) mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; MP_DEPRECATED(mp_or) mp_err mp_tc_or(const mp_int * a, const mp_int * b,
mp_int * c) MP_WUR;
mp_err mp_or(const mp_int * a, const mp_int * b, mp_int * c) MP_WUR; mp_err mp_or(const mp_int * a, const mp_int * b, mp_int * c) MP_WUR;
/* c = a AND b (two complement) */ /* c = a AND b (two complement) */
MP_DEPRECATED(mp_and) mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; MP_DEPRECATED(mp_and) mp_err mp_tc_and(const mp_int * a, const mp_int * b,
mp_int * c) MP_WUR;
mp_err mp_and(const mp_int * a, const mp_int * b, mp_int * c) MP_WUR; mp_err mp_and(const mp_int * a, const mp_int * b, mp_int * c) MP_WUR;
/* b = ~a (bitwise not, two complement) */ /* b = ~a (bitwise not, two complement) */
mp_err mp_complement(const mp_int * a, mp_int * b) MP_WUR; mp_err mp_complement(const mp_int * a, mp_int * b) MP_WUR;
/* right shift with sign extension */ /* right shift with sign extension */
MP_DEPRECATED(mp_signed_rsh) mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c) MP_WUR; MP_DEPRECATED(mp_signed_rsh) mp_err mp_tc_div_2d(const mp_int * a, int b,
mp_int * c) MP_WUR;
mp_err mp_signed_rsh(const mp_int * a, int b, mp_int * c) MP_WUR; mp_err mp_signed_rsh(const mp_int * a, int b, mp_int * c) MP_WUR;
/* ---> Basic arithmetic <--- */ /* ---> Basic arithmetic <--- */
@ -493,7 +505,8 @@ mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
mp_err mp_sqr(const mp_int * a, mp_int * b) MP_WUR; mp_err mp_sqr(const mp_int * a, mp_int * b) MP_WUR;
/* a/b => cb + d == a */ /* a/b => cb + d == a */
mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) MP_WUR; mp_err mp_div(const mp_int * a, const mp_int * b, mp_int * c,
mp_int * d) MP_WUR;
/* c = a mod b, 0 <= c < b */ /* c = a mod b, 0 <= c < b */
mp_err mp_mod(const mp_int * a, const mp_int * b, mp_int * c) MP_WUR; mp_err mp_mod(const mp_int * a, const mp_int * b, mp_int * c) MP_WUR;
@ -519,7 +532,8 @@ mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
mp_err mp_mul_d(const mp_int * a, mp_digit b, mp_int * c) MP_WUR; mp_err mp_mul_d(const mp_int * a, mp_digit b, mp_int * c) MP_WUR;
/* a/b => cb + d == a */ /* a/b => cb + d == a */
mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d) MP_WUR; mp_err mp_div_d(const mp_int * a, mp_digit b, mp_int * c,
mp_digit * d) MP_WUR;
/* c = a mod b, 0 <= c < b */ /* c = a mod b, 0 <= c < b */
mp_err mp_mod_d(const mp_int * a, mp_digit b, mp_digit * c) MP_WUR; mp_err mp_mod_d(const mp_int * a, mp_digit b, mp_digit * c) MP_WUR;
@ -527,13 +541,16 @@ mp_err mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c) MP_WUR;
/* ---> number theory <--- */ /* ---> number theory <--- */
/* d = a + b (mod c) */ /* d = a + b (mod c) */
mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR; mp_err mp_addmod(const mp_int * a, const mp_int * b, const mp_int * c,
mp_int * d) MP_WUR;
/* d = a - b (mod c) */ /* d = a - b (mod c) */
mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR; mp_err mp_submod(const mp_int * a, const mp_int * b, const mp_int * c,
mp_int * d) MP_WUR;
/* d = a * b (mod c) */ /* d = a * b (mod c) */
mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR; mp_err mp_mulmod(const mp_int * a, const mp_int * b, const mp_int * c,
mp_int * d) MP_WUR;
/* c = a * a (mod b) */ /* c = a * a (mod b) */
mp_err mp_sqrmod(const mp_int * a, const mp_int * b, mp_int * c) MP_WUR; mp_err mp_sqrmod(const mp_int * a, const mp_int * b, mp_int * c) MP_WUR;
@ -545,7 +562,8 @@ mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
mp_err mp_gcd(const mp_int * a, const mp_int * b, mp_int * c) MP_WUR; mp_err mp_gcd(const mp_int * a, const mp_int * b, mp_int * c) MP_WUR;
/* produces value such that U1*a + U2*b = U3 */ /* produces value such that U1*a + U2*b = U3 */
mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) MP_WUR; mp_err mp_exteuclid(const mp_int * a, const mp_int * b, mp_int * U1,
mp_int * U2, mp_int * U3) MP_WUR;
/* c = [a, b] or (a*b)/(a, b) */ /* c = [a, b] or (a*b)/(a, b) */
mp_err mp_lcm(const mp_int * a, const mp_int * b, mp_int * c) MP_WUR; mp_err mp_lcm(const mp_int * a, const mp_int * b, mp_int * c) MP_WUR;
@ -555,20 +573,25 @@ mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
* returns error if a < 0 and b is even * returns error if a < 0 and b is even
*/ */
mp_err mp_root_u32(const mp_int * a, uint32_t b, mp_int * c) MP_WUR; mp_err mp_root_u32(const mp_int * a, uint32_t b, mp_int * c) MP_WUR;
MP_DEPRECATED(mp_root_u32) mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c) MP_WUR; MP_DEPRECATED(mp_root_u32) mp_err mp_n_root(const mp_int * a, mp_digit b,
MP_DEPRECATED(mp_root_u32) mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR; mp_int * c) MP_WUR;
MP_DEPRECATED(mp_root_u32) mp_err mp_n_root_ex(const mp_int * a, mp_digit b,
mp_int * c, int fast) MP_WUR;
/* special sqrt algo */ /* special sqrt algo */
mp_err mp_sqrt(const mp_int * arg, mp_int * ret) MP_WUR; mp_err mp_sqrt(const mp_int * arg, mp_int * ret) MP_WUR;
/* special sqrt (mod prime) */ /* special sqrt (mod prime) */
mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret) MP_WUR; mp_err mp_sqrtmod_prime(const mp_int * n, const mp_int * prime,
mp_int * ret) MP_WUR;
/* is number a square? */ /* is number a square? */
mp_err mp_is_square(const mp_int * arg, mp_bool * ret) MP_WUR; mp_err mp_is_square(const mp_int * arg, mp_bool * ret) MP_WUR;
/* computes the jacobi c = (a | n) (or Legendre if b is prime) */ /* computes the jacobi c = (a | n) (or Legendre if b is prime) */
MP_DEPRECATED(mp_kronecker) mp_err mp_jacobi(const mp_int *a, const mp_int *n, int *c) MP_WUR; MP_DEPRECATED(mp_kronecker) mp_err mp_jacobi(const mp_int * a,
const mp_int * n,
int *c) MP_WUR;
/* computes the Kronecker symbol c = (a | p) (like jacobi() but with {a,p} in Z */ /* computes the Kronecker symbol c = (a | p) (like jacobi() but with {a,p} in Z */
mp_err mp_kronecker(const mp_int * a, const mp_int * p, int *c) MP_WUR; mp_err mp_kronecker(const mp_int * a, const mp_int * p, int *c) MP_WUR;
@ -592,7 +615,8 @@ mp_err mp_montgomery_setup(const mp_int *n, mp_digit *rho) MP_WUR;
mp_err mp_montgomery_calc_normalization(mp_int * a, const mp_int * b) MP_WUR; mp_err mp_montgomery_calc_normalization(mp_int * a, const mp_int * b) MP_WUR;
/* computes x/R == x (mod N) via Montgomery Reduction */ /* computes x/R == x (mod N) via Montgomery Reduction */
mp_err mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR; mp_err mp_montgomery_reduce(mp_int * x, const mp_int * n,
mp_digit rho) MP_WUR;
/* returns 1 if a is a valid DR modulus */ /* returns 1 if a is a valid DR modulus */
mp_bool mp_dr_is_modulus(const mp_int * a) MP_WUR; mp_bool mp_dr_is_modulus(const mp_int * a) MP_WUR;
@ -622,7 +646,8 @@ mp_err mp_reduce_2k_setup_l(const mp_int *a, mp_int *d) MP_WUR;
mp_err mp_reduce_2k_l(mp_int * a, const mp_int * n, const mp_int * d) MP_WUR; mp_err mp_reduce_2k_l(mp_int * a, const mp_int * n, const mp_int * d) MP_WUR;
/* Y = G**X (mod P) */ /* Y = G**X (mod P) */
mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) MP_WUR; mp_err mp_exptmod(const mp_int * G, const mp_int * X, const mp_int * P,
mp_int * Y) MP_WUR;
/* ---> Primes <--- */ /* ---> Primes <--- */
@ -635,20 +660,26 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
#define PRIME_SIZE (MP_DEPRECATED_PRAGMA("PRIME_SIZE has been made internal") PRIVATE_MP_PRIME_TAB_SIZE) #define PRIME_SIZE (MP_DEPRECATED_PRAGMA("PRIME_SIZE has been made internal") PRIVATE_MP_PRIME_TAB_SIZE)
/* table of first PRIME_SIZE primes */ /* table of first PRIME_SIZE primes */
MP_DEPRECATED(internal) extern const mp_digit ltm_prime_tab[PRIVATE_MP_PRIME_TAB_SIZE]; MP_DEPRECATED(internal) extern const mp_digit
ltm_prime_tab[PRIVATE_MP_PRIME_TAB_SIZE];
/* result=1 if a is divisible by one of the first PRIME_SIZE primes */ /* result=1 if a is divisible by one of the first PRIME_SIZE primes */
MP_DEPRECATED(mp_prime_is_prime) mp_err mp_prime_is_divisible(const mp_int *a, mp_bool *result) MP_WUR; MP_DEPRECATED(mp_prime_is_prime) mp_err mp_prime_is_divisible(const mp_int *
a,
mp_bool *
result) MP_WUR;
/* performs one Fermat test of "a" using base "b". /* performs one Fermat test of "a" using base "b".
* Sets result to 0 if composite or 1 if probable prime * Sets result to 0 if composite or 1 if probable prime
*/ */
mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, mp_bool *result) MP_WUR; mp_err mp_prime_fermat(const mp_int * a, const mp_int * b,
mp_bool * result) MP_WUR;
/* performs one Miller-Rabin test of "a" using base "b". /* performs one Miller-Rabin test of "a" using base "b".
* Sets result to 0 if composite or 1 if probable prime * Sets result to 0 if composite or 1 if probable prime
*/ */
mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, mp_bool *result) MP_WUR; mp_err mp_prime_miller_rabin(const mp_int * a, const mp_int * b,
mp_bool * result) MP_WUR;
/* This gives [for a given bit size] the number of trials required /* This gives [for a given bit size] the number of trials required
* such that Miller-Rabin gives a prob of failure lower than 2^-96 * such that Miller-Rabin gives a prob of failure lower than 2^-96
@ -658,12 +689,14 @@ int mp_prime_rabin_miller_trials(int size) MP_WUR;
/* performs one strong Lucas-Selfridge test of "a". /* performs one strong Lucas-Selfridge test of "a".
* Sets result to 0 if composite or 1 if probable prime * Sets result to 0 if composite or 1 if probable prime
*/ */
mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) MP_WUR; mp_err mp_prime_strong_lucas_selfridge(const mp_int * a,
mp_bool * result) MP_WUR;
/* performs one Frobenius test of "a" as described by Paul Underwood. /* performs one Frobenius test of "a" as described by Paul Underwood.
* Sets result to 0 if composite or 1 if probable prime * Sets result to 0 if composite or 1 if probable prime
*/ */
mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) MP_WUR; mp_err mp_prime_frobenius_underwood(const mp_int * N,
mp_bool * result) MP_WUR;
/* performs t random rounds of Miller-Rabin on "a" additional to /* performs t random rounds of Miller-Rabin on "a" additional to
* bases 2 and 3. Also performs an initial sieve of trial * bases 2 and 3. Also performs an initial sieve of trial
@ -712,8 +745,10 @@ mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style) MP_WUR;
* so it can be NULL * so it can be NULL
* *
*/ */
MP_DEPRECATED(mp_prime_rand) mp_err mp_prime_random_ex(mp_int *a, int t, int size, int flags, MP_DEPRECATED(mp_prime_rand) mp_err mp_prime_random_ex(mp_int * a, int t,
private_mp_prime_callback cb, void *dat) MP_WUR; int size, int flags,
private_mp_prime_callback
cb, void *dat) MP_WUR;
mp_err mp_prime_rand(mp_int * a, int t, int size, int flags) MP_WUR; mp_err mp_prime_rand(mp_int * a, int t, int size, int flags) MP_WUR;
/* Integer logarithm to integer base */ /* Integer logarithm to integer base */
@ -721,35 +756,54 @@ mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c) MP_WUR;
/* c = a**b */ /* c = a**b */
mp_err mp_expt_u32(const mp_int * a, uint32_t b, mp_int * c) MP_WUR; mp_err mp_expt_u32(const mp_int * a, uint32_t b, mp_int * c) MP_WUR;
MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR; MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d(const mp_int * a, mp_digit b,
MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR; mp_int * c) MP_WUR;
MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d_ex(const mp_int * a, mp_digit b,
mp_int * c, int fast) MP_WUR;
/* ---> radix conversion <--- */ /* ---> radix conversion <--- */
int mp_count_bits(const mp_int * a) MP_WUR; int mp_count_bits(const mp_int * a) MP_WUR;
MP_DEPRECATED(mp_ubin_size) int mp_unsigned_bin_size(const mp_int *
MP_DEPRECATED(mp_ubin_size) int mp_unsigned_bin_size(const mp_int *a) MP_WUR; a) MP_WUR;
MP_DEPRECATED(mp_from_ubin) mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) MP_WUR; MP_DEPRECATED(mp_from_ubin) mp_err mp_read_unsigned_bin(mp_int * a,
MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b) MP_WUR; const unsigned char
MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR; *b, int c) MP_WUR;
MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin(const mp_int * a,
unsigned char *b) MP_WUR;
MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin_n(const mp_int * a,
unsigned char *b,
unsigned long *outlen)
MP_WUR;
MP_DEPRECATED(mp_sbin_size) int mp_signed_bin_size(const mp_int * a) MP_WUR; MP_DEPRECATED(mp_sbin_size) int mp_signed_bin_size(const mp_int * a) MP_WUR;
MP_DEPRECATED(mp_from_sbin) mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c) MP_WUR; MP_DEPRECATED(mp_from_sbin) mp_err mp_read_signed_bin(mp_int * a,
MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin(const mp_int *a, unsigned char *b) MP_WUR; const unsigned char *b,
MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR; int c) MP_WUR;
MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin(const mp_int * a,
unsigned char *b) MP_WUR;
MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin_n(const mp_int * a,
unsigned char *b,
unsigned long *outlen)
MP_WUR;
size_t mp_ubin_size(const mp_int * a) MP_WUR; size_t mp_ubin_size(const mp_int * a) MP_WUR;
mp_err mp_from_ubin(mp_int * a, const unsigned char *buf, size_t size) MP_WUR; mp_err mp_from_ubin(mp_int * a, const unsigned char *buf, size_t size) MP_WUR;
mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR; mp_err mp_to_ubin(const mp_int * a, unsigned char *buf, size_t maxlen,
size_t *written) MP_WUR;
size_t mp_sbin_size(const mp_int * a) MP_WUR; size_t mp_sbin_size(const mp_int * a) MP_WUR;
mp_err mp_from_sbin(mp_int * a, const unsigned char *buf, size_t size) MP_WUR; mp_err mp_from_sbin(mp_int * a, const unsigned char *buf, size_t size) MP_WUR;
mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR; mp_err mp_to_sbin(const mp_int * a, unsigned char *buf, size_t maxlen,
size_t *written) MP_WUR;
mp_err mp_read_radix(mp_int * a, const char *str, int radix) MP_WUR; mp_err mp_read_radix(mp_int * a, const char *str, int radix) MP_WUR;
MP_DEPRECATED(mp_to_radix) mp_err mp_toradix(const mp_int *a, char *str, int radix) MP_WUR; MP_DEPRECATED(mp_to_radix) mp_err mp_toradix(const mp_int * a, char *str,
MP_DEPRECATED(mp_to_radix) mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) MP_WUR; int radix) MP_WUR;
mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix) MP_WUR; MP_DEPRECATED(mp_to_radix) mp_err mp_toradix_n(const mp_int * a, char *str,
int radix, int maxlen) MP_WUR;
mp_err mp_to_radix(const mp_int * a, char *str, size_t maxlen,
size_t *written, int radix) MP_WUR;
mp_err mp_radix_size(const mp_int * a, int radix, int *size) MP_WUR; mp_err mp_radix_size(const mp_int * a, int radix, int *size) MP_WUR;
#ifndef MP_NO_FILE #ifndef MP_NO_FILE
@ -777,5 +831,4 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) MP_WUR;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

View file

@ -71,5 +71,4 @@ extern "C" {
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

View file

@ -9,7 +9,6 @@
#ifndef CYCLONE_RUNTIME_H #ifndef CYCLONE_RUNTIME_H
#define CYCLONE_RUNTIME_H #define CYCLONE_RUNTIME_H
/** /**
* The boolean True value. * The boolean True value.
* \ingroup objects * \ingroup objects
@ -231,7 +230,8 @@ object Cyc_global_set(void *thd, object sym, object * glo, object value);
#define global_set_cps(thd,k,glo,value) Cyc_global_set_cps(thd, k, NULL, (object *)&glo, value) #define global_set_cps(thd,k,glo,value) Cyc_global_set_cps(thd, k, NULL, (object *)&glo, value)
#define global_set_cps_id(thd,k,id,glo,value) Cyc_global_set_cps(thd, k, id, (object *)&glo, value) #define global_set_cps_id(thd,k,id,glo,value) Cyc_global_set_cps(thd, k, id, (object *)&glo, value)
object Cyc_global_set_cps(void *thd, object cont, object sym, object * glo, object value); object Cyc_global_set_cps(void *thd, object cont, object sym, object * glo,
object value);
/** /**
* Variable argument count support * Variable argument count support
@ -372,13 +372,13 @@ object Cyc_io_char_ready(void *data, object port);
object Cyc_write_u8(void *data, object c, object port); object Cyc_write_u8(void *data, object c, object port);
object Cyc_io_read_u8(void *data, object cont, object port); object Cyc_io_read_u8(void *data, object cont, object port);
object Cyc_io_peek_u8(void *data, object cont, object port); object Cyc_io_peek_u8(void *data, object cont, object port);
object Cyc_write_bytevector(void *data, object bvec, object port, object start, object end); object Cyc_write_bytevector(void *data, object bvec, object port, object start,
object end);
object Cyc_io_read_line(void *data, object cont, object port); object Cyc_io_read_line(void *data, object cont, object port);
void Cyc_io_read_token(void *data, object cont, object port); void Cyc_io_read_token(void *data, object cont, object port);
int Cyc_have_mstreams(); int Cyc_have_mstreams();
/**@}*/ /**@}*/
/** /**
* \defgroup prim_num Numbers * \defgroup prim_num Numbers
* @brief Number functions * @brief Number functions
@ -558,8 +558,10 @@ object Cyc_fast_list_3(object ptr, object a1, object a2, object a3);
object Cyc_fast_list_4(object ptr, object a1, object a2, object a3, object a4); object Cyc_fast_list_4(object ptr, object a1, object a2, object a3, object a4);
object Cyc_fast_vector_2(object ptr, object a1, object a2); object Cyc_fast_vector_2(object ptr, object a1, object a2);
object Cyc_fast_vector_3(object ptr, object a1, object a2, object a3); object Cyc_fast_vector_3(object ptr, object a1, object a2, object a3);
object Cyc_fast_vector_4(object ptr, object a1, object a2, object a3, object a4); object Cyc_fast_vector_4(object ptr, object a1, object a2, object a3,
object Cyc_fast_vector_5(object ptr, object a1, object a2, object a3, object a4, object a5); object a4);
object Cyc_fast_vector_5(object ptr, object a1, object a2, object a3, object a4,
object a5);
object Cyc_bit_unset(void *data, object n1, object n2); object Cyc_bit_unset(void *data, object n1, object n2);
object Cyc_bit_set(void *data, object n1, object n2); object Cyc_bit_set(void *data, object n1, object n2);
object Cyc_num_op_va_list(void *data, int argc, object Cyc_num_op_va_list(void *data, int argc,
@ -569,8 +571,7 @@ object Cyc_num_op_va_list(void *data, int argc,
object Cyc_num_op_args(void *data, int argc, object Cyc_num_op_args(void *data, int argc,
object(fn_op(void *, common_type *, object)), object(fn_op(void *, common_type *, object)),
int default_no_args, int default_one_arg, int default_no_args, int default_one_arg,
object *args, object * args, common_type * buf);
common_type * buf);
void Cyc_int2bignum(int n, mp_int * bn); void Cyc_int2bignum(int n, mp_int * bn);
object Cyc_bignum_normalize(void *data, object n); object Cyc_bignum_normalize(void *data, object n);
int Cyc_bignum_cmp(bn_cmp_type type, object x, int tx, object y, int ty); int Cyc_bignum_cmp(bn_cmp_type type, object x, int tx, object y, int ty);
@ -651,7 +652,8 @@ object Cyc_vector_ref(void *d, object v, object k);
object Cyc_vector_set(void *d, object v, object k, object obj); object Cyc_vector_set(void *d, object v, object k, object obj);
object Cyc_vector_set_unsafe(void *d, object v, object k, object obj); object Cyc_vector_set_unsafe(void *d, object v, object k, object obj);
object Cyc_vector_set_cps(void *d, object cont, object v, object k, object obj); object Cyc_vector_set_cps(void *d, object cont, object v, object k, object obj);
object Cyc_vector_set_unsafe_cps(void *d, object cont, object v, object k, object obj); object Cyc_vector_set_unsafe_cps(void *d, object cont, object v, object k,
object obj);
object Cyc_make_vector(void *data, object cont, int argc, object len, ...); object Cyc_make_vector(void *data, object cont, int argc, object len, ...);
/**@}*/ /**@}*/
@ -907,7 +909,8 @@ extern object Cyc_glo_call_cc;
* @brief Raise and handle Scheme exceptions * @brief Raise and handle Scheme exceptions
*/ */
/**@{*/ /**@{*/
object Cyc_default_exception_handler(void *data, object _, int argc, object *args); object Cyc_default_exception_handler(void *data, object _, int argc,
object * args);
object Cyc_current_exception_handler(void *data); object Cyc_current_exception_handler(void *data);
void Cyc_rt_raise(void *data, object err); void Cyc_rt_raise(void *data, object err);
@ -994,6 +997,7 @@ static inline object Cyc_cdr(void *data, object lis)
Cyc_check_pair(data, lis); Cyc_check_pair(data, lis);
return cdr(lis); return cdr(lis);
} }
// Unsafe car/cdr // Unsafe car/cdr
#define Cyc_car_unsafe(d, lis) car(lis) #define Cyc_car_unsafe(d, lis) car(lis)
#define Cyc_cdr_unsafe(d, lis) cdr(lis) #define Cyc_cdr_unsafe(d, lis) cdr(lis)

View file

@ -46,31 +46,13 @@ typedef void *object;
*\ingroup objects *\ingroup objects
*/ */
enum object_tag { enum object_tag {
closure0_tag = 0 closure0_tag = 0, closure1_tag = 1, closureN_tag = 2, macro_tag = 3 // Keep closures here for quick type checking
, closure1_tag = 1 , boolean_tag = 4, bytevector_tag = 5, c_opaque_tag = 6, cond_var_tag =
, closureN_tag = 2 7, cvar_tag = 8, double_tag = 9, eof_tag = 10, forward_tag =
, macro_tag = 3 // Keep closures here for quick type checking 11, integer_tag = 12, bignum_tag = 13, mutex_tag = 14, pair_tag =
, boolean_tag = 4 15, port_tag = 16, primitive_tag = 17, string_tag = 18, symbol_tag =
, bytevector_tag = 5 19, vector_tag = 20, complex_num_tag = 21, atomic_tag = 22, void_tag =
, c_opaque_tag = 6 23, record_tag = 24
, cond_var_tag = 7
, cvar_tag = 8
, double_tag = 9
, eof_tag = 10
, forward_tag = 11
, integer_tag = 12
, bignum_tag = 13
, mutex_tag = 14
, pair_tag = 15
, port_tag = 16
, primitive_tag = 17
, string_tag = 18
, symbol_tag = 19
, vector_tag = 20
, complex_num_tag = 21
, atomic_tag = 22
, void_tag = 23
, record_tag = 24
}; };
/** /**
@ -408,9 +390,9 @@ void gc_merge_all_heaps(gc_thread_data *dest, gc_thread_data *src);
void gc_print_stats(gc_heap * h); void gc_print_stats(gc_heap * h);
gc_heap *gc_grow_heap(gc_heap * h, size_t size, gc_thread_data * thd); gc_heap *gc_grow_heap(gc_heap * h, size_t size, gc_thread_data * thd);
char *gc_copy_obj(object hp, char *obj, gc_thread_data * thd); char *gc_copy_obj(object hp, char *obj, gc_thread_data * thd);
void *gc_try_alloc(gc_heap * h, size_t size, char *obj, void *gc_try_alloc(gc_heap * h, size_t size, char *obj, gc_thread_data * thd);
void *gc_try_alloc_slow(gc_heap * h_passed, gc_heap * h, size_t size, char *obj,
gc_thread_data * thd); gc_thread_data * thd);
void *gc_try_alloc_slow(gc_heap *h_passed, gc_heap *h, size_t size, char *obj, gc_thread_data *thd);
void *gc_alloc(gc_heap_root * h, size_t size, char *obj, gc_thread_data * thd, void *gc_alloc(gc_heap_root * h, size_t size, char *obj, gc_thread_data * thd,
int *heap_grown); int *heap_grown);
void *gc_alloc_bignum(gc_thread_data * data); void *gc_alloc_bignum(gc_thread_data * data);
@ -418,8 +400,10 @@ size_t gc_allocated_bytes(object obj, gc_free_list * q, gc_free_list * r);
gc_heap *gc_heap_last(gc_heap * h); gc_heap *gc_heap_last(gc_heap * h);
void gc_heap_create_rest(gc_heap * h, gc_thread_data * thd); void gc_heap_create_rest(gc_heap * h, gc_thread_data * thd);
void *gc_try_alloc_rest(gc_heap * h, size_t size, char *obj, gc_thread_data * thd); void *gc_try_alloc_rest(gc_heap * h, size_t size, char *obj,
void *gc_alloc_rest(gc_heap_root * hrt, size_t size, char *obj, gc_thread_data * thd, int *heap_grown); gc_thread_data * thd);
void *gc_alloc_rest(gc_heap_root * hrt, size_t size, char *obj,
gc_thread_data * thd, int *heap_grown);
void gc_init_fixed_size_free_list(gc_heap * h); void gc_init_fixed_size_free_list(gc_heap * h);
//size_t gc_heap_total_size(gc_heap * h); //size_t gc_heap_total_size(gc_heap * h);
@ -456,7 +440,8 @@ void gc_post_handshake(gc_status_type s);
void gc_wait_handshake(); void gc_wait_handshake();
void gc_start_collector(); void gc_start_collector();
void gc_mutator_thread_blocked(gc_thread_data * thd, object cont); void gc_mutator_thread_blocked(gc_thread_data * thd, object cont);
void gc_mutator_thread_runnable(gc_thread_data * thd, object result, object maybe_copied); void gc_mutator_thread_runnable(gc_thread_data * thd, object result,
object maybe_copied);
void Cyc_make_shared_object(void *data, object k, object obj); void Cyc_make_shared_object(void *data, object k, object obj);
#define set_thread_blocked(d, c) \ #define set_thread_blocked(d, c) \
gc_mutator_thread_blocked(((gc_thread_data *)d), (c)) gc_mutator_thread_blocked(((gc_thread_data *)d), (c))
@ -523,7 +508,6 @@ void Cyc_make_shared_object(void *data, object k, object obj);
*/ */
#define forward(obj) (((pair_type *) obj)->pair_car) #define forward(obj) (((pair_type *) obj)->pair_car)
/** /**
* \defgroup gc_minor_mut Mutation table * \defgroup gc_minor_mut Mutation table
* @brief Mutation table to support the minor GC write barrier * @brief Mutation table to support the minor GC write barrier
@ -538,7 +522,8 @@ void clear_mutations(void *data);
* @brief Minor GC write barrier to ensure there are no references to stack objects from the heap. * @brief Minor GC write barrier to ensure there are no references to stack objects from the heap.
*/ */
/**@{*/ /**@{*/
object transport_stack_value(gc_thread_data *data, object var, object value, int *run_gc); object transport_stack_value(gc_thread_data * data, object var, object value,
int *run_gc);
/**@}*/ /**@}*/
/**@}*/ /**@}*/
@ -550,7 +535,8 @@ object transport_stack_value(gc_thread_data *data, object var, object value, int
* \defgroup ffi Foreign Function Interface * \defgroup ffi Foreign Function Interface
*/ */
/**@{*/ /**@{*/
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);
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);
/**@}*/ /**@}*/
@ -913,11 +899,8 @@ typedef struct {
* and provides constants for each of the comparison operators. * and provides constants for each of the comparison operators.
*/ */
typedef enum { typedef enum {
CYC_BN_LTE = -2 CYC_BN_LTE = -2, CYC_BN_LT = MP_LT, CYC_BN_EQ = MP_EQ, CYC_BN_GT =
, CYC_BN_LT = MP_LT MP_GT, CYC_BN_GTE = 2
, CYC_BN_EQ = MP_EQ
, CYC_BN_GT = MP_GT
, CYC_BN_GTE = 2
} bn_cmp_type; } bn_cmp_type;
/** /**
@ -1168,10 +1151,22 @@ typedef struct {
} vector_type; } vector_type;
typedef vector_type *vector; typedef vector_type *vector;
typedef struct { vector_type v; object arr[2]; } vector_2_type; typedef struct {
typedef struct { vector_type v; object arr[3]; } vector_3_type; vector_type v;
typedef struct { vector_type v; object arr[4]; } vector_4_type; object arr[2];
typedef struct { vector_type v; object arr[5]; } vector_5_type; } vector_2_type;
typedef struct {
vector_type v;
object arr[3];
} vector_3_type;
typedef struct {
vector_type v;
object arr[4];
} vector_4_type;
typedef struct {
vector_type v;
object arr[5];
} vector_5_type;
/** Create a new vector in the nursery */ /** Create a new vector in the nursery */
#define make_empty_vector(v) \ #define make_empty_vector(v) \
@ -1296,9 +1291,21 @@ typedef pair_type *pair;
(n)) (n))
//typedef list_1_type pair_type; //typedef list_1_type pair_type;
typedef struct { pair_type a; pair_type b; } list_2_type; typedef struct {
typedef struct { pair_type a; pair_type b; pair_type c;} list_3_type; pair_type a;
typedef struct { pair_type a; pair_type b; pair_type c; pair_type d;} list_4_type; pair_type b;
} list_2_type;
typedef struct {
pair_type a;
pair_type b;
pair_type c;
} list_3_type;
typedef struct {
pair_type a;
pair_type b;
pair_type c;
pair_type d;
} list_4_type;
/** /**
* Create a pair with a single value. * Create a pair with a single value.
@ -1548,5 +1555,6 @@ void *gc_alloc_from_bignum(gc_thread_data *data, bignum_type *src);
int gc_minor(void *data, object low_limit, object high_limit, closure cont, int gc_minor(void *data, object low_limit, object high_limit, closure cont,
object * args, int num_args); object * args, int num_args);
void Cyc_import_shared_object(void *data, object cont, object filename, object entry_pt_fnc); void Cyc_import_shared_object(void *data, object cont, object filename,
object entry_pt_fnc);
#endif /* CYCLONE_TYPES_H */ #endif /* CYCLONE_TYPES_H */

View file

@ -58,7 +58,8 @@ port_type *Cyc_io_open_input_string(void *data, object str)
p->fp = fmemopen(p->str_bv_in_mem_buf, string_len(str), "r"); p->fp = fmemopen(p->str_bv_in_mem_buf, string_len(str), "r");
#endif #endif
if (p->fp == NULL) { if (p->fp == NULL) {
Cyc_rt_raise2(data, "Unable to open input memory stream", obj_int2obj(errno)); Cyc_rt_raise2(data, "Unable to open input memory stream",
obj_int2obj(errno));
} }
return p; return p;
} }
@ -79,7 +80,8 @@ port_type *Cyc_io_open_input_bytevector(void *data, object bv)
p->fp = fmemopen(p->str_bv_in_mem_buf, ((bytevector) bv)->len, "r"); p->fp = fmemopen(p->str_bv_in_mem_buf, ((bytevector) bv)->len, "r");
#endif #endif
if (p->fp == NULL) { if (p->fp == NULL) {
Cyc_rt_raise2(data, "Unable to open input memory stream", obj_int2obj(errno)); Cyc_rt_raise2(data, "Unable to open input memory stream",
obj_int2obj(errno));
} }
return p; return p;
} }
@ -95,7 +97,8 @@ port_type *Cyc_io_open_output_string(void *data)
p->fp = open_memstream(&(p->str_bv_in_mem_buf), &(p->str_bv_in_mem_buf_len)); p->fp = open_memstream(&(p->str_bv_in_mem_buf), &(p->str_bv_in_mem_buf_len));
#endif #endif
if (p->fp == NULL) { if (p->fp == NULL) {
Cyc_rt_raise2(data, "Unable to open output memory stream", obj_int2obj(errno)); Cyc_rt_raise2(data, "Unable to open output memory stream",
obj_int2obj(errno));
} }
return p; return p;
} }
@ -130,8 +133,8 @@ void Cyc_io_get_output_bytevector(void *data, object cont, object port)
{ {
object bv; object bv;
alloc_bytevector(data, bv, p->str_bv_in_mem_buf_len); alloc_bytevector(data, bv, p->str_bv_in_mem_buf_len);
memcpy(((bytevector)bv)->data, p->str_bv_in_mem_buf, p->str_bv_in_mem_buf_len); memcpy(((bytevector) bv)->data, p->str_bv_in_mem_buf,
p->str_bv_in_mem_buf_len);
return_closcall1(data, cont, bv); return_closcall1(data, cont, bv);
} }
} }

857
runtime.c

File diff suppressed because it is too large Load diff