1.1 --- a/templates/native/float.c Thu Aug 15 22:49:18 2024 +0200
1.2 +++ b/templates/native/float.c Sun Aug 18 18:52:50 2024 +0200
1.3 @@ -1,6 +1,6 @@
1.4 /* Native functions for floating point operations.
1.5
1.6 -Copyright (C) 2016, 2017, 2018, 2019 Paul Boddie <paul@boddie.org.uk>
1.7 +Copyright (C) 2016, 2017, 2018, 2019, 2024 Paul Boddie <paul@boddie.org.uk>
1.8
1.9 This program is free software; you can redistribute it and/or modify it under
1.10 the terms of the GNU General Public License as published by the Free Software
1.11 @@ -28,19 +28,37 @@
1.12 #include "progtypes.h"
1.13 #include "main.h"
1.14
1.15 -/* A table of preallocated float instances. */
1.16 +/* Tables of preallocated float instances. */
1.17 +
1.18 +#define NUM_PREALLOCATED 100000
1.19 +
1.20 +/* Link tables together to retain them. */
1.21
1.22 -#define NUM_PREALLOCATED 10000
1.23 +struct float_block
1.24 +{
1.25 + __OBJTYPE(__builtins___float_float) *floats;
1.26 + struct float_block *previous_block;
1.27 +};
1.28
1.29 -static __ref preallocated_floats;
1.30 +static struct float_block *current_block = NULL;
1.31 static int floats_left = 0;
1.32
1.33 /* Preallocate some float instances. */
1.34
1.35 static void preallocate_floats()
1.36 {
1.37 - preallocated_floats = __ALLOCATEIM(NUM_PREALLOCATED, __INSTANCESIZE(__builtins___float_float));
1.38 + struct float_block *new_block = (struct float_block *) __ALLOCATE(1, sizeof(struct float_block));
1.39 +
1.40 + /* Allocate a pointer-free block to hold the table of floats, linking to the
1.41 + previous block. */
1.42 +
1.43 + new_block->previous_block = current_block;
1.44 + new_block->floats = __ALLOCATEIM(NUM_PREALLOCATED, __INSTANCESIZE(__builtins___float_float));
1.45 +
1.46 + /* Reference the new block. */
1.47 +
1.48 floats_left = NUM_PREALLOCATED;
1.49 + current_block = new_block;
1.50 }
1.51
1.52 static __attr new_float(double n)
1.53 @@ -52,7 +70,7 @@
1.54 preallocate_floats();
1.55
1.56 floats_left--;
1.57 - obj = preallocated_floats;
1.58 + obj = (__ref) ¤t_block->floats[floats_left];
1.59
1.60 /* Initialise the instance. */
1.61
1.62 @@ -64,10 +82,6 @@
1.63 attr = __ATTRVALUE(obj);
1.64 __set_trailing_data(attr, __builtins___float_float, n);
1.65
1.66 - /* Reference the next float. */
1.67 -
1.68 - preallocated_floats = (__ref) ((char *) preallocated_floats + __INSTANCESIZE(__builtins___float_float));
1.69 -
1.70 return attr;
1.71 }
1.72