From b7e11892d02b9277b139bacb1ecde6ede3014788 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Sun, 6 Dec 2015 11:23:30 -0500 Subject: [PATCH] Update to gobook@27cf7b490526a0a0751ff12ed7cc4acd8afa2edc --- ch1/dup3/main.go | 5 +++-- ch1/fetch/main.go | 2 +- ch13/bzip/bzip2.c | 15 +++++++++------ ch13/bzip/bzip2.go | 39 ++++++++++++++++++++------------------- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/ch1/dup3/main.go b/ch1/dup3/main.go index d5a0ad9..50434d8 100644 --- a/ch1/dup3/main.go +++ b/ch1/dup3/main.go @@ -3,9 +3,10 @@ // See page 12. -// Dup3 prints the count and text of lines that appear more than once -// in the named input files. //!+ + +// Dup3 prints the count and text of lines that +// appear more than once in the named input files. package main import ( diff --git a/ch1/fetch/main.go b/ch1/fetch/main.go index 5bde965..e704ae6 100644 --- a/ch1/fetch/main.go +++ b/ch1/fetch/main.go @@ -4,7 +4,7 @@ // See page 16. //!+ -// Fetch prints the content found at a URL. +// Fetch prints the content found at each specified URL. package main import ( diff --git a/ch13/bzip/bzip2.c b/ch13/bzip/bzip2.c index 3ad459b..985869e 100644 --- a/ch13/bzip/bzip2.c +++ b/ch13/bzip/bzip2.c @@ -2,6 +2,14 @@ // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ // See page 362. +// +// The version of this program that appeared in the first and second +// printings did not comply with the proposed rules for passing +// pointers between Go and C, described here: +// https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md +// +// The version below, which appears in the third printing, +// has been corrected. See bzip2.go for explanation. //!+ /* This file is gopl.io/ch13/bzip/bzip2.c, */ @@ -17,12 +25,7 @@ int bz2compress(bz_stream *s, int action, int r = BZ2_bzCompress(s, action); *inlen -= s->avail_in; *outlen -= s->avail_out; - - /* "C code may store a Go pointer in C memory subject to rule 2: - * it must stop storing the pointer before it returns to Go." */ - s->next_in = NULL; - s->next_out = NULL; - + s->next_in = s->next_out = NULL; return r; } diff --git a/ch13/bzip/bzip2.go b/ch13/bzip/bzip2.go index 2409b84..278b238 100644 --- a/ch13/bzip/bzip2.go +++ b/ch13/bzip/bzip2.go @@ -2,19 +2,23 @@ // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ // See page 362. -// The version of this file that appears in the book does not comply -// with the proposed rules for passing pointers between Go and C. -// (https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md) -// The rules forbid a C function like bz2compress from storing 'in' and -// 'out' (pointers to variables allocated by Go) into the Go variable 's', -// even temporarily. // -// To comply with the rules, the bz_stream variable must be allocated -// by C code. We have introduced two C functions, bz2alloc and -// bz2free, to allocate and free instances of the bz_stream type. -// Also, we have changed bz2compress so that before it returns, it -// clears the fields of the bz_stream that contain point to Go -// variables. +// The version of this program that appeared in the first and second +// printings did not comply with the proposed rules for passing +// pointers between Go and C, described here: +// https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md +// +// The rules forbid a C function like bz2compress from storing 'in' +// and 'out' (pointers to variables allocated by Go) into the Go +// variable 's', even temporarily. +// +// The version below, which appears in the third printing, has been +// corrected. To comply with the rules, the bz_stream variable must +// be allocated by C code. We have introduced two C functions, +// bz2alloc and bz2free, to allocate and free instances of the +// bz_stream type. Also, we have changed bz2compress so that before +// it returns, it clears the fields of the bz_stream that contain +// pointers to Go variables. //!+ @@ -26,11 +30,10 @@ package bzip #cgo LDFLAGS: -L/usr/lib -lbz2 #include #include - bz_stream* bz2alloc() { return calloc(1, sizeof(bz_stream)); } int bz2compress(bz_stream *s, int action, char *in, unsigned *inlen, char *out, unsigned *outlen); -void bz2free(bz_stream* s) { return free(s); } +void bz2free(bz_stream* s) { free(s); } */ import "C" @@ -47,11 +50,9 @@ type writer struct { // NewWriter returns a writer for bzip2-compressed streams. func NewWriter(out io.Writer) io.WriteCloser { - const ( - blockSize = 9 - verbosity = 0 - workFactor = 30 - ) + const blockSize = 9 + const verbosity = 0 + const workFactor = 30 w := &writer{w: out, stream: C.bz2alloc()} C.BZ2_bzCompressInit(w.stream, blockSize, verbosity, workFactor) return w