1
0
mirror of https://github.com/golang/go synced 2024-10-05 04:21:22 -06:00
go/src/pkg/runtime/slice.c

312 lines
5.9 KiB
C
Raw Normal View History

2008-08-27 18:28:30 -06:00
// Copyright 2009 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 "runtime.h"
#include "type.h"
#include "malloc.h"
2008-08-27 18:28:30 -06:00
static int32 debug = 0;
static void makeslice(SliceType*, int32, int32, Slice*);
void ·slicecopy(Slice to, Slice fm, uintptr width, int32 ret);
// see also unsafe·NewArray
// makeslice(typ *Type, len, cap int64) (ary []any);
2008-08-27 18:28:30 -06:00
void
·makeslice(SliceType *t, int64 len, int64 cap, Slice ret)
2008-08-27 18:28:30 -06:00
{
if(len < 0 || (int32)len != len)
panicstring("makeslice: len out of range");
if(cap < len || (int32)cap != cap || cap > ((uintptr)-1) / t->elem->size)
panicstring("makeslice: cap out of range");
2008-08-27 18:28:30 -06:00
makeslice(t, len, cap, &ret);
if(debug) {
printf("makeslice(%S, %D, %D); ret=",
*t->string, len, cap);
·printslice(ret);
}
}
static void
makeslice(SliceType *t, int32 len, int32 cap, Slice *ret)
{
uintptr size;
size = cap*t->elem->size;
2008-08-27 18:28:30 -06:00
ret->len = len;
ret->cap = cap;
if((t->elem->kind&KindNoPointers))
ret->array = mallocgc(size, RefNoPointers, 1, 1);
else
ret->array = mal(size);
}
2008-08-27 18:28:30 -06:00
static void appendslice(SliceType*, Slice, Slice, Slice*);
2008-08-27 18:28:30 -06:00
// append(type *Type, n int, old []T, ...,) []T
#pragma textflag 7
void
·append(SliceType *t, int32 n, Slice old, ...)
{
Slice sl;
Slice *ret;
sl.len = n;
sl.array = (byte*)(&old+1);
ret = (Slice*)(sl.array + ((t->elem->size*n+sizeof(uintptr)-1) & ~(sizeof(uintptr)-1)));
appendslice(t, old, sl, ret);
}
// appendslice(type *Type, x, y, []T) []T
void
·appendslice(SliceType *t, Slice x, Slice y, Slice ret)
{
appendslice(t, x, y, &ret);
}
static void
appendslice(SliceType *t, Slice x, Slice y, Slice *ret)
{
Slice newx;
int32 m;
uintptr w;
if(x.len+y.len < x.len)
throw("append: slice overflow");
w = t->elem->size;
if(x.len+y.len > x.cap) {
m = x.cap;
if(m == 0)
m = y.len;
else {
do {
if(x.len < 1024)
m += m;
else
m += m/4;
} while(m < x.len+y.len);
}
makeslice(t, x.len, m, &newx);
memmove(newx.array, x.array, x.len*w);
x = newx;
2008-08-27 18:28:30 -06:00
}
memmove(x.array+x.len*w, y.array, y.len*w);
x.len += y.len;
*ret = x;
2008-08-27 18:28:30 -06:00
}
// sliceslice(old []any, lb uint64, hb uint64, width uint64) (ary []any);
2008-08-27 18:28:30 -06:00
void
·sliceslice(Slice old, uint64 lb, uint64 hb, uint64 width, Slice ret)
2008-08-27 18:28:30 -06:00
{
2008-12-18 21:06:28 -07:00
if(hb > old.cap || lb > hb) {
2008-08-27 18:28:30 -06:00
if(debug) {
prints("runtime.sliceslice: old=");
·printslice(old);
2008-08-27 18:28:30 -06:00
prints("; lb=");
·printint(lb);
2008-08-27 18:28:30 -06:00
prints("; hb=");
·printint(hb);
2008-08-27 18:28:30 -06:00
prints("; width=");
·printint(width);
2008-08-27 18:28:30 -06:00
prints("\n");
prints("oldarray: nel=");
·printint(old.len);
2008-08-27 18:28:30 -06:00
prints("; cap=");
·printint(old.cap);
2008-08-27 18:28:30 -06:00
prints("\n");
}
·panicslice();
2008-08-27 18:28:30 -06:00
}
// new array is inside old array
ret.len = hb - lb;
2008-12-18 21:06:28 -07:00
ret.cap = old.cap - lb;
ret.array = old.array + lb*width;
2008-08-27 18:28:30 -06:00
FLUSH(&ret);
2008-08-27 18:28:30 -06:00
if(debug) {
prints("runtime.sliceslice: old=");
·printslice(old);
2008-08-27 18:28:30 -06:00
prints("; lb=");
·printint(lb);
2008-08-27 18:28:30 -06:00
prints("; hb=");
·printint(hb);
2008-08-27 18:28:30 -06:00
prints("; width=");
·printint(width);
2008-08-27 18:28:30 -06:00
prints("; ret=");
·printslice(ret);
2008-08-27 18:28:30 -06:00
prints("\n");
}
}
// sliceslice1(old []any, lb uint64, width uint64) (ary []any);
void
·sliceslice1(Slice old, uint64 lb, uint64 width, Slice ret)
{
if(lb > old.len) {
if(debug) {
prints("runtime.sliceslice: old=");
·printslice(old);
prints("; lb=");
·printint(lb);
prints("; width=");
·printint(width);
prints("\n");
prints("oldarray: nel=");
·printint(old.len);
prints("; cap=");
·printint(old.cap);
prints("\n");
}
·panicslice();
}
// new array is inside old array
ret.len = old.len - lb;
ret.cap = old.cap - lb;
ret.array = old.array + lb*width;
FLUSH(&ret);
if(debug) {
prints("runtime.sliceslice: old=");
·printslice(old);
prints("; lb=");
·printint(lb);
prints("; width=");
·printint(width);
prints("; ret=");
·printslice(ret);
prints("\n");
}
}
// slicearray(old *any, nel uint64, lb uint64, hb uint64, width uint64) (ary []any);
2008-08-27 18:28:30 -06:00
void
·slicearray(byte* old, uint64 nel, uint64 lb, uint64 hb, uint64 width, Slice ret)
2008-08-27 18:28:30 -06:00
{
if(nel > 0 && old == nil) {
// crash if old == nil.
// could give a better message
// but this is consistent with all the in-line checks
// that the compiler inserts for other uses.
*old = 0;
}
2008-08-27 18:28:30 -06:00
if(hb > nel || lb > hb) {
if(debug) {
prints("runtime.slicearray: old=");
·printpointer(old);
2008-08-27 18:28:30 -06:00
prints("; nel=");
·printint(nel);
2008-08-27 18:28:30 -06:00
prints("; lb=");
·printint(lb);
2008-08-27 18:28:30 -06:00
prints("; hb=");
·printint(hb);
2008-08-27 18:28:30 -06:00
prints("; width=");
·printint(width);
2008-08-27 18:28:30 -06:00
prints("\n");
}
·panicslice();
2008-08-27 18:28:30 -06:00
}
// new array is inside old array
ret.len = hb-lb;
2008-12-18 21:06:28 -07:00
ret.cap = nel-lb;
ret.array = old + lb*width;
2008-08-27 18:28:30 -06:00
FLUSH(&ret);
2008-08-27 18:28:30 -06:00
if(debug) {
prints("runtime.slicearray: old=");
·printpointer(old);
2008-08-27 18:28:30 -06:00
prints("; nel=");
·printint(nel);
2008-08-27 18:28:30 -06:00
prints("; lb=");
·printint(lb);
2008-08-27 18:28:30 -06:00
prints("; hb=");
·printint(hb);
2008-08-27 18:28:30 -06:00
prints("; width=");
·printint(width);
2008-08-27 18:28:30 -06:00
prints("; ret=");
·printslice(ret);
2008-08-27 18:28:30 -06:00
prints("\n");
}
}
// slicecopy(to any, fr any, wid uint32) int
void
·slicecopy(Slice to, Slice fm, uintptr width, int32 ret)
{
if(fm.len == 0 || to.len == 0 || width == 0) {
ret = 0;
goto out;
}
ret = fm.len;
if(to.len < ret)
ret = to.len;
if(ret == 1 && width == 1) { // common case worth about 2x to do here
*to.array = *fm.array; // known to be a byte pointer
} else {
memmove(to.array, fm.array, ret*width);
}
out:
FLUSH(&ret);
if(debug) {
prints("main·copy: to=");
·printslice(to);
prints("; fm=");
·printslice(fm);
prints("; width=");
·printint(width);
prints("; ret=");
·printint(ret);
prints("\n");
}
}
void
·slicestringcopy(Slice to, String fm, int32 ret)
{
if(fm.len == 0 || to.len == 0) {
ret = 0;
goto out;
}
ret = fm.len;
if(to.len < ret)
ret = to.len;
memmove(to.array, fm.str, ret);
out:
FLUSH(&ret);
}
void
·printslice(Slice a)
{
prints("[");
·printint(a.len);
2008-12-18 21:06:28 -07:00
prints("/");
·printint(a.cap);
prints("]");
·printpointer(a.array);
}