Commit de7920e6 authored by Russ Cox's avatar Russ Cox

finish ChanValue: Len and Cap.

R=r
DELTA=45  (45 added, 0 deleted, 0 changed)
OCL=33873
CL=33881
parent 653cef1b
......@@ -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
......
......@@ -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);
......
......@@ -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
......
......@@ -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
......
......@@ -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*);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment