From 66a603c9869a4b7eaebb08e39c16d0947a961574 Mon Sep 17 00:00:00 2001 From: Ken Thompson Date: Wed, 27 Aug 2008 17:28:30 -0700 Subject: arrays R=r OCL=14603 CL=14603 --- src/runtime/Makefile | 1 + src/runtime/array.c | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/runtime/runtime.h | 9 +++ 3 files changed, 175 insertions(+) create mode 100644 src/runtime/array.c (limited to 'src/runtime') 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; -- cgit v1.3-6-g1900