Add PUFFS_MAGIC.

This commit is contained in:
Nigel Tao committed 2017-05-27 10:18:34 +10:00
1 parent a9bb43229a
commit 56bc1d7945
4 files changed
+38 -14

No files matched your search

+9
View File
@@ -68,6 +68,12 @@ func (g *Generator) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byt
}
b.WriteString("// ---------------- Constructor and Destructor Implementations\n\n")
b.WriteString("// PUFFS_MAGIC is a magic number to check that constructors are called. It's\n")
b.WriteString("// not foolproof, given C doesn't automatically zero memory before use, but it\n")
b.WriteString("// should catch 99.99% of cases.\n")
b.WriteString("//\n")
b.WriteString("// Its (non-zero) value is arbitrary, based on md5sum(\"puffs\").\n")
b.WriteString("#define PUFFS_MAGIC (0xCB3699CCU)\n\n")
for _, f := range files {
for _, n := range f.TopLevelDecls() {
if n.Kind() == a.KStruct {
@@ -86,6 +92,7 @@ func writeStruct(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct) error
fmt.Fprintf(b, "typedef struct {\n")
if n.Suspendible() {
fmt.Fprintf(b, "puffs_%s_status status;\n", pkgName)
fmt.Fprintf(b, "uint32_t magic;\n")
}
for _, f := range n.Fields() {
if err := writeField(b, m, f.Field()); err != nil {
@@ -131,6 +138,8 @@ func writeCtorImpls(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct) er
fmt.Fprintf(b, "}\n")
b.WriteString("memset(self, 0, sizeof(*self));\n")
b.WriteString("self->magic = PUFFS_MAGIC;\n")
// TODO: set any non-zero default values.
}
+9
View File
@@ -46,6 +46,7 @@ typedef enum {
typedef struct {
puffs_gif_status status;
uint32_t magic;
uint32_t f_literal_width;
uint8_t f_stack[4096];
uint8_t f_suffixes[4096];
@@ -65,6 +66,13 @@ void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self);
// ---------------- Constructor and Destructor Implementations
// PUFFS_MAGIC is a magic number to check that constructors are called. It's
// not foolproof, given C doesn't automatically zero memory before use, but it
// should catch 99.99% of cases.
//
// Its (non-zero) value is arbitrary, based on md5sum("puffs").
#define PUFFS_MAGIC (0xCB3699CCU)
void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self,
uint32_t puffs_version) {
if (puffs_version != PUFFS_VERSION) {
@@ -72,6 +80,7 @@ void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self,
return;
}
memset(self, 0, sizeof(*self));
self->magic = PUFFS_MAGIC;
}
void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self) {}
+1
View File
@@ -46,6 +46,7 @@ typedef enum {
typedef struct {
puffs_gif_status status;
uint32_t magic;
uint32_t f_literal_width;
uint8_t f_stack[4096];
uint8_t f_suffixes[4096];
+19 -14
View File
@@ -12,29 +12,34 @@ done
Each edition should print "PASS", amongst other information, and exit(0).
*/
#include "../testlib/testlib.c"
#include "../../../gen/c/gif.c"
#include "../testlib/testlib.c"
const char* test_filename = "gif/basic.c";
void test_default_literal_width() {
void test_puffs_version_bad() {
puffs_gif_lzw_decoder dec;
puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION);
if (dec.f_literal_width != 0) {
FAIL("test_default_literal_width: got %d, want %d", dec.f_literal_width, 0);
puffs_gif_lzw_decoder_constructor(&dec, 0); // 0 is not PUFFS_VERSION.
if (dec.status != puffs_gif_error_bad_version) {
FAIL("test_puffs_version_bad: status: got %d, want %d", dec.status,
puffs_gif_error_bad_version);
goto cleanup0;
}
cleanup0:
puffs_gif_lzw_decoder_destructor(&dec);
}
void test_puffs_version_bad() {
void test_puffs_version_good() {
puffs_gif_lzw_decoder dec;
puffs_gif_lzw_decoder_constructor(&dec, 0); // 0 is not PUFFS_VERSION.
if (dec.status != puffs_gif_error_bad_version) {
FAIL("test_puffs_version_bad: got %d, want %d", dec.status,
puffs_gif_error_bad_version);
puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION);
if (dec.magic != PUFFS_MAGIC) {
FAIL("test_puffs_version_good: magic: got %u, want %u", dec.magic,
PUFFS_MAGIC);
goto cleanup0;
}
if (dec.f_literal_width != 0) {
FAIL("test_puffs_version_good: f_literal_width: got %u, want %u",
dec.f_literal_width, 0);
goto cleanup0;
}
cleanup0:
@@ -43,7 +48,7 @@ cleanup0:
// The empty comments forces clang-format to place one element per line.
test tests[] = {
test_default_literal_width, //
test_puffs_version_bad, //
NULL, //
test_puffs_version_bad, //
test_puffs_version_good, //
NULL, //
};