Add a private_impl layer.

This commit is contained in:
Nigel Tao committed 2017-06-10 18:36:05 +10:00
1 parent 2fd08e0cf8
commit 3fbb9ee3cf
4 files changed
+89 -57

No files matched your search

+29 -18
View File
@@ -373,13 +373,21 @@ func (g *gen) gatherStatuses(n *a.Status) error {
}
func (g *gen) writeStruct(n *a.Struct) error {
// For API/ABI compatibility, the very first field in the struct must be
// the status code. This lets the constructor callee set "this->status =
// etc_error_bad_version;" regardless of the sizeof(*this) struct reserved
// by the caller and even if the caller and callee were built with
// different versions.
// For API/ABI compatibility, the very first field in the struct's
// private_impl must be the status code. This lets the constructor callee
// set "self->private_impl.status = etc_error_bad_version;" regardless of
// the sizeof(*self) struct reserved by the caller and even if the caller
// and callee were built with different versions.
structName := n.Name().String(g.tm)
g.printf("typedef struct {\n")
g.writes("typedef struct {\n")
g.writes("// Do not access the private_impl's fields directly. There is no API/ABI\n")
g.writes("// compatibility or safety guarantee if you do so. Instead, use the\n")
g.printf("// puffs_%s_%s_etc functions.\n", g.pkgName, structName)
g.writes("//\n")
g.writes("// In C++, these fields would be \"private\", but C does not support that.\n")
g.writes("//\n")
g.writes("// It is a struct, not a struct*, so that it can be stack allocated.\n")
g.writes("struct {\n")
if n.Suspendible() {
g.printf("puffs_%s_status status;\n", g.pkgName)
g.printf("uint32_t magic;\n")
@@ -390,7 +398,7 @@ func (g *gen) writeStruct(n *a.Struct) error {
}
g.writes(";\n")
}
g.printf("} puffs_%s_%s;\n\n", g.pkgName, structName)
g.printf("} private_impl;\n } puffs_%s_%s;\n\n", g.pkgName, structName)
return nil
}
@@ -445,19 +453,19 @@ func (g *gen) writeCtorImpls(n *a.Struct) error {
if ctor {
g.printf("if (puffs_version != PUFFS_VERSION) {\n")
g.printf("self->status = puffs_%s_error_bad_version;\n", g.pkgName)
g.printf("self->private_impl.status = puffs_%s_error_bad_version;\n", g.pkgName)
g.printf("return;\n")
g.printf("}\n")
g.writes("if (for_internal_use_only != PUFFS_ALREADY_ZEROED) {" +
"memset(self, 0, sizeof(*self)); }\n")
g.writes("self->magic = PUFFS_MAGIC;\n")
g.writes("self->private_impl.magic = PUFFS_MAGIC;\n")
for _, f := range n.Fields() {
f := f.Field()
if dv := f.DefaultValue(); dv != nil {
// TODO: set default values for array types.
g.printf("self->%s%s = %d;\n", fPrefix, f.Name().String(g.tm), dv.ConstValue())
g.printf("self->private_impl.%s%s = %d;\n", fPrefix, f.Name().String(g.tm), dv.ConstValue())
}
}
}
@@ -526,7 +534,7 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
g.perFunc.suspendible = true
g.printf("puffs_%s_status status = ", g.pkgName)
if n.Receiver() != 0 {
g.printf("self->status;\n")
g.printf("self->private_impl.status;\n")
if n.Public() {
g.printf("if (status & 1) { return status; }")
}
@@ -534,7 +542,7 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
g.printf("puffs_%s_status_ok;\n", g.pkgName)
}
if n.Public() {
g.printf("if (self->magic != PUFFS_MAGIC) {"+
g.printf("if (self->private_impl.magic != PUFFS_MAGIC) {"+
"status = puffs_%s_error_constructor_not_called; goto cleanup0; }\n", g.pkgName)
}
} else if r := n.Receiver(); r != 0 {
@@ -586,7 +594,7 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
if g.perFunc.suspendible {
if g.perFunc.public {
g.printf("cleanup0: self->status = status;\n")
g.printf("cleanup0: self->private_impl.status = status;\n")
}
g.printf("return status;\n")
}
@@ -900,7 +908,7 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
// TODO: suspend coroutine state.
//
// TODO: don't assume that the argument is "this.stack[s:]".
g.printf("if ((%sdst->len - %sdst->wi) < (sizeof(self->f_stack) - v_s)) {", aPrefix, aPrefix)
g.printf("if ((%sdst->len - %sdst->wi) < (sizeof(self->private_impl.f_stack) - v_s)) {", aPrefix, aPrefix)
g.printf("status = puffs_%s_status_short_write;", g.pkgName)
if g.perFunc.public && g.perFunc.suspendible {
g.writes("goto cleanup0;")
@@ -908,8 +916,11 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
g.writes("return status;")
}
g.writes("}\n")
g.printf("memmove(a_dst->ptr + a_dst->wi, self->f_stack + v_s, sizeof(self->f_stack) - v_s);\n")
g.printf("a_dst->wi += sizeof(self->f_stack) - v_s;\n")
g.printf("memmove(" +
"a_dst->ptr + a_dst->wi," +
"self->private_impl.f_stack + v_s," +
"sizeof(self->private_impl.f_stack) - v_s);\n")
g.printf("a_dst->wi += sizeof(self->private_impl.f_stack) - v_s;\n")
} else if isInDst(g.tm, n, t.KeyWriteU8) {
// TODO: suspend coroutine state.
@@ -998,7 +1009,7 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
switch n.ID0().Key() {
case 0:
if id1 := n.ID1(); id1.Key() == t.KeyThis {
g.writes("self")
g.writes("self->private_impl")
} else {
// TODO: don't assume that the vPrefix is necessary.
g.writes(vPrefix)
@@ -1046,7 +1057,7 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
// TODO: choose between . vs -> operators.
//
// TODO: don't assume that the fPrefix is necessary.
g.writes("->")
g.writes(".")
g.writes(fPrefix)
g.writes(n.ID1().String(g.tm))
return nil
+37 -27
View File
@@ -74,12 +74,21 @@ const char* puffs_gif_status_string(puffs_gif_status s);
// ---------------- Public Structs
typedef struct {
puffs_gif_status status;
uint32_t magic;
uint32_t f_literal_width;
uint8_t f_stack[4096];
uint8_t f_suffixes[4096];
uint16_t f_prefixes[4096];
// 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
// puffs_gif_lzw_decoder_etc functions.
//
// In C++, these fields would be "private", but C does not support that.
//
// It is a struct, not a struct*, so that it can be stack allocated.
struct {
puffs_gif_status status;
uint32_t magic;
uint32_t f_literal_width;
uint8_t f_stack[4096];
uint8_t f_suffixes[4096];
uint16_t f_prefixes[4096];
} private_impl;
} puffs_gif_lzw_decoder;
// ---------------- Public Constructor and Destructor Prototypes
@@ -179,14 +188,14 @@ void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self,
return;
}
if (puffs_version != PUFFS_VERSION) {
self->status = puffs_gif_error_bad_version;
self->private_impl.status = puffs_gif_error_bad_version;
return;
}
if (for_internal_use_only != PUFFS_ALREADY_ZEROED) {
memset(self, 0, sizeof(*self));
}
self->magic = PUFFS_MAGIC;
self->f_literal_width = 8;
self->private_impl.magic = PUFFS_MAGIC;
self->private_impl.f_literal_width = 8;
}
void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self) {
@@ -204,11 +213,11 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
if (!self) {
return puffs_gif_error_bad_receiver;
}
puffs_gif_status status = self->status;
puffs_gif_status status = self->private_impl.status;
if (status & 1) {
return status;
}
if (self->magic != PUFFS_MAGIC) {
if (self->private_impl.magic != PUFFS_MAGIC) {
status = puffs_gif_error_constructor_not_called;
goto cleanup0;
}
@@ -229,12 +238,12 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
uint32_t v_s;
uint32_t v_c;
v_clear_code = (((uint32_t)(1)) << self->f_literal_width);
v_clear_code = (((uint32_t)(1)) << self->private_impl.f_literal_width);
v_end_code = (v_clear_code + 1);
v_use_save_code = 0;
v_save_code = v_end_code;
v_prev_code = 0;
v_width = (self->f_literal_width + 1);
v_width = (self->private_impl.f_literal_width + 1);
v_bits = 0;
v_n_bits = 0;
label_0_continue:;
@@ -258,14 +267,14 @@ label_0_continue:;
}
a_dst->ptr[a_dst->wi++] = ((uint8_t)(v_code));
if (v_use_save_code) {
self->f_suffixes[v_save_code] = ((uint8_t)(v_code));
self->f_prefixes[v_save_code] = ((uint16_t)(v_prev_code));
self->private_impl.f_suffixes[v_save_code] = ((uint8_t)(v_code));
self->private_impl.f_prefixes[v_save_code] = ((uint16_t)(v_prev_code));
}
} else if (v_code == v_clear_code) {
v_use_save_code = false;
v_save_code = v_end_code;
v_prev_code = 0;
v_width = (self->f_literal_width + 1);
v_width = (self->private_impl.f_literal_width + 1);
goto label_0_continue;
} else if (v_code == v_end_code) {
status = puffs_gif_status_ok;
@@ -278,28 +287,29 @@ label_0_continue:;
v_c = v_prev_code;
}
while (v_c >= v_clear_code) {
self->f_stack[v_s] = self->f_suffixes[v_c];
self->private_impl.f_stack[v_s] = self->private_impl.f_suffixes[v_c];
if (v_s == 0) {
status = puffs_gif_error_lzw_prefix_chain_is_cyclical;
goto cleanup0;
}
v_s -= 1;
v_c = ((uint32_t)(self->f_prefixes[v_c]));
v_c = ((uint32_t)(self->private_impl.f_prefixes[v_c]));
}
self->f_stack[v_s] = ((uint8_t)(v_c));
self->private_impl.f_stack[v_s] = ((uint8_t)(v_c));
if ((v_code == v_save_code) && v_use_save_code) {
self->f_stack[4095] = ((uint8_t)(v_c));
self->private_impl.f_stack[4095] = ((uint8_t)(v_c));
}
if ((a_dst->len - a_dst->wi) < (sizeof(self->f_stack) - v_s)) {
if ((a_dst->len - a_dst->wi) <
(sizeof(self->private_impl.f_stack) - v_s)) {
status = puffs_gif_status_short_write;
goto cleanup0;
}
memmove(a_dst->ptr + a_dst->wi, self->f_stack + v_s,
sizeof(self->f_stack) - v_s);
a_dst->wi += sizeof(self->f_stack) - v_s;
memmove(a_dst->ptr + a_dst->wi, self->private_impl.f_stack + v_s,
sizeof(self->private_impl.f_stack) - v_s);
a_dst->wi += sizeof(self->private_impl.f_stack) - v_s;
if (v_use_save_code) {
self->f_suffixes[v_save_code] = ((uint8_t)(v_c));
self->f_prefixes[v_save_code] = ((uint16_t)(v_prev_code));
self->private_impl.f_suffixes[v_save_code] = ((uint8_t)(v_c));
self->private_impl.f_prefixes[v_save_code] = ((uint16_t)(v_prev_code));
}
} else {
status = puffs_gif_error_lzw_code_is_out_of_range;
@@ -316,6 +326,6 @@ label_0_continue:;
}
cleanup0:
self->status = status;
self->private_impl.status = status;
return status;
}
+15 -6
View File
@@ -74,12 +74,21 @@ const char* puffs_gif_status_string(puffs_gif_status s);
// ---------------- Public Structs
typedef struct {
puffs_gif_status status;
uint32_t magic;
uint32_t f_literal_width;
uint8_t f_stack[4096];
uint8_t f_suffixes[4096];
uint16_t f_prefixes[4096];
// 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
// puffs_gif_lzw_decoder_etc functions.
//
// In C++, these fields would be "private", but C does not support that.
//
// It is a struct, not a struct*, so that it can be stack allocated.
struct {
puffs_gif_status status;
uint32_t magic;
uint32_t f_literal_width;
uint8_t f_stack[4096];
uint8_t f_suffixes[4096];
uint16_t f_prefixes[4096];
} private_impl;
} puffs_gif_lzw_decoder;
// ---------------- Public Constructor and Destructor Prototypes
+8 -6
View File
@@ -56,8 +56,9 @@ void test_puffs_version_bad() {
test_funcname = __func__;
puffs_gif_lzw_decoder dec;
puffs_gif_lzw_decoder_constructor(&dec, 0, 0); // 0 is not PUFFS_VERSION.
if (dec.status != puffs_gif_error_bad_version) {
FAIL("status: got %d, want %d", dec.status, puffs_gif_error_bad_version);
if (dec.private_impl.status != puffs_gif_error_bad_version) {
FAIL("status: got %d, want %d", dec.private_impl.status,
puffs_gif_error_bad_version);
goto cleanup0;
}
cleanup0:
@@ -68,12 +69,13 @@ void test_puffs_version_good() {
test_funcname = __func__;
puffs_gif_lzw_decoder dec;
puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0);
if (dec.magic != PUFFS_MAGIC) {
FAIL("magic: got %u, want %u", dec.magic, PUFFS_MAGIC);
if (dec.private_impl.magic != PUFFS_MAGIC) {
FAIL("magic: got %u, want %u", dec.private_impl.magic, PUFFS_MAGIC);
goto cleanup0;
}
if (dec.f_literal_width != 8) {
FAIL("f_literal_width: got %u, want %u", dec.f_literal_width, 8);
if (dec.private_impl.f_literal_width != 8) {
FAIL("f_literal_width: got %u, want %u", dec.private_impl.f_literal_width,
8);
goto cleanup0;
}
cleanup0: