1
0
mirror of https://github.com/golang/go synced 2024-11-20 04:54:44 -07:00
go/src/pkg/runtime/slice.c

223 lines
4.3 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"
static int32 debug = 0;
// makeslice(nel int, cap int, width int) (ary []any);
2008-08-27 18:28:30 -06:00
void
runtime·makeslice(uint32 nel, uint32 cap, uint32 width, Slice ret)
2008-08-27 18:28:30 -06:00
{
uint64 size;
if(cap < nel)
cap = nel;
size = cap*width;
ret.len = nel;
2008-12-18 21:06:28 -07:00
ret.cap = cap;
ret.array = mal(size);
2008-08-27 18:28:30 -06:00
FLUSH(&ret);
2008-08-27 18:28:30 -06:00
if(debug) {
prints("makeslice: nel=");
runtime·printint(nel);
2008-08-27 18:28:30 -06:00
prints("; cap=");
runtime·printint(cap);
2008-08-27 18:28:30 -06:00
prints("; width=");
runtime·printint(width);
2008-08-27 18:28:30 -06:00
prints("; ret=");
runtime·printslice(ret);
2008-08-27 18:28:30 -06:00
prints("\n");
}
}
static void
throwslice(uint32 lb, uint32 hb, uint32 n)
{
prints("slice[");
runtime·printint(lb);
prints(":");
runtime·printint(hb);
prints("] of [");
runtime·printint(n);
prints("] array\n");
throw("array slice");
}
// sliceslice(old []any, lb int, hb int, width int) (ary []any);
2008-08-27 18:28:30 -06:00
void
runtime·sliceslice(Slice old, uint32 lb, uint32 hb, uint32 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=");
runtime·printslice(old);
2008-08-27 18:28:30 -06:00
prints("; lb=");
runtime·printint(lb);
2008-08-27 18:28:30 -06:00
prints("; hb=");
runtime·printint(hb);
2008-08-27 18:28:30 -06:00
prints("; width=");
runtime·printint(width);
2008-08-27 18:28:30 -06:00
prints("\n");
prints("oldarray: nel=");
runtime·printint(old.len);
2008-08-27 18:28:30 -06:00
prints("; cap=");
runtime·printint(old.cap);
2008-08-27 18:28:30 -06:00
prints("\n");
}
2008-12-18 21:06:28 -07:00
throwslice(lb, hb, old.cap);
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=");
runtime·printslice(old);
2008-08-27 18:28:30 -06:00
prints("; lb=");
runtime·printint(lb);
2008-08-27 18:28:30 -06:00
prints("; hb=");
runtime·printint(hb);
2008-08-27 18:28:30 -06:00
prints("; width=");
runtime·printint(width);
2008-08-27 18:28:30 -06:00
prints("; ret=");
runtime·printslice(ret);
2008-08-27 18:28:30 -06:00
prints("\n");
}
}
// slicearray(old *any, nel int, lb int, hb int, width int) (ary []any);
2008-08-27 18:28:30 -06:00
void
runtime·slicearray(byte* old, uint32 nel, uint32 lb, uint32 hb, uint32 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=");
runtime·printpointer(old);
2008-08-27 18:28:30 -06:00
prints("; nel=");
runtime·printint(nel);
2008-08-27 18:28:30 -06:00
prints("; lb=");
runtime·printint(lb);
2008-08-27 18:28:30 -06:00
prints("; hb=");
runtime·printint(hb);
2008-08-27 18:28:30 -06:00
prints("; width=");
runtime·printint(width);
2008-08-27 18:28:30 -06:00
prints("\n");
}
throwslice(lb, hb, nel);
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=");
runtime·printpointer(old);
2008-08-27 18:28:30 -06:00
prints("; nel=");
runtime·printint(nel);
2008-08-27 18:28:30 -06:00
prints("; lb=");
runtime·printint(lb);
2008-08-27 18:28:30 -06:00
prints("; hb=");
runtime·printint(hb);
2008-08-27 18:28:30 -06:00
prints("; width=");
runtime·printint(width);
2008-08-27 18:28:30 -06:00
prints("; ret=");
runtime·printslice(ret);
2008-08-27 18:28:30 -06:00
prints("\n");
}
}
// arraytoslice(old *any, nel int) (ary []any)
2008-08-27 18:28:30 -06:00
void
runtime·arraytoslice(byte* old, uint32 nel, 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
// new dope to old array
ret.len = nel;
2008-12-18 21:06:28 -07:00
ret.cap = nel;
ret.array = old;
2008-08-27 18:28:30 -06:00
FLUSH(&ret);
2008-08-27 18:28:30 -06:00
if(debug) {
prints("runtime·slicearrayp: old=");
runtime·printpointer(old);
2008-08-27 18:28:30 -06:00
prints("; ret=");
runtime·printslice(ret);
2008-08-27 18:28:30 -06:00
prints("\n");
}
}
// slicecopy(to any, fr any, wid uint32) int
void
runtime·slicecopy(Slice to, Slice fm, uintptr width, int32 ret)
{
if(fm.array == nil || fm.len == 0 ||
to.array == nil || to.len == 0 ||
width == 0) {
ret = 0;
goto out;
}
ret = fm.len;
if(to.len < ret)
ret = to.len;
memmove(to.array, fm.array, ret*width);
out:
FLUSH(&ret);
if(debug) {
prints("main·copy: to=");
runtime·printslice(to);
prints("; fm=");
runtime·printslice(fm);
prints("; width=");
runtime·printint(width);
prints("; ret=");
runtime·printint(ret);
prints("\n");
}
}
void
runtime·printslice(Slice a)
{
prints("[");
runtime·printint(a.len);
2008-12-18 21:06:28 -07:00
prints("/");
runtime·printint(a.cap);
prints("]");
runtime·printpointer(a.array);
}