2010-12-03 05:34:57 +01:00
|
|
|
/* go-append.c -- the go builtin append function.
|
|
|
|
|
|
|
|
Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
Use of this source code is governed by a BSD-style
|
|
|
|
license that can be found in the LICENSE file. */
|
|
|
|
|
2012-11-01 04:02:13 +01:00
|
|
|
#include "runtime.h"
|
2010-12-17 07:33:41 +01:00
|
|
|
#include "go-panic.h"
|
2012-11-01 04:02:13 +01:00
|
|
|
#include "go-type.h"
|
2010-12-03 05:34:57 +01:00
|
|
|
#include "array.h"
|
2011-10-27 01:57:58 +02:00
|
|
|
#include "arch.h"
|
2010-12-03 05:34:57 +01:00
|
|
|
#include "malloc.h"
|
|
|
|
|
2010-12-17 07:33:41 +01:00
|
|
|
/* We should be OK if we don't split the stack here, since the only
|
|
|
|
libc functions we call are memcpy and memmove. If we don't do
|
|
|
|
this, we will always split the stack, because of memcpy and
|
|
|
|
memmove. */
|
|
|
|
extern struct __go_open_array
|
2011-04-18 19:31:00 +02:00
|
|
|
__go_append (struct __go_open_array, void *, uintptr_t, uintptr_t)
|
2010-12-17 07:33:41 +01:00
|
|
|
__attribute__ ((no_split_stack));
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
struct __go_open_array
|
2011-04-13 23:00:59 +02:00
|
|
|
__go_append (struct __go_open_array a, void *bvalues, uintptr_t bcount,
|
|
|
|
uintptr_t element_size)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
2011-04-13 23:00:59 +02:00
|
|
|
uintptr_t ucount;
|
2013-10-03 01:49:39 +02:00
|
|
|
intgo count;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2010-12-17 07:33:41 +01:00
|
|
|
if (bvalues == NULL || bcount == 0)
|
2010-12-03 05:34:57 +01:00
|
|
|
return a;
|
|
|
|
|
2011-04-13 23:00:59 +02:00
|
|
|
ucount = (uintptr_t) a.__count + bcount;
|
2013-10-03 01:49:39 +02:00
|
|
|
count = (intgo) ucount;
|
2011-04-13 23:00:59 +02:00
|
|
|
if ((uintptr_t) count != ucount || count <= a.__count)
|
2011-11-30 01:21:52 +01:00
|
|
|
runtime_panicstring ("append: slice overflow");
|
2010-12-17 07:33:41 +01:00
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
if (count > a.__capacity)
|
|
|
|
{
|
2013-10-03 01:49:39 +02:00
|
|
|
intgo m;
|
2014-06-07 00:37:27 +02:00
|
|
|
uintptr capmem;
|
2010-12-17 07:33:41 +01:00
|
|
|
void *n;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
m = a.__capacity;
|
2013-10-03 01:49:39 +02:00
|
|
|
if (m + m < count)
|
|
|
|
m = count;
|
2010-12-03 05:34:57 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (a.__count < 1024)
|
|
|
|
m += m;
|
|
|
|
else
|
|
|
|
m += m / 4;
|
|
|
|
}
|
|
|
|
while (m < count);
|
|
|
|
}
|
|
|
|
|
2013-10-03 01:49:39 +02:00
|
|
|
if (element_size > 0 && (uintptr) m > MaxMem / element_size)
|
2012-09-28 23:25:20 +02:00
|
|
|
runtime_panicstring ("growslice: cap out of range");
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
capmem = runtime_roundupsize (m * element_size);
|
|
|
|
|
|
|
|
n = __go_alloc (capmem);
|
2010-12-17 07:33:41 +01:00
|
|
|
__builtin_memcpy (n, a.__values, a.__count * element_size);
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2010-12-17 07:33:41 +01:00
|
|
|
a.__values = n;
|
|
|
|
a.__capacity = m;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
__builtin_memmove ((char *) a.__values + a.__count * element_size,
|
2010-12-17 07:33:41 +01:00
|
|
|
bvalues, bcount * element_size);
|
2010-12-03 05:34:57 +01:00
|
|
|
a.__count = count;
|
|
|
|
return a;
|
|
|
|
}
|