Tweak puffs_base_limit1 field and type names.

This commit is contained in:
Nigel Tao committed 2017-06-23 10:14:44 +10:00
1 parent 0088c7e257
commit 09c59dfade
5 files changed
+81 -61

No files matched your search

+12 -6
View File
@@ -46,19 +46,25 @@ typedef struct {
bool closed; // No further writes are expected.
} puffs_base_buf1;
typedef struct puffs_base_limit {
uint64_t* len;
struct puffs_base_limit* next;
} puffs_base_limit;
// puffs_base_limit1 provides a limited view of a 1-dimensional byte stream:
// its first N bytes. That N can be greater than a buffer's current read or
// write capacity. N decreases naturally over time as bytes are read from or
// written to the stream.
//
// A value with all fields NULL or zero is a valid, unlimited view.
typedef struct puffs_base_limit1 {
uint64_t* ptr_to_len; // Pointer to N.
struct puffs_base_limit1* next; // Linked list of limits.
} puffs_base_limit1;
typedef struct {
puffs_base_buf1* buf;
puffs_base_limit limit;
puffs_base_limit1 limit;
} puffs_base_reader1;
typedef struct {
puffs_base_buf1* buf;
puffs_base_limit limit;
puffs_base_limit1 limit;
} puffs_base_writer1;
#endif // PUFFS_BASE_HEADER_H
+2 -1
View File
@@ -7,7 +7,8 @@ package main
const baseHeader = "" +
"#ifndef PUFFS_BASE_HEADER_H\n#define PUFFS_BASE_HEADER_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#include <stdbool.h>\n#include <stdint.h>\n#include <string.h>\n\n// Puffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n//\n// Similarly, the word size is at most 64 bits because it assumes that\n// converting a usize to u64 will never overflow.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#elif __WORDSIZE > 64\n#error \"Puffs requires a word size of at most 64 bits\"\n#endif\n\n// PUFFS_VERSION is the major.minor version number as a uint32. The major\n// number is the high 16 bits. The minor number is the low 16 bits.\n//\n// The intention is to bump the version number at least on every API " +
"/ ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in base-header.h.\n#define PUFFS_VERSION (0x00001)\n\n// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length) plus\n// additional indexes into that buffer.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef struct {\n uint8_t* ptr; // Pointer.\n size_t len; // Length.\n size_t wi; // Write index. Invariant: wi <= len.\n size_t ri; // Read index. Invariant: ri <= wi.\n bool closed; // No further writes are expected.\n} puffs_base_buf1;\n\ntypedef struct puffs_base_limit {\n uint64_t* len;\n struct puffs_base_limit* next;\n} puffs_base_limit;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit limit;\n} puffs_base_reader1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit limit;\n} puffs_base_writer1;\n\n#endif // PUFFS_BASE_HEADER_H\n" +
"/ ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in base-header.h.\n#define PUFFS_VERSION (0x00001)\n\n// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length) plus\n// additional indexes into that buffer.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef struct {\n uint8_t* ptr; // Pointer.\n size_t len; // Length.\n size_t wi; // Write index. Invariant: wi <= len.\n size_t ri; // Read index. Invariant: ri <= wi.\n bool closed; // No further writes are expected.\n} puffs_base_buf1;\n\n// puffs_base_limit1 provides a limited view of a 1-dimensional byte stream:\n// its first N bytes. That N can be greater than a buffer's current read or\n// write capacity. N decreases naturally over time as bytes are read from or\n// written to the stream.\n//\n// A value with all fields NULL or zero is a valid, unlimited view.\ntypedef struct puffs_base_limit1 {\n uint64_t* ptr_to_len" +
"; // Pointer to N.\n struct puffs_base_limit1* next; // Linked list of limits.\n} puffs_base_limit1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit1 limit;\n} puffs_base_reader1;\n\ntypedef struct {\n puffs_base_buf1* buf;\n puffs_base_limit1 limit;\n} puffs_base_writer1;\n\n#endif // PUFFS_BASE_HEADER_H\n" +
""
const baseImpl = "" +
+5 -4
View File
@@ -990,7 +990,8 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
// TODO: suspend coroutine state.
//
// TODO: loop over all limits, not just the first one.
g.printf("if ((%ssrc.buf->ri >= %ssrc.buf->wi) || (%ssrc.limit.len && (*%ssrc.limit.len == 0))) {",
g.printf("if ((%ssrc.buf->ri >= %ssrc.buf->wi) || "+
"(%ssrc.limit.ptr_to_len && (*%ssrc.limit.ptr_to_len == 0))) {",
aPrefix, aPrefix, aPrefix, aPrefix)
g.printf("status = ((%ssrc.buf->closed) && (%ssrc.buf->ri == %ssrc.buf->wi)) ?"+
"puffs_%s_error_unexpected_eof : puffs_%s_status_short_read;",
@@ -1007,7 +1008,7 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
return err
}
g.printf(" = %ssrc.buf->ptr[%ssrc.buf->ri++];\n", aPrefix, aPrefix)
g.printf("if (%ssrc.limit.len) { (*%ssrc.limit.len)--; }\n", aPrefix, aPrefix)
g.printf("if (%ssrc.limit.ptr_to_len) { (*%ssrc.limit.ptr_to_len)--; }\n", aPrefix, aPrefix)
} else if isInSrc(g.tm, n, t.KeySkip32, 1) {
if g.perFunc.tempW > maxTemp {
@@ -1243,8 +1244,8 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
// TODO: don't hard code so much detail.
g.writes("(puffs_base_reader1) {")
g.writes(".buf = a_src.buf,")
g.writes(".limit = (puffs_base_limit) {")
g.printf(".len = &%s%s,", lPrefix, g.perFunc.limitVarName)
g.writes(".limit = (puffs_base_limit1) {")
g.printf(".ptr_to_len = &%s%s,", lPrefix, g.perFunc.limitVarName)
g.writes(".next = &a_src.limit,")
g.writes("}}")
g.perFunc.limitVarName = ""