len and cap on chans

R=ken
OCL=33599
CL=33599
This commit is contained in:
Russ Cox 2009-08-20 11:12:04 -07:00
parent 7c4f7cc7eb
commit 8a45917f3d
6 changed files with 78 additions and 12 deletions

View file

@ -4345,10 +4345,6 @@ The following minimal alignment properties are guaranteed:
<p>
<font color=red>
Implementation does not honor the restriction on goto statements and targets (no intervening declarations).
<br/>
cap() does not work on chans.
<br/>
len() does not work on chans.
</font>
</p>

View file

@ -242,8 +242,8 @@ cgen(Node *n, Node *res)
break;
case OLEN:
if(istype(nl->type, TMAP)) {
// map has len in the first 32-bit word.
if(istype(nl->type, TMAP) || istype(nl->type, TCHAN)) {
// map and chan have len in the first 32-bit word.
// a zero pointer means zero length
regalloc(&n1, types[tptr], res);
cgen(nl, &n1);
@ -279,6 +279,28 @@ cgen(Node *n, Node *res)
break;
case OCAP:
if(istype(nl->type, TCHAN)) {
// chan has cap in the second 32-bit word.
// a zero pointer means zero length
regalloc(&n1, types[tptr], res);
cgen(nl, &n1);
nodconst(&n2, types[tptr], 0);
gins(optoas(OCMP, types[tptr]), &n1, &n2);
p1 = gbranch(optoas(OEQ, types[tptr]), T);
n2 = n1;
n2.op = OINDREG;
n2.xoffset = 4;
n2.type = types[TINT32];
gmove(&n2, &n1);
patch(p1, pc);
gmove(&n1, res);
regfree(&n1);
break;
}
if(isslice(nl->type)) {
regalloc(&n1, types[tptr], res);
agen(nl, &n1);

View file

@ -237,7 +237,7 @@ cgen(Node *n, Node *res)
break;
case OLEN:
if(istype(nl->type, TMAP)) {
if(istype(nl->type, TMAP) || istype(nl->type, TCHAN)) {
// map has len in the first 32-bit word.
// a zero pointer means zero length
tempalloc(&n1, types[tptr]);
@ -280,6 +280,28 @@ cgen(Node *n, Node *res)
break;
case OCAP:
if(istype(nl->type, TCHAN)) {
// chan has cap in the second 32-bit word.
// a zero pointer means zero length
regalloc(&n1, types[tptr], res);
cgen(nl, &n1);
nodconst(&n2, types[tptr], 0);
gins(optoas(OCMP, types[tptr]), &n1, &n2);
p1 = gbranch(optoas(OEQ, types[tptr]), T);
n2 = n1;
n2.op = OINDREG;
n2.xoffset = 4;
n2.type = types[TINT32];
gmove(&n2, &n1);
patch(p1, pc);
gmove(&n1, res);
regfree(&n1);
break;
}
if(isslice(nl->type)) {
igen(nl, &n1, res);
n1.op = OINDREG;

View file

@ -303,11 +303,10 @@ typeinit(void)
okforbool[TBOOL] = 1;
okforcap[TARRAY] = 1;
//okforcap[TCHAN] = 1;
//okforcap[TMAP] = 1;
okforcap[TCHAN] = 1;
okforlen[TARRAY] = 1;
//okforlen[TCHAN] = 1;
okforlen[TCHAN] = 1;
okforlen[TMAP] = 1;
okforlen[TSTRING] = 1;

View file

@ -39,10 +39,10 @@ struct WaitQ
struct Hchan
{
uint32 qcount; // total data in the q
uint32 dataqsiz; // size of the circular q
uint16 elemsize;
uint16 closed; // Wclosed Rclosed errorcount
uint32 dataqsiz; // size of the circular q
uint32 qcount; // total data in the q
Alg* elemalg; // interface for element type
Link* senddataq; // pointer for sender
Link* recvdataq; // pointer for receiver

27
test/chancap.go Normal file
View file

@ -0,0 +1,27 @@
// $G $D/$F.go && $L $F.$A && ./$A.out
// 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.
package main
func main() {
c := make(chan int, 10);
if len(c) != 0 || cap(c) != 10 {
panicln("chan len/cap ", len(c), cap(c), " want 0 10");
}
for i := 0; i < 3; i++ {
c <- i;
}
if len(c) != 3 || cap(c) != 10 {
panicln("chan len/cap ", len(c), cap(c), " want 3 10");
}
c = make(chan int);
if len(c) != 0 || cap(c) != 0 {
panicln("chan len/cap ", len(c), cap(c), " want 0 0");
}
}