Skip to content

Commit

Permalink
Update to gobook@27cf7b490526a0a0751ff12ed7cc4acd8afa2edc
Browse files Browse the repository at this point in the history
  • Loading branch information
adonovan committed Dec 6, 2015
1 parent 34cd171 commit b7e1189
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
5 changes: 3 additions & 2 deletions ch1/dup3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion ch1/fetch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
15 changes: 9 additions & 6 deletions ch13/bzip/bzip2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, */
Expand All @@ -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;
}

Expand Down
39 changes: 20 additions & 19 deletions ch13/bzip/bzip2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

//!+

Expand All @@ -26,11 +30,10 @@ package bzip
#cgo LDFLAGS: -L/usr/lib -lbz2
#include <bzlib.h>
#include <stdlib.h>
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"

Expand All @@ -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
Expand Down

0 comments on commit b7e1189

Please sign in to comment.