Make struct impls private (opaque pointers)

This gives us flexibility to change sizeof(wuffs_foo__bar__struct)
across Wuffs versions, while retaining ABI / API compatibility.

In particular, this lets us add and remove fields.
This commit is contained in:
Nigel Tao committed 2018-10-06 10:33:47 +10:00
1 parent 26a448450e
commit 8b1ffec576
4 files changed
+86 -8

No files matched your search

+12 -5
View File
@@ -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')
+1
View File
@@ -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.
+10 -3
View File
@@ -30,6 +30,7 @@ rm -f a.out
for a C compiler $CC, such as clang or gcc.
*/
#include <stdlib.h>
#include <unistd.h>
#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;
}
+63
View File
@@ -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