aboutsummaryrefslogtreecommitdiff
path: root/src/runtime
diff options
context:
space:
mode:
authorKen Thompson <ken@golang.org>2008-08-27 17:28:30 -0700
committerKen Thompson <ken@golang.org>2008-08-27 17:28:30 -0700
commit66a603c9869a4b7eaebb08e39c16d0947a961574 (patch)
tree6ae6ed7d2f31f727c0a999d0455f307b02e4d22d /src/runtime
parent30fd44cf9d0fcfc07c08d21d01d0c8b9a431d2cc (diff)
downloadgo-66a603c9869a4b7eaebb08e39c16d0947a961574.tar.xz
arrays
R=r OCL=14603 CL=14603
Diffstat (limited to 'src/runtime')
-rw-r--r--src/runtime/Makefile1
-rw-r--r--src/runtime/array.c165
-rw-r--r--src/runtime/runtime.h9
3 files changed, 175 insertions, 0 deletions
diff --git a/src/runtime/Makefile b/src/runtime/Makefile
index 1dc29598e4..d70a4baf77 100644
--- a/src/runtime/Makefile
+++ b/src/runtime/Makefile
@@ -20,6 +20,7 @@ LIBOFILES=\
runtime.$O\
map.$O\
chan.$O\
+ array.$O\
print.$O\
rune.$O\
proc.$O\
diff --git a/src/runtime/array.c b/src/runtime/array.c
new file mode 100644
index 0000000000..548886e6cf
--- /dev/null
+++ b/src/runtime/array.c
@@ -0,0 +1,165 @@
+// 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 uint32, cap uint32, width uint32) (ary *[]any);
+void
+sys·newarray(uint32 nel, uint32 cap, uint32 width, Array* ret)
+{
+ Array *d;
+ uint64 size;
+
+ if(cap < nel)
+ cap = nel;
+ size = cap*width;
+
+ d = mal(sizeof(*d) - sizeof(d->b) + size);
+ d->nel = nel;
+ d->cap = cap;
+ d->array = d->b;
+
+ ret = d;
+ FLUSH(&d);
+
+ if(debug) {
+ prints("newarray: nel=");
+ sys·printint(nel);
+ prints("; cap=");
+ sys·printint(cap);
+ prints("; width=");
+ sys·printint(width);
+ prints("; ret=");
+ sys·printpointer(ret);
+ prints("\n");
+ }
+}
+
+// arraysliced(old *[]any, lb uint32, hb uint32, width uint32) (ary *[]any);
+void
+sys·arraysliced(Array* old, uint32 lb, uint32 hb, uint32 width, Array* ret)
+{
+ Array *d;
+
+ if(hb > old->cap || lb > hb) {
+ if(debug) {
+ prints("sys·arrayslices: old=");
+ sys·printpointer(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");
+ }
+ throw("sys·arraysliced: new size exceeds old size");
+ }
+
+ // new array is inside old array
+ d = mal(sizeof(*d) - sizeof(d->b));
+ d->nel = hb-lb;
+ d->cap = old->cap - lb;
+ d->array = old->array + lb*width;
+
+ ret = d;
+ FLUSH(&d);
+
+ if(debug) {
+ prints("sys·arrayslices: old=");
+ sys·printpointer(old);
+ prints("; lb=");
+ sys·printint(lb);
+ prints("; hb=");
+ sys·printint(hb);
+ prints("; width=");
+ sys·printint(width);
+ prints("; ret=");
+ sys·printpointer(ret);
+ prints("\n");
+ }
+}
+
+// arrayslices(old *any, nel uint32, lb uint32, hb uint32, width uint32) (ary *[]any);
+void
+sys·arrayslices(byte* old, uint32 nel, uint32 lb, uint32 hb, uint32 width, Array* ret)
+{
+ Array *d;
+
+ 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");
+ }
+ throw("sys·arrayslices: new size exceeds cap");
+ }
+
+ // new array is inside old array
+ d = mal(sizeof(*d) - sizeof(d->b));
+ d->nel = hb-lb;
+ d->cap = nel-lb;
+ d->array = old + lb*width;
+
+ ret = d;
+ FLUSH(&d);
+
+ 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·printpointer(ret);
+ prints("\n");
+ }
+}
+
+// arrays2d(old *any, nel uint32) (ary *[]any)
+void
+sys·arrays2d(byte* old, uint32 nel, Array* ret)
+{
+ Array *d;
+
+ // new dope to old array
+ d = mal(sizeof(*d) - sizeof(d->b));
+ d->nel = nel;
+ d->cap = nel;
+ d->array = old;
+
+ ret = d;
+ FLUSH(&d);
+
+ if(debug) {
+ prints("sys·arrays2d: old=");
+ sys·printpointer(old);
+ prints("; nel=");
+ sys·printint(nel);
+ prints("; ret=");
+ sys·printpointer(ret);
+ prints("\n");
+ }
+}
diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h
index 591af542dc..c7471cf3e6 100644
--- a/src/runtime/runtime.h
+++ b/src/runtime/runtime.h
@@ -37,6 +37,7 @@ typedef struct String *string;
typedef struct Sigs Sigs;
typedef struct Sigi Sigi;
typedef struct Map Map;
+typedef struct Array Array;
typedef struct Gobuf Gobuf;
typedef struct G G;
typedef struct M M;
@@ -99,6 +100,14 @@ struct Sigi
uint32 hash;
uint32 offset;
};
+
+struct Array
+{ // must not move anything
+ byte* array; // actual data
+ uint32 nel; // number of elements
+ uint32 cap; // allocate3d number of elements
+ byte b[8]; // actual array - may not be contig
+};
struct Map
{
Sigi* si;