diff --git a/cmd/wuffs-c/internal/cgen/cgen.go b/cmd/wuffs-c/internal/cgen/cgen.go index 30ec6b5d..08c1ab43 100644 --- a/cmd/wuffs-c/internal/cgen/cgen.go +++ b/cmd/wuffs-c/internal/cgen/cgen.go @@ -336,7 +336,7 @@ func (g *gen) generate() ([]byte, error) { if err := g.genHeader(b); err != nil { return nil, err } - b.writex(wiStart) + b.writex(wiStartImpl) if err := g.genImpl(b); err != nil { return nil, err } @@ -394,11 +394,17 @@ func (g *gen) genHeader(b *buffer) error { } b.writes("// ---------------- Struct Definitions\n\n") + b.writes("// These structs' fields, and the sizeof them, are private implementation\n") + b.writes("// details that aren't guaranteed to be stable across Wuffs versions.\n") + b.writes("//\n") + b.writes("// See https://en.wikipedia.org/wiki/Opaque_pointer#C\n") + b.writex(wiStart) for _, n := range g.structList { if err := g.writeStruct(b, n); err != nil { return err } } + b.writex(wiEnd) b.writes("\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n") return nil @@ -735,8 +741,9 @@ func (g *gen) writeCppMethods(b *buffer, n *a.Struct) error { } var ( - wiStart = []byte("\n// WUFFS C HEADER ENDS HERE.\n#ifdef WUFFS_IMPLEMENTATION\n\n") - wiEnd = []byte("\n#endif // WUFFS_IMPLEMENTATION\n\n") + wiStartImpl = []byte("\n// WUFFS C HEADER ENDS HERE.\n#ifdef WUFFS_IMPLEMENTATION\n\n") + wiStart = []byte("\n#ifdef WUFFS_IMPLEMENTATION\n\n") + wiEnd = []byte("\n#endif // WUFFS_IMPLEMENTATION\n\n") wigbpStart = []byte("\n#ifndef WUFFS_INCLUDE_GUARD__BASE_PUBLIC\n") wigbpEnd = []byte("\n#endif // WUFFS_INCLUDE_GUARD__BASE_PUBLIC\n") @@ -770,8 +777,8 @@ func (g *gen) writeUse(b *buffer, n *a.Use) error { return err } trimmed := []byte(nil) - if i := bytes.Index(usee, wiStart); i >= 0 { - remaining := usee[i+len(wiStart):] + if i := bytes.Index(usee, wiStartImpl); i >= 0 { + remaining := usee[i+len(wiStartImpl):] if j := bytes.Index(remaining, wiEnd); j >= 0 { trimmed = append(trimmed, usee[:i]...) trimmed = append(trimmed, '\n') diff --git a/doc/changelog.md b/doc/changelog.md index a1a85cd4..acb0fcee 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -47,6 +47,7 @@ - Added `WUFFS_CONFIG__STATIC_FUNCTIONS`. - Added some C++ convenience methods. - Added some Go and Rust benchmarks. +- Made struct implementations private (opaque pointers). - Sped up the `mimic_deflate_xxx` benchmarks. diff --git a/example/library/library.c b/example/library/library.c index 6259c7a2..723f86ff 100644 --- a/example/library/library.c +++ b/example/library/library.c @@ -30,6 +30,7 @@ rm -f a.out for a C compiler $CC, such as clang or gcc. */ +#include #include #include "wuffs/release/c/wuffs-unsupported-snapshot.h" @@ -69,17 +70,23 @@ static const char* decode() { wuffs_base__io_writer dst_writer = wuffs_base__io_buffer__writer(&dst); wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src); - wuffs_deflate__decoder dec = ((wuffs_deflate__decoder){}); + wuffs_deflate__decoder* dec = calloc(sizeof__wuffs_deflate__decoder(), 1); + if (!dec) { + return "out of memory"; + } wuffs_base__status z = wuffs_deflate__decoder__check_wuffs_version( - &dec, sizeof dec, WUFFS_VERSION); + dec, sizeof__wuffs_deflate__decoder(), WUFFS_VERSION); if (z) { + free(dec); return z; } - z = wuffs_deflate__decoder__decode(&dec, dst_writer, src_reader); + z = wuffs_deflate__decoder__decode(dec, dst_writer, src_reader); if (z) { + free(dec); return z; } ignore_return_value(write(1, dst.data.ptr, dst.meta.wi)); + free(dec); return NULL; } diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index a7ec606b..9bdd2487 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -2226,6 +2226,13 @@ wuffs_adler32__hasher__update(wuffs_adler32__hasher* self, // ---------------- Struct Definitions +// These structs' fields, and the sizeof them, are private implementation +// details that aren't guaranteed to be stable across Wuffs versions. +// +// See https://en.wikipedia.org/wiki/Opaque_pointer#C + +#ifdef WUFFS_IMPLEMENTATION + struct wuffs_adler32__hasher__struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. Instead, use the @@ -2259,6 +2266,8 @@ struct wuffs_adler32__hasher__struct { }; // struct wuffs_adler32__hasher__struct +#endif // WUFFS_IMPLEMENTATION + #ifdef __cplusplus } // extern "C" #endif @@ -2302,6 +2311,13 @@ wuffs_crc32__ieee_hasher__update(wuffs_crc32__ieee_hasher* self, // ---------------- Struct Definitions +// These structs' fields, and the sizeof them, are private implementation +// details that aren't guaranteed to be stable across Wuffs versions. +// +// See https://en.wikipedia.org/wiki/Opaque_pointer#C + +#ifdef WUFFS_IMPLEMENTATION + struct wuffs_crc32__ieee_hasher__struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. Instead, use the @@ -2334,6 +2350,8 @@ struct wuffs_crc32__ieee_hasher__struct { }; // struct wuffs_crc32__ieee_hasher__struct +#endif // WUFFS_IMPLEMENTATION + #ifdef __cplusplus } // extern "C" #endif @@ -2392,6 +2410,13 @@ wuffs_deflate__decoder__decode(wuffs_deflate__decoder* self, // ---------------- Struct Definitions +// These structs' fields, and the sizeof them, are private implementation +// details that aren't guaranteed to be stable across Wuffs versions. +// +// See https://en.wikipedia.org/wiki/Opaque_pointer#C + +#ifdef WUFFS_IMPLEMENTATION + struct wuffs_deflate__decoder__struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. Instead, use the @@ -2483,6 +2508,8 @@ struct wuffs_deflate__decoder__struct { }; // struct wuffs_deflate__decoder__struct +#endif // WUFFS_IMPLEMENTATION + #ifdef __cplusplus } // extern "C" #endif @@ -2539,6 +2566,13 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, // ---------------- Struct Definitions +// These structs' fields, and the sizeof them, are private implementation +// details that aren't guaranteed to be stable across Wuffs versions. +// +// See https://en.wikipedia.org/wiki/Opaque_pointer#C + +#ifdef WUFFS_IMPLEMENTATION + struct wuffs_lzw__decoder__struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. Instead, use the @@ -2594,6 +2628,8 @@ struct wuffs_lzw__decoder__struct { }; // struct wuffs_lzw__decoder__struct +#endif // WUFFS_IMPLEMENTATION + #ifdef __cplusplus } // extern "C" #endif @@ -2670,6 +2706,13 @@ wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self, // ---------------- Struct Definitions +// These structs' fields, and the sizeof them, are private implementation +// details that aren't guaranteed to be stable across Wuffs versions. +// +// See https://en.wikipedia.org/wiki/Opaque_pointer#C + +#ifdef WUFFS_IMPLEMENTATION + struct wuffs_gif__decoder__struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. Instead, use the @@ -2840,6 +2883,8 @@ struct wuffs_gif__decoder__struct { }; // struct wuffs_gif__decoder__struct +#endif // WUFFS_IMPLEMENTATION + #ifdef __cplusplus } // extern "C" #endif @@ -2900,6 +2945,13 @@ wuffs_gzip__decoder__decode(wuffs_gzip__decoder* self, // ---------------- Struct Definitions +// These structs' fields, and the sizeof them, are private implementation +// details that aren't guaranteed to be stable across Wuffs versions. +// +// See https://en.wikipedia.org/wiki/Opaque_pointer#C + +#ifdef WUFFS_IMPLEMENTATION + struct wuffs_gzip__decoder__struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. Instead, use the @@ -2951,6 +3003,8 @@ struct wuffs_gzip__decoder__struct { }; // struct wuffs_gzip__decoder__struct +#endif // WUFFS_IMPLEMENTATION + #ifdef __cplusplus } // extern "C" #endif @@ -3011,6 +3065,13 @@ wuffs_zlib__decoder__decode(wuffs_zlib__decoder* self, // ---------------- Struct Definitions +// These structs' fields, and the sizeof them, are private implementation +// details that aren't guaranteed to be stable across Wuffs versions. +// +// See https://en.wikipedia.org/wiki/Opaque_pointer#C + +#ifdef WUFFS_IMPLEMENTATION + struct wuffs_zlib__decoder__struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. Instead, use the @@ -3058,6 +3119,8 @@ struct wuffs_zlib__decoder__struct { }; // struct wuffs_zlib__decoder__struct +#endif // WUFFS_IMPLEMENTATION + #ifdef __cplusplus } // extern "C" #endif