2011-06-14 15:51:16 +02:00
|
|
|
/* go-make-slice.c -- make a slice.
|
|
|
|
|
|
|
|
Copyright 2011 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 <stdint.h>
|
|
|
|
|
2012-11-01 04:02:13 +01:00
|
|
|
#include "runtime.h"
|
2011-06-14 15:51:16 +02:00
|
|
|
#include "go-alloc.h"
|
|
|
|
#include "go-assert.h"
|
|
|
|
#include "go-panic.h"
|
|
|
|
#include "go-type.h"
|
|
|
|
#include "array.h"
|
2011-10-27 01:57:58 +02:00
|
|
|
#include "arch.h"
|
2011-06-14 15:51:16 +02:00
|
|
|
#include "malloc.h"
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
/* Dummy word to use as base pointer for make([]T, 0).
|
|
|
|
Since you cannot take the address of such a slice,
|
|
|
|
you can't tell that they all have the same base pointer. */
|
|
|
|
uintptr runtime_zerobase;
|
|
|
|
|
2011-06-14 15:51:16 +02:00
|
|
|
struct __go_open_array
|
|
|
|
__go_make_slice2 (const struct __go_type_descriptor *td, uintptr_t len,
|
|
|
|
uintptr_t cap)
|
|
|
|
{
|
|
|
|
const struct __go_slice_type* std;
|
2012-11-01 04:02:13 +01:00
|
|
|
intgo ilen;
|
|
|
|
intgo icap;
|
2011-06-14 15:51:16 +02:00
|
|
|
uintptr_t size;
|
|
|
|
struct __go_open_array ret;
|
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
__go_assert ((td->__code & GO_CODE_MASK) == GO_SLICE);
|
2011-06-14 15:51:16 +02:00
|
|
|
std = (const struct __go_slice_type *) td;
|
|
|
|
|
2012-11-01 04:02:13 +01:00
|
|
|
ilen = (intgo) len;
|
2013-10-11 02:46:57 +02:00
|
|
|
if (ilen < 0
|
|
|
|
|| (uintptr_t) ilen != len
|
|
|
|
|| (std->__element_type->__size > 0
|
|
|
|
&& len > MaxMem / std->__element_type->__size))
|
2011-11-30 01:21:52 +01:00
|
|
|
runtime_panicstring ("makeslice: len out of range");
|
2011-06-14 15:51:16 +02:00
|
|
|
|
2012-11-01 04:02:13 +01:00
|
|
|
icap = (intgo) cap;
|
2011-06-14 15:51:16 +02:00
|
|
|
if (cap < len
|
|
|
|
|| (uintptr_t) icap != cap
|
2011-09-21 00:06:20 +02:00
|
|
|
|| (std->__element_type->__size > 0
|
2012-09-28 23:25:20 +02:00
|
|
|
&& cap > MaxMem / std->__element_type->__size))
|
2011-11-30 01:21:52 +01:00
|
|
|
runtime_panicstring ("makeslice: cap out of range");
|
2011-06-14 15:51:16 +02:00
|
|
|
|
|
|
|
ret.__count = ilen;
|
|
|
|
ret.__capacity = icap;
|
|
|
|
|
|
|
|
size = cap * std->__element_type->__size;
|
2012-11-21 08:03:38 +01:00
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
ret.__values = &runtime_zerobase;
|
|
|
|
else if ((std->__element_type->__code & GO_NO_POINTERS) != 0)
|
2013-11-06 20:49:01 +01:00
|
|
|
ret.__values =
|
|
|
|
runtime_mallocgc (size,
|
|
|
|
(uintptr) std->__element_type | TypeInfo_Array,
|
|
|
|
FlagNoScan);
|
2012-11-21 08:03:38 +01:00
|
|
|
else
|
2013-11-06 20:49:01 +01:00
|
|
|
ret.__values =
|
|
|
|
runtime_mallocgc (size,
|
|
|
|
(uintptr) std->__element_type | TypeInfo_Array,
|
|
|
|
0);
|
2011-06-14 15:51:16 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct __go_open_array
|
|
|
|
__go_make_slice1 (const struct __go_type_descriptor *td, uintptr_t len)
|
|
|
|
{
|
|
|
|
return __go_make_slice2 (td, len, len);
|
|
|
|
}
|
2012-02-08 07:18:41 +01:00
|
|
|
|
|
|
|
struct __go_open_array
|
|
|
|
__go_make_slice2_big (const struct __go_type_descriptor *td, uint64_t len,
|
|
|
|
uint64_t cap)
|
|
|
|
{
|
|
|
|
uintptr_t slen;
|
|
|
|
uintptr_t scap;
|
|
|
|
|
|
|
|
slen = (uintptr_t) len;
|
|
|
|
if ((uint64_t) slen != len)
|
|
|
|
runtime_panicstring ("makeslice: len out of range");
|
|
|
|
|
|
|
|
scap = (uintptr_t) cap;
|
|
|
|
if ((uint64_t) scap != cap)
|
|
|
|
runtime_panicstring ("makeslice: cap out of range");
|
|
|
|
|
|
|
|
return __go_make_slice2 (td, slen, scap);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct __go_open_array
|
|
|
|
__go_make_slice1_big (const struct __go_type_descriptor *td, uint64_t len)
|
|
|
|
{
|
|
|
|
return __go_make_slice2_big (td, len, len);
|
|
|
|
}
|