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. */
|
|
|
|
|
|
|
|
#include "go-type.h"
|
2010-12-17 07:33:41 +01:00
|
|
|
#include "go-panic.h"
|
2010-12-03 05:34:57 +01:00
|
|
|
#include "array.h"
|
|
|
|
#include "runtime.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;
|
2010-12-03 05:34:57 +01:00
|
|
|
int count;
|
|
|
|
|
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;
|
2010-12-03 05:34:57 +01:00
|
|
|
count = (int) 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)
|
|
|
|
{
|
|
|
|
int m;
|
2010-12-17 07:33:41 +01:00
|
|
|
void *n;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
m = a.__capacity;
|
|
|
|
if (m == 0)
|
2010-12-17 07:33:41 +01:00
|
|
|
m = (int) bcount;
|
2010-12-03 05:34:57 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (a.__count < 1024)
|
|
|
|
m += m;
|
|
|
|
else
|
|
|
|
m += m / 4;
|
|
|
|
}
|
|
|
|
while (m < count);
|
|
|
|
}
|
|
|
|
|
2010-12-17 07:33:41 +01:00
|
|
|
n = __go_alloc (m * element_size);
|
|
|
|
__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;
|
|
|
|
}
|