1
0
mirror of https://github.com/golang/go synced 2024-11-20 00:04:43 -07:00

finish ChanValue: Len and Cap.

R=r
DELTA=45  (45 added, 0 deleted, 0 changed)
OCL=33873
CL=33881
This commit is contained in:
Russ Cox 2009-08-26 12:42:22 -07:00
parent 653cef1ba0
commit de7920e6fd
5 changed files with 45 additions and 0 deletions

View File

@ -757,6 +757,17 @@ func TestChan(t *testing.T) {
if cv.TryRecv() != nil {
t.Errorf("TryRecv on sync chan succeeded");
}
// len/cap
cv = MakeChan(Typeof(c).(*ChanType), 10);
c = cv.Interface().(chan int);
for i := 0; i < 3; i++ {
c <- i;
}
if l, m := cv.Len(), cv.Cap(); l != len(c) || m != cap(c) {
t.Errorf("Len/Cap = %d/%d want %d/%d", l, m, len(c), cap(c));
}
}
// Difficult test for function call because of

View File

@ -636,6 +636,8 @@ func chansend(ch, val *byte, pres *bool)
func chanrecv(ch, val *byte, pres *bool)
func chanclosed(ch *byte) bool
func chanclose(ch *byte)
func chanlen(ch *byte) int32
func chancap(ch *byte) int32
// Closed returns the result of closed(c) on the underlying channel.
func (v *ChanValue) Closed() bool {
@ -649,6 +651,16 @@ func (v *ChanValue) Close() {
chanclose(ch);
}
func (v *ChanValue) Len() int {
ch := *(**byte)(v.addr);
return int(chanlen(ch));
}
func (v *ChanValue) Cap() int {
ch := *(**byte)(v.addr);
return int(chancap(ch));
}
// internal send; non-blocking if b != nil
func (v *ChanValue) send(x Value, b *bool) {
t := v.Type().(*ChanType);

View File

@ -917,6 +917,18 @@ chanclosed(Hchan *c)
return (c->closed & Rclosed) != 0;
}
int32
chanlen(Hchan *c)
{
return c->qcount;
}
int32
chancap(Hchan *c)
{
return c->dataqsiz;
}
// closedchan(sel *byte) bool;
void

View File

@ -86,6 +86,14 @@ func chanclosed(ch *byte) (r bool) {
r = chanclosed((Hchan*)ch);
}
func chanlen(ch *byte) (r int32) {
r = chanlen((Hchan*)ch);
}
func chancap(ch *byte) (r int32) {
r = chancap((Hchan*)ch);
}
/*
* Go wrappers around the functions in iface.c

View File

@ -493,5 +493,7 @@ void chansend(Hchan*, void*, bool*);
void chanrecv(Hchan*, void*, bool*);
void chanclose(Hchan*);
bool chanclosed(Hchan*);
int32 chanlen(Hchan*);
int32 chancap(Hchan*);
void ifaceE2I(struct InterfaceType*, Eface, Iface*);