diff options
| author | Ken Thompson <ken@golang.org> | 2008-07-13 14:29:46 -0700 |
|---|---|---|
| committer | Ken Thompson <ken@golang.org> | 2008-07-13 14:29:46 -0700 |
| commit | 594175d0b5e1c183b0d1642dabbc85437f07407b (patch) | |
| tree | 4ec40b6342c5afa0bdd3cf4f48033e13195ab1e3 /src/runtime | |
| parent | 3f982aeaf6c5809d2ec61b07944a72ac2f6aa5a9 (diff) | |
| download | go-594175d0b5e1c183b0d1642dabbc85437f07407b.tar.xz | |
chan
SVN=126958
Diffstat (limited to 'src/runtime')
| -rw-r--r-- | src/runtime/Makefile | 1 | ||||
| -rw-r--r-- | src/runtime/chan.c | 75 | ||||
| -rw-r--r-- | src/runtime/map.c | 154 | ||||
| -rw-r--r-- | src/runtime/runtime.c | 147 | ||||
| -rw-r--r-- | src/runtime/runtime.h | 103 |
5 files changed, 286 insertions, 194 deletions
diff --git a/src/runtime/Makefile b/src/runtime/Makefile index 5b7da999c1..d7f351ede1 100644 --- a/src/runtime/Makefile +++ b/src/runtime/Makefile @@ -19,6 +19,7 @@ LIBOFILES=\ sys_$(GOARCH)_$(GOOS).$O\ runtime.$O\ map.$O\ + chan.$O\ print.$O\ rune.$O\ string.$O\ diff --git a/src/runtime/chan.c b/src/runtime/chan.c new file mode 100644 index 0000000000..b491bbda32 --- /dev/null +++ b/src/runtime/chan.c @@ -0,0 +1,75 @@ +// 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 = 1; + +typedef struct Hchan Hchan; + +struct Hchan +{ + uint32 elemsize; + uint32 hint; + uint32 eo; + Alg* elemalg; +}; + +// newchan(elemsize uint32, elemalg uint32, hint uint32) (hchan *chan any); +void +sys·newchan(uint32 elemsize, uint32 elemalg, uint32 hint, + Hchan* ret) +{ + Hchan *c; + + if(elemalg >= nelem(algarray)) { + prints("0<="); + sys·printint(elemalg); + prints("<"); + sys·printint(nelem(algarray)); + prints("\n"); + + throw("sys·newchan: elem algorithm out of range"); + } + + c = mal(sizeof(*c)); + + c->elemsize = elemsize; + c->elemalg = &algarray[elemalg]; + c->hint = hint; + + // these calculations are compiler dependent + c->eo = rnd(sizeof(c), elemsize); + + ret = c; + FLUSH(&ret); + + if(debug) { + prints("newchan: chan="); + sys·printpointer(c); + prints("; elemsize="); + sys·printint(elemsize); + prints("; elemalg="); + sys·printint(elemalg); + prints("; hint="); + sys·printint(hint); + prints("\n"); + } +} + +// chansend(hchan *chan any, elem any); +void +sys·chansend(Hchan* c, ...) +{ + byte *ae; + + ae = (byte*)&c + c->eo; + if(debug) { + prints("chansend: chan="); + sys·printpointer(c); + prints("; elem="); + c->elemalg->print(c->elemsize, ae); + prints("\n"); + } +} diff --git a/src/runtime/map.c b/src/runtime/map.c index 0dd655b16e..93a985f15e 100644 --- a/src/runtime/map.c +++ b/src/runtime/map.c @@ -4,17 +4,10 @@ #include "runtime.h" +static int32 debug = 0; + typedef struct Link Link; typedef struct Hmap Hmap; -typedef struct Alg Alg; - -struct Alg -{ - uint64 (*hash)(uint32, void*); - uint32 (*equal)(uint32, void*, void*); - void (*print)(uint32, void*); - void (*copy)(uint32, void*, void*); -}; struct Link { @@ -28,154 +21,15 @@ struct Hmap uint32 keysize; uint32 valsize; uint32 hint; - Alg* keyalg; - Alg* valalg; uint32 valoffset; uint32 ko; uint32 vo; uint32 po; + Alg* keyalg; + Alg* valalg; Link* link; }; -static uint64 -memhash(uint32 s, void *a) -{ - prints("memhash\n"); - return 0x12345; -} - -static uint32 -memequal(uint32 s, void *a, void *b) -{ - byte *ba, *bb; - uint32 i; - - ba = a; - bb = b; - for(i=0; i<s; i++) - if(ba[i] != bb[i]) - return 0; - return 1; -} - -static void -memprint(uint32 s, void *a) -{ - uint64 v; - - v = 0xbadb00b; - switch(s) { - case 1: - v = *(uint8*)a; - break; - case 2: - v = *(uint16*)a; - break; - case 4: - v = *(uint32*)a; - break; - case 8: - v = *(uint64*)a; - break; - } - sys·printint(v); -} - -static void -memcopy(uint32 s, void *a, void *b) -{ - byte *ba, *bb; - uint32 i; - - ba = a; - bb = b; - if(bb == nil) { - for(i=0; i<s; i++) - ba[i] = 0; - return; - } - for(i=0; i<s; i++) - ba[i] = bb[i]; -} - -static uint64 -stringhash(uint32 s, string *a) -{ - prints("stringhash\n"); - return 0x12345; -} - -static uint32 -stringequal(uint32 s, string *a, string *b) -{ - return cmpstring(*a, *b) == 0; -} - -static void -stringprint(uint32 s, string *a) -{ - sys·printstring(*a); -} - -static void -stringcopy(uint32 s, string *a, string *b) -{ - if(b == nil) { - *a = nil; - return; - } - *a = *b; -} - -static uint64 -pointerhash(uint32 s, void **a) -{ - prints("pointerhash\n"); - return 0x12345; -} - -static uint32 -pointerequal(uint32 s, void **a, void **b) -{ - prints("pointerequal\n"); - return 0; -} - -static void -pointerprint(uint32 s, void **a) -{ - prints("pointerprint\n"); -} - -static void -pointercopy(uint32 s, void **a, void **b) -{ - if(b == nil) { - *a = nil; - return; - } - *a = *b; -} - -static uint32 -rnd(uint32 n, uint32 m) -{ - uint32 r; - - r = n % m; - if(r) - n += m-r; - return n; -} - -static Alg -algarray[] = -{ - { &memhash, &memequal, &memprint, &memcopy }, - { &stringhash, &stringequal, &stringprint, &stringcopy }, - { &pointerhash, &pointerequal, &pointerprint, &pointercopy }, -}; - // newmap(keysize uint32, valsize uint32, // keyalg uint32, valalg uint32, // hint uint32) (hmap *map[any]any); diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index f8dfa954cc..bc6c0ccb9d 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -64,6 +64,17 @@ mcpy(byte *t, byte *f, uint32 n) } } +uint32 +rnd(uint32 n, uint32 m) +{ + uint32 r; + + r = n % m; + if(r) + n += m-r; + return n; +} + static byte* brk(uint32 n) { @@ -81,7 +92,7 @@ mal(uint32 n) byte* v; // round to keep everything 64-bit aligned - n = (n+7) & ~7; + n = rnd(n, 8); nmal += n; // do we have enough in contiguous hunk @@ -469,7 +480,6 @@ static uint8** argv; static int32 envc; static uint8** envv; - void args(int32 c, uint8 **v) { @@ -797,3 +807,136 @@ sys·morestack(uint64 u) *(int32*)234 = 123; } + +/* + * map and chan helpers for + * dealing with unknown types + */ + +static uint64 +memhash(uint32 s, void *a) +{ + prints("memhash\n"); + return 0x12345; +} + +static uint32 +memequal(uint32 s, void *a, void *b) +{ + byte *ba, *bb; + uint32 i; + + ba = a; + bb = b; + for(i=0; i<s; i++) + if(ba[i] != bb[i]) + return 0; + return 1; +} + +static void +memprint(uint32 s, void *a) +{ + uint64 v; + + v = 0xbadb00b; + switch(s) { + case 1: + v = *(uint8*)a; + break; + case 2: + v = *(uint16*)a; + break; + case 4: + v = *(uint32*)a; + break; + case 8: + v = *(uint64*)a; + break; + } + sys·printint(v); +} + +static void +memcopy(uint32 s, void *a, void *b) +{ + byte *ba, *bb; + uint32 i; + + ba = a; + bb = b; + if(bb == nil) { + for(i=0; i<s; i++) + ba[i] = 0; + return; + } + for(i=0; i<s; i++) + ba[i] = bb[i]; +} + +static uint64 +stringhash(uint32 s, string *a) +{ + prints("stringhash\n"); + return 0x12345; +} + +static uint32 +stringequal(uint32 s, string *a, string *b) +{ + return cmpstring(*a, *b) == 0; +} + +static void +stringprint(uint32 s, string *a) +{ + sys·printstring(*a); +} + +static void +stringcopy(uint32 s, string *a, string *b) +{ + if(b == nil) { + *a = nil; + return; + } + *a = *b; +} + +static uint64 +pointerhash(uint32 s, void **a) +{ + prints("pointerhash\n"); + return 0x12345; +} + +static uint32 +pointerequal(uint32 s, void **a, void **b) +{ + prints("pointerequal\n"); + return 0; +} + +static void +pointerprint(uint32 s, void **a) +{ + prints("pointerprint\n"); +} + +static void +pointercopy(uint32 s, void **a, void **b) +{ + if(b == nil) { + *a = nil; + return; + } + *a = *b; +} + +Alg +algarray[3] = +{ + { &memhash, &memequal, &memprint, &memcopy }, + { &stringhash, &stringequal, &stringprint, &stringcopy }, + { &pointerhash, &pointerequal, &pointerprint, &pointercopy }, +}; diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index 3a2e3bde9e..4273f16eac 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -33,24 +33,58 @@ typedef double float64; */ typedef uint8 bool; typedef uint8 byte; -typedef struct +typedef struct String *string; +typedef struct Sigs Sigs; +typedef struct Sigi Sigi; +typedef struct Map Map; +typedef struct Gobuf Gobuf; +typedef struct G G; +typedef struct M M; +typedef struct Stktop Stktop; +typedef struct Alg Alg; + +/* + * per cpu declaration + */ +extern register G* g; // R15 +extern register M* m; // R14 + +/* + * defined constants + */ +enum +{ + // G status + Gidle, + Grunnable, + Gdead, +}; +enum +{ + true = 1, + false = 0, +}; + +/* + * structures + */ +struct String { int32 len; byte str[1]; -} *string; -typedef struct +}; +struct Sigs { byte* name; uint32 hash; void (*fun)(void); -} Sigs; -typedef struct +}; +struct Sigi { byte* name; uint32 hash; uint32 offset; -} Sigi; -typedef struct Map Map; +}; struct Map { Sigi* si; @@ -60,13 +94,11 @@ struct Map int32 unused; void (*fun[])(void); }; -typedef struct Gobuf Gobuf; struct Gobuf { byte* SP; byte* PC; }; -typedef struct G G; struct G { byte* stackguard; // must not move @@ -77,7 +109,6 @@ struct G int32 pri; int32 goid; }; -typedef struct M M; struct M { G* g0; // g0 w interrupt stack - must not move @@ -90,38 +121,24 @@ struct M int32 siz1; int32 siz2; }; -typedef struct Stktop Stktop; -struct Stktop { +struct Stktop +{ uint8* oldbase; uint8* oldsp; uint64 magic; uint8* oldguard; }; -extern register G* g; // R15 -extern register M* m; // R14 - -enum +struct Alg { - // G status - Gidle, - Grunnable, - Gdead, + uint64 (*hash)(uint32, void*); + uint32 (*equal)(uint32, void*, void*); + void (*print)(uint32, void*); + void (*copy)(uint32, void*, void*); }; - -/* - * global variables - */ -M* allm; -G* allg; -int32 goidgen; - -/* - * defined constants - */ -enum +struct SigTab { - true = 1, - false = 0, + int32 catch; + int8 *name; }; /* @@ -133,6 +150,15 @@ enum #define nil ((void*)0) /* + * external data + */ +extern Alg algarray[3]; +extern string emptystring; +M* allm; +G* allg; +int32 goidgen; + +/* * common functions and data */ int32 strcmp(byte*, byte*); @@ -141,9 +167,6 @@ void dump(byte*, int32); int32 runetochar(byte*, int32); int32 chartorune(uint32*, byte*); -extern string emptystring; -extern int32 debug; - /* * very low level c-called */ @@ -155,6 +178,7 @@ void setspgoto(byte*, void(*)(void), void(*)(void)); void FLUSH(void*); void* getu(void); void throw(int8*); +uint32 rnd(uint32, uint32); void prints(int8*); void mcpy(byte*, byte*, uint32); void* mal(uint32); @@ -165,11 +189,6 @@ int32 open(byte*, int32); int32 read(int32, void*, int32); void close(int32); int32 fstat(int32, void*); -struct SigTab -{ - int32 catch; - int8 *name; -}; /* * low level go -called |
