2012-01-13 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / go-make-slice.c
blob765e7c021b7cdaaa3c0080b7319a1e6ec50983a6
1 /* go-make-slice.c -- make a slice.
3 Copyright 2011 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
7 #include <stdint.h>
9 #include "go-alloc.h"
10 #include "go-assert.h"
11 #include "go-panic.h"
12 #include "go-type.h"
13 #include "array.h"
14 #include "runtime.h"
15 #include "arch.h"
16 #include "malloc.h"
18 struct __go_open_array
19 __go_make_slice2 (const struct __go_type_descriptor *td, uintptr_t len,
20 uintptr_t cap)
22 const struct __go_slice_type* std;
23 int ilen;
24 int icap;
25 uintptr_t size;
26 struct __go_open_array ret;
27 unsigned int flag;
29 __go_assert (td->__code == GO_SLICE);
30 std = (const struct __go_slice_type *) td;
32 ilen = (int) len;
33 if (ilen < 0 || (uintptr_t) ilen != len)
34 runtime_panicstring ("makeslice: len out of range");
36 icap = (int) cap;
37 if (cap < len
38 || (uintptr_t) icap != cap
39 || (std->__element_type->__size > 0
40 && cap > (uintptr_t) -1U / std->__element_type->__size))
41 runtime_panicstring ("makeslice: cap out of range");
43 ret.__count = ilen;
44 ret.__capacity = icap;
46 size = cap * std->__element_type->__size;
47 flag = ((std->__element_type->__code & GO_NO_POINTERS) != 0
48 ? FlagNoPointers
49 : 0);
50 ret.__values = runtime_mallocgc (size, flag, 1, 1);
52 return ret;
55 struct __go_open_array
56 __go_make_slice1 (const struct __go_type_descriptor *td, uintptr_t len)
58 return __go_make_slice2 (td, len, len);