1.1 --- a/templates/native.c Wed Nov 23 01:01:40 2016 +0100
1.2 +++ b/templates/native.c Wed Nov 23 13:19:57 2016 +0100
1.3 @@ -355,6 +355,7 @@
1.4 __attr * const value = &__args[2];
1.5 /* self.__data__ interpreted as list */
1.6 __fragment *data = __load_via_object(self->value, __pos___data__).data;
1.7 + __fragment *newdata = data;
1.8 unsigned int size = data->size, capacity = data->capacity;
1.9 unsigned int n;
1.10
1.11 @@ -363,23 +364,28 @@
1.12 {
1.13 /* NOTE: Consider various restrictions on capacity increases. */
1.14 n = capacity ? capacity * 2 : 1;
1.15 - data = realloc(data, __FRAGMENT_SIZE(n));
1.16 - data->capacity = n;
1.17 + newdata = realloc(data, __FRAGMENT_SIZE(n));
1.18 + newdata->capacity = n;
1.19 }
1.20
1.21 /* Insert the new element and increment the list size. */
1.22 - data->attrs[size] = *value;
1.23 - data->size = size + 1;
1.24 + newdata->attrs[size] = *value;
1.25 + newdata->size = size + 1;
1.26 +
1.27 + /* Replace the __data__ attribute if appropriate. */
1.28 + if (newdata != data)
1.29 + __store_via_object(self->value, __pos___data__, ((__attr) {0, .data=newdata}));
1.30 return __builtins___none_None;
1.31 }
1.32
1.33 __attr __fn_native__list_concat(__attr __args[])
1.34 {
1.35 __attr * const self = &__args[1];
1.36 - __attr * const __other = &__args[2];
1.37 - /* self.__data__, __other.__data__ interpreted as list */
1.38 + __attr * const other = &__args[2];
1.39 + /* self.__data__, other.__data__ interpreted as list */
1.40 __fragment *data = __load_via_object(self->value, __pos___data__).data;
1.41 - __fragment *other_data = __load_via_object(__other->value, __pos___data__).data;
1.42 + __fragment *other_data = __load_via_object(other->value, __pos___data__).data;
1.43 + __fragment *newdata = data;
1.44 unsigned int size = data->size, capacity = data->capacity;
1.45 unsigned int other_size = other_data->size;
1.46 unsigned int i, j, n;
1.47 @@ -388,14 +394,18 @@
1.48 if (size + other_size >= capacity)
1.49 {
1.50 n = size + other_size;
1.51 - data = realloc(data, __FRAGMENT_SIZE(n));
1.52 - data->capacity = n;
1.53 + newdata = realloc(data, __FRAGMENT_SIZE(n));
1.54 + newdata->capacity = n;
1.55 }
1.56
1.57 /* Copy the elements from the other list and increment the list size. */
1.58 for (i = size, j = 0; j < other_size; i++, j++)
1.59 - data->attrs[i] = other_data->attrs[j];
1.60 - data->size = n;
1.61 + newdata->attrs[i] = other_data->attrs[j];
1.62 + newdata->size = n;
1.63 +
1.64 + /* Replace the __data__ attribute if appropriate. */
1.65 + if (newdata != data)
1.66 + __store_via_object(self->value, __pos___data__, ((__attr) {0, .data=newdata}));
1.67 return __builtins___none_None;
1.68 }
1.69