# HG changeset patch # User Paul Boddie # Date 1548594685 -3600 # Node ID 31053f0bb63b0acfe9179b7c1e1babc976836629 # Parent 9c9bd4fccad4e817b657077c5ff06a67d93c5aba Experiment with a statically-allocated table of preallocated instances, also marking the instances as "atomic". diff -r 9c9bd4fccad4 -r 31053f0bb63b templates/native/float.c --- a/templates/native/float.c Sun Jan 27 01:08:28 2019 +0100 +++ b/templates/native/float.c Sun Jan 27 14:11:25 2019 +0100 @@ -30,64 +30,50 @@ /* A table of preallocated float instances. */ -struct float_table -{ - struct float_table *next; - char data[]; -}; +#define NUM_PREALLOCATED 1000 -static struct float_table *next_float = 0; +__ref preallocated_floats[NUM_PREALLOCATED]; + +static int floats_left = 0; /* Preallocate some float instances. */ -static void preallocate_floats(int num) +static void preallocate_floats() { - struct float_table *latest; int i; - for (i = 0; i < num; i++) + for (i = 0; i < NUM_PREALLOCATED; i++) { - /* Allocate a table entry. */ + /* Allocate a float for each table entry. */ - latest = (struct float_table *) - __ALLOCATE(1, sizeof(struct float_table *) + - __INSTANCESIZE(__builtins___float_float)); + preallocated_floats[i] = \ + __ALLOCATEIM(1, __INSTANCESIZE(__builtins___float_float)); + } - /* Reference the last entry from the new entry. */ - - latest->next = next_float; - next_float = latest; - } + floats_left = NUM_PREALLOCATED; } static __attr new_float(double n) { - struct float_table *this_float; + __ref obj; __attr attr; - if (!next_float) - preallocate_floats(1000); - - /* Reference the next preallocated entry. */ + if (!floats_left) + preallocate_floats(); - this_float = next_float; - - /* Initialise the embedded instance. */ + floats_left--; + obj = preallocated_floats[floats_left]; - __init((__ref) &this_float->data, - &__INSTANCETABLE(__builtins___float_float), - &__builtins___float_float); + /* Initialise the instance. */ + + __init(obj, &__INSTANCETABLE(__builtins___float_float), + &__builtins___float_float); /* Populate the float with the value. */ - attr = __ATTRVALUE(&this_float->data); + attr = __ATTRVALUE(obj); __set_trailing_data(attr, __builtins___float_float, n); - /* Make the next entry available and detach it from this one. */ - - next_float = this_float->next; - this_float->next = 0; - return attr; }