diff options
author | Rob Pike <r@golang.org> | 2009-06-09 09:53:44 -0700 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2009-06-09 09:53:44 -0700 |
commit | 7249ea4df2b4f12a4e7ed446f270cea87e4ffd34 (patch) | |
tree | 7032a11d0cac2ae4d3e90f7a189b575b5a50f848 /src/pkg/runtime/array.c | |
parent | acf6ef7a82b3fe61516a1bac4563706552bdf078 (diff) | |
download | golang-7249ea4df2b4f12a4e7ed446f270cea87e4ffd34.tar.gz |
mv src/lib to src/pkg
tests: all.bash passes, gobuild still works, godoc still works.
R=rsc
OCL=30096
CL=30102
Diffstat (limited to 'src/pkg/runtime/array.c')
-rw-r--r-- | src/pkg/runtime/array.c | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/src/pkg/runtime/array.c b/src/pkg/runtime/array.c new file mode 100644 index 000000000..bbd57b03e --- /dev/null +++ b/src/pkg/runtime/array.c @@ -0,0 +1,175 @@ +// 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; + +// newarray(nel int, cap int, width int) (ary []any); +void +sys·newarray(uint32 nel, uint32 cap, uint32 width, Array ret) +{ + uint64 size; + + if(cap < nel) + cap = nel; + size = cap*width; + + ret.nel = nel; + ret.cap = cap; + ret.array = mal(size); + + FLUSH(&ret); + + if(debug) { + prints("newarray: nel="); + sys·printint(nel); + prints("; cap="); + sys·printint(cap); + prints("; width="); + sys·printint(width); + prints("; ret="); + sys·printarray(ret); + prints("\n"); + } +} + +static void +throwslice(uint32 lb, uint32 hb, uint32 n) +{ + prints("slice["); + sys·printint(lb); + prints(":"); + sys·printint(hb); + prints("] of ["); + sys·printint(n); + prints("] array\n"); + throw("array slice"); +} + +// arraysliced(old []any, lb int, hb int, width int) (ary []any); +void +sys·arraysliced(Array old, uint32 lb, uint32 hb, uint32 width, Array ret) +{ + + if(hb > old.cap || lb > hb) { + if(debug) { + prints("sys·arraysliced: old="); + sys·printarray(old); + prints("; lb="); + sys·printint(lb); + prints("; hb="); + sys·printint(hb); + prints("; width="); + sys·printint(width); + prints("\n"); + + prints("oldarray: nel="); + sys·printint(old.nel); + prints("; cap="); + sys·printint(old.cap); + prints("\n"); + } + throwslice(lb, hb, old.cap); + } + + // new array is inside old array + ret.nel = hb-lb; + ret.cap = old.cap - lb; + ret.array = old.array + lb*width; + + FLUSH(&ret); + + if(debug) { + prints("sys·arraysliced: old="); + sys·printarray(old); + prints("; lb="); + sys·printint(lb); + prints("; hb="); + sys·printint(hb); + prints("; width="); + sys·printint(width); + prints("; ret="); + sys·printarray(ret); + prints("\n"); + } +} + +// arrayslices(old *any, nel int, lb int, hb int, width int) (ary []any); +void +sys·arrayslices(byte* old, uint32 nel, uint32 lb, uint32 hb, uint32 width, Array ret) +{ + + if(hb > nel || lb > hb) { + if(debug) { + prints("sys·arrayslices: old="); + sys·printpointer(old); + prints("; nel="); + sys·printint(nel); + prints("; lb="); + sys·printint(lb); + prints("; hb="); + sys·printint(hb); + prints("; width="); + sys·printint(width); + prints("\n"); + } + throwslice(lb, hb, nel); + } + + // new array is inside old array + ret.nel = hb-lb; + ret.cap = nel-lb; + ret.array = old + lb*width; + + FLUSH(&ret); + + if(debug) { + prints("sys·arrayslices: old="); + sys·printpointer(old); + prints("; nel="); + sys·printint(nel); + prints("; lb="); + sys·printint(lb); + prints("; hb="); + sys·printint(hb); + prints("; width="); + sys·printint(width); + prints("; ret="); + sys·printarray(ret); + prints("\n"); + } +} + +// arrays2d(old *any, nel int) (ary []any) +void +sys·arrays2d(byte* old, uint32 nel, Array ret) +{ + + // new dope to old array + ret.nel = nel; + ret.cap = nel; + ret.array = old; + + FLUSH(&ret); + + if(debug) { + prints("sys·arrays2d: old="); + sys·printpointer(old); + prints("; ret="); + sys·printarray(ret); + prints("\n"); + } +} + +void +sys·printarray(Array a) +{ + prints("["); + sys·printint(a.nel); + prints("/"); + sys·printint(a.cap); + prints("]"); + sys·printpointer(a.array); +} |