2012-03-27 23:13:14 +00: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. */
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
#include "runtime.h"
|
2012-03-27 23:13:14 +00:00
|
|
|
#include "go-panic.h"
|
2014-09-21 17:33:12 +00:00
|
|
|
#include "go-type.h"
|
2012-03-27 23:13:14 +00:00
|
|
|
#include "array.h"
|
|
|
|
#include "arch.h"
|
|
|
|
#include "malloc.h"
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
__go_append (struct __go_open_array, void *, uintptr_t, uintptr_t)
|
|
|
|
__attribute__ ((no_split_stack));
|
|
|
|
|
|
|
|
struct __go_open_array
|
|
|
|
__go_append (struct __go_open_array a, void *bvalues, uintptr_t bcount,
|
|
|
|
uintptr_t element_size)
|
|
|
|
{
|
|
|
|
uintptr_t ucount;
|
2014-09-21 17:33:12 +00:00
|
|
|
intgo count;
|
2012-03-27 23:13:14 +00:00
|
|
|
|
|
|
|
if (bvalues == NULL || bcount == 0)
|
|
|
|
return a;
|
|
|
|
|
|
|
|
ucount = (uintptr_t) a.__count + bcount;
|
2014-09-21 17:33:12 +00:00
|
|
|
count = (intgo) ucount;
|
2012-03-27 23:13:14 +00:00
|
|
|
if ((uintptr_t) count != ucount || count <= a.__count)
|
|
|
|
runtime_panicstring ("append: slice overflow");
|
|
|
|
|
|
|
|
if (count > a.__capacity)
|
|
|
|
{
|
2014-09-21 17:33:12 +00:00
|
|
|
intgo m;
|
2015-08-28 15:33:40 +00:00
|
|
|
uintptr capmem;
|
2012-03-27 23:13:14 +00:00
|
|
|
void *n;
|
|
|
|
|
|
|
|
m = a.__capacity;
|
2014-09-21 17:33:12 +00:00
|
|
|
if (m + m < count)
|
|
|
|
m = count;
|
2012-03-27 23:13:14 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (a.__count < 1024)
|
|
|
|
m += m;
|
|
|
|
else
|
|
|
|
m += m / 4;
|
|
|
|
}
|
|
|
|
while (m < count);
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
if (element_size > 0 && (uintptr) m > MaxMem / element_size)
|
|
|
|
runtime_panicstring ("growslice: cap out of range");
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
capmem = runtime_roundupsize (m * element_size);
|
|
|
|
|
|
|
|
n = __go_alloc (capmem);
|
2012-03-27 23:13:14 +00:00
|
|
|
__builtin_memcpy (n, a.__values, a.__count * element_size);
|
|
|
|
|
|
|
|
a.__values = n;
|
|
|
|
a.__capacity = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
__builtin_memmove ((char *) a.__values + a.__count * element_size,
|
|
|
|
bvalues, bcount * element_size);
|
|
|
|
a.__count = count;
|
|
|
|
return a;
|
|
|
|
}
|