diff --git a/cmd/puffs-c/internal/cgen/base-header.h b/cmd/puffs-c/internal/cgen/base-header.h index cd1b76ac..a14cd314 100644 --- a/cmd/puffs-c/internal/cgen/base-header.h +++ b/cmd/puffs-c/internal/cgen/base-header.h @@ -76,6 +76,17 @@ typedef struct { bool closed; // No further writes are expected. } puffs_base__buf1; +// 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; + #ifdef PUFFS_USE_NO_OP_PERFORMANCE_HACKS typedef struct { void* always_null0; @@ -91,12 +102,8 @@ typedef struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. struct { - uint8_t* limit; + puffs_base__limit1 limit; uint8_t* mark; - // use_limit is redundant, in that it always equals (limit != NULL), but - // having a separate bool can have a significant performance effect with - // gcc 4.8.4 (e.g. 1.1x on some benchmarks). - bool use_limit; #ifdef PUFFS_USE_NO_OP_PERFORMANCE_HACKS struct { puffs_base__paired_nulls* noph0; @@ -114,12 +121,8 @@ typedef struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. struct { - uint8_t* limit; + puffs_base__limit1 limit; uint8_t* mark; - // use_limit is redundant, in that it always equals (limit != NULL), but - // having a separate bool can have a significant performance effect with - // gcc 4.8.4 (e.g. 1.1x on some benchmarks). - bool use_limit; } private_impl; } puffs_base__writer1; diff --git a/cmd/puffs-c/internal/cgen/base-impl.h b/cmd/puffs-c/internal/cgen/base-impl.h index 26335f32..f32c1d34 100644 --- a/cmd/puffs-c/internal/cgen/base-impl.h +++ b/cmd/puffs-c/internal/cgen/base-impl.h @@ -278,31 +278,21 @@ static inline uint32_t puffs_base__writer1__copy_from_slice32( // public (in base-header.h). We assume that, at the boundary between user code // and Puffs code, the reader1 and writer1's private_impl fields (including // limit and mark) are NULL. Otherwise, some internal assumptions break down. -// For example, the limit field is represented as a pointer, even though -// conceptually it is a count, but that pointer-to-count correspondence becomes -// invalid if a buffer is re-used (e.g. on resuming a coroutine). +// For example, limits could be represented as pointers, even though +// conceptually they are counts, but that pointer-to-count correspondence +// becomes invalid if a buffer is re-used (e.g. on resuming a coroutine). // // Admittedly, some of the Puffs test code calls these methods, but that test // code is still Puffs code, not user code. Other Puffs test code modifies // private_impl fields directly. -static inline puffs_base__empty_struct puffs_base__reader1__limit( +static inline puffs_base__reader1 puffs_base__reader1__limit( puffs_base__reader1* o, - uint8_t* ptr, - uint64_t limit) { - if (!o->buf) { - o->private_impl.limit = NULL; - } else { - uint64_t len = o->buf->wi - o->buf->ri; - if (limit > len) { - limit = len; - } - if (!o->private_impl.limit || (limit < (o->private_impl.limit - ptr))) { - o->private_impl.limit = ptr + limit; - } - } - o->private_impl.use_limit = true; - return ((puffs_base__empty_struct){}); + uint64_t* ptr_to_len) { + puffs_base__reader1 ret = *o; + ret.private_impl.limit.ptr_to_len = ptr_to_len; + ret.private_impl.limit.next = &o->private_impl.limit; + return ret; } static inline puffs_base__empty_struct puffs_base__reader1__mark( @@ -312,24 +302,7 @@ static inline puffs_base__empty_struct puffs_base__reader1__mark( return ((puffs_base__empty_struct){}); } -static inline puffs_base__empty_struct puffs_base__writer1__limit( - puffs_base__writer1* o, - uint8_t* ptr, - uint64_t limit) { - if (!o->buf) { - o->private_impl.limit = NULL; - } else { - uint64_t len = o->buf->len - o->buf->wi; - if (limit > len) { - limit = len; - } - if (!o->private_impl.limit || (limit < (o->private_impl.limit - ptr))) { - o->private_impl.limit = ptr + limit; - } - } - o->private_impl.use_limit = true; - return ((puffs_base__empty_struct){}); -} +// TODO: static inline puffs_base__writer1 puffs_base__writer1__limit() static inline puffs_base__empty_struct puffs_base__writer1__mark( puffs_base__writer1* o, diff --git a/cmd/puffs-c/internal/cgen/data.go b/cmd/puffs-c/internal/cgen/data.go index 5db08258..8c3182bb 100644 --- a/cmd/puffs-c/internal/cgen/data.go +++ b/cmd/puffs-c/internal/cgen/data.go @@ -19,9 +19,9 @@ package cgen const baseHeader = "" + "#ifndef PUFFS_BASE_HEADER_H\n#define PUFFS_BASE_HEADER_H\n\n// Copyright 2017 The Puffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n#include \n#include \n#include \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// Si" + "milarly, 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_USE_NO_OP_PERFORMANCE_HACKS enables code paths that look like\n// redundant no-ops, but for reasons to be investigated, can have dramatic\n// performance effects with gcc 4.8.4 (e.g. 1.2x on some benchmarks).\n//\n// TODO: investigate; delete these hacks (without regressing performance).\n// The order matters here. Clang als" + - "o defines \"__GNUC__\".\n#if defined(__clang__)\n#elif defined(__GNUC__)\n#define PUFFS_USE_NO_OP_PERFORMANCE_HACKS 1\n#endif\n\n// puffs_base__slice_u8 is a 1-dimensional buffer (a pointer and length).\n//\n// A value with all fields NULL or zero is a valid, empty slice.\ntypedef struct {\n uint8_t* ptr;\n size_t len;\n} puffs_base__slice_u8;\n\n// puffs_base__buf1 is a 1-dimensional buffer (a pointer and length), plus\n// additional indexes into that buffer, plus an opened / closed flag.\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#ifdef PUFFS_USE_NO_OP_PERFORMANCE_HACKS\ntypedef struct {\n void* always_null0;\n void* always_null1;\n} puffs_base__paired_nulls;\n#endif\n\ntypedef struct {\n // TODO: move buf into private_impl? As it is, it looks like u" + - "sers can modify\n // the buf field to point to a different buffer, which can turn the limit and\n // mark fields into dangling pointers.\n puffs_base__buf1* buf;\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n uint8_t* limit;\n uint8_t* mark;\n // use_limit is redundant, in that it always equals (limit != NULL), but\n // having a separate bool can have a significant performance effect with\n // gcc 4.8.4 (e.g. 1.1x on some benchmarks).\n bool use_limit;\n#ifdef PUFFS_USE_NO_OP_PERFORMANCE_HACKS\n struct {\n puffs_base__paired_nulls* noph0;\n uint32_t noph1;\n } * no_op_performance_hacks;\n#endif\n } private_impl;\n} puffs_base__reader1;\n\ntypedef struct {\n // TODO: move buf into private_impl? As it is, it looks like users can modify\n // the buf field to point to a different buffer, which can turn the limit and\n // mark fields into dangling pointers.\n puffs_base__buf1* buf;\n // Do not access th" + - "e private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n uint8_t* limit;\n uint8_t* mark;\n // use_limit is redundant, in that it always equals (limit != NULL), but\n // having a separate bool can have a significant performance effect with\n // gcc 4.8.4 (e.g. 1.1x on some benchmarks).\n bool use_limit;\n } private_impl;\n} puffs_base__writer1;\n\n#endif // PUFFS_BASE_HEADER_H\n" + + "o defines \"__GNUC__\".\n#if defined(__clang__)\n#elif defined(__GNUC__)\n#define PUFFS_USE_NO_OP_PERFORMANCE_HACKS 1\n#endif\n\n// puffs_base__slice_u8 is a 1-dimensional buffer (a pointer and length).\n//\n// A value with all fields NULL or zero is a valid, empty slice.\ntypedef struct {\n uint8_t* ptr;\n size_t len;\n} puffs_base__slice_u8;\n\n// puffs_base__buf1 is a 1-dimensional buffer (a pointer and length), plus\n// additional indexes into that buffer, plus an opened / closed flag.\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 r" + + "ead 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\n#ifdef PUFFS_USE_NO_OP_PERFORMANCE_HACKS\ntypedef struct {\n void* always_null0;\n void* always_null1;\n} puffs_base__paired_nulls;\n#endif\n\ntypedef struct {\n // TODO: move buf into private_impl? As it is, it looks like users can modify\n // the buf field to point to a different buffer, which can turn the limit and\n // mark fields into dangling pointers.\n puffs_base__buf1* buf;\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n puffs_base__limit1 limit;\n uint8_t* mark;\n#ifdef PUFFS_USE_NO_OP_PERFORMANCE_HACKS\n struct {\n puffs_base__paired_nulls* noph0;\n uint32_t noph1;\n } * no_op_performance_hacks;\n#endif\n } private_impl;" + + "\n} puffs_base__reader1;\n\ntypedef struct {\n // TODO: move buf into private_impl? As it is, it looks like users can modify\n // the buf field to point to a different buffer, which can turn the limit and\n // mark fields into dangling pointers.\n puffs_base__buf1* buf;\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n puffs_base__limit1 limit;\n uint8_t* mark;\n } private_impl;\n} puffs_base__writer1;\n\n#endif // PUFFS_BASE_HEADER_H\n" + "" const baseImpl = "" + @@ -33,9 +33,8 @@ const baseImpl = "" + "_ij(\n puffs_base__slice_u8 s,\n uint64_t i,\n uint64_t j) {\n if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {\n return ((puffs_base__slice_u8){\n .ptr = s.ptr + i,\n .len = j - i,\n });\n }\n return ((puffs_base__slice_u8){});\n}\n\n// puffs_base__slice_u8__prefix returns up to the first up_to bytes of s.\nstatic inline puffs_base__slice_u8 puffs_base__slice_u8__prefix(\n puffs_base__slice_u8 s,\n uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.len = up_to;\n }\n return s;\n}\n\n// puffs_base__slice_u8__suffix returns up to the last up_to bytes of s.\nstatic inline puffs_base__slice_u8 puffs_base__slice_u8_suffix(\n puffs_base__slice_u8 s,\n uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.ptr += (uint64_t)(s.len) - up_to;\n s.len = up_to;\n }\n return s;\n}\n\n// puffs_base__slice_u8__copy_from_slice calls memmove(dst.ptr, src.ptr,\n// length) where length is the minimum of dst.len and src.len.\n//\n// Passing a puffs_base__slice_u8 with all fields NULL or zero (a v" + "alid, empty\n// slice) is valid and results in a no-op.\nstatic inline uint64_t puffs_base__slice_u8__copy_from_slice(\n puffs_base__slice_u8 dst,\n puffs_base__slice_u8 src) {\n size_t length = dst.len < src.len ? dst.len : src.len;\n if (length > 0) {\n memmove(dst.ptr, src.ptr, length);\n }\n return length;\n}\n\nstatic inline uint32_t puffs_base__writer1__copy_from_history32(\n uint8_t** ptr_ptr,\n uint8_t* start, // May be NULL, meaning an unmarked writer1.\n uint8_t* end,\n uint32_t distance,\n uint32_t length) {\n if (!start) {\n return 0;\n }\n uint8_t* ptr = *ptr_ptr;\n size_t d = ptr - start;\n if ((d == 0) || (d < (size_t)(distance))) {\n return 0;\n }\n start = ptr - distance;\n size_t n = end - ptr;\n if ((size_t)(length) > n) {\n length = n;\n } else {\n n = length;\n }\n // TODO: is manual unrolling actually helpful?\n for (; n >= 8; n -= 8) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *star" + "t++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; n; n--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\nstatic inline uint32_t puffs_base__writer1__copy_from_reader32(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n uint8_t** ptr_rptr,\n uint8_t* rend,\n uint32_t length) {\n uint8_t* wptr = *ptr_wptr;\n size_t n = length;\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n uint8_t* rptr = *ptr_rptr;\n if (n > rend - rptr) {\n n = rend - rptr;\n }\n if (n > 0) {\n memmove(wptr, rptr, n);\n *ptr_wptr += n;\n *ptr_rptr += n;\n }\n return n;\n}\n\nstatic inline uint64_t puffs_base__writer1__copy_from_slice(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n puffs_base__slice_u8 src) {\n uint8_t* wptr = *ptr_wptr;\n size_t n = src.len;\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n if (n > 0) {\n memmove(wptr, src.ptr, n);\n *ptr_wptr += n;\n }\n return n;\n}\n\nstatic inline uint32_t puffs_base__writer1__copy_from_slice32(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n " + - " puffs_base__slice_u8 src,\n uint32_t length) {\n uint8_t* wptr = *ptr_wptr;\n size_t n = src.len;\n if (n > length) {\n n = length;\n }\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n if (n > 0) {\n memmove(wptr, src.ptr, n);\n *ptr_wptr += n;\n }\n return n;\n}\n\n// Note that the *__limit and *__mark methods are private (in base-impl.h) not\n// public (in base-header.h). We assume that, at the boundary between user code\n// and Puffs code, the reader1 and writer1's private_impl fields (including\n// limit and mark) are NULL. Otherwise, some internal assumptions break down.\n// For example, the limit field is represented as a pointer, even though\n// conceptually it is a count, but that pointer-to-count correspondence becomes\n// invalid if a buffer is re-used (e.g. on resuming a coroutine).\n//\n// Admittedly, some of the Puffs test code calls these methods, but that test\n// code is still Puffs code, not user code. Other Puffs test code modifies\n// private_impl fields directly.\n\nstatic inline puffs_base__" + - "empty_struct puffs_base__reader1__limit(\n puffs_base__reader1* o,\n uint8_t* ptr,\n uint64_t limit) {\n if (!o->buf) {\n o->private_impl.limit = NULL;\n } else {\n uint64_t len = o->buf->wi - o->buf->ri;\n if (limit > len) {\n limit = len;\n }\n if (!o->private_impl.limit || (limit < (o->private_impl.limit - ptr))) {\n o->private_impl.limit = ptr + limit;\n }\n }\n o->private_impl.use_limit = true;\n return ((puffs_base__empty_struct){});\n}\n\nstatic inline puffs_base__empty_struct puffs_base__reader1__mark(\n puffs_base__reader1* o,\n uint8_t* mark) {\n o->private_impl.mark = mark;\n return ((puffs_base__empty_struct){});\n}\n\nstatic inline puffs_base__empty_struct puffs_base__writer1__limit(\n puffs_base__writer1* o,\n uint8_t* ptr,\n uint64_t limit) {\n if (!o->buf) {\n o->private_impl.limit = NULL;\n } else {\n uint64_t len = o->buf->len - o->buf->wi;\n if (limit > len) {\n limit = len;\n }\n if (!o->private_impl.limit || (limit < (o->private_impl.limit - ptr" + - "))) {\n o->private_impl.limit = ptr + limit;\n }\n }\n o->private_impl.use_limit = true;\n return ((puffs_base__empty_struct){});\n}\n\nstatic inline puffs_base__empty_struct puffs_base__writer1__mark(\n puffs_base__writer1* o,\n uint8_t* mark) {\n o->private_impl.mark = mark;\n return ((puffs_base__empty_struct){});\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" + + " puffs_base__slice_u8 src,\n uint32_t length) {\n uint8_t* wptr = *ptr_wptr;\n size_t n = src.len;\n if (n > length) {\n n = length;\n }\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n if (n > 0) {\n memmove(wptr, src.ptr, n);\n *ptr_wptr += n;\n }\n return n;\n}\n\n// Note that the *__limit and *__mark methods are private (in base-impl.h) not\n// public (in base-header.h). We assume that, at the boundary between user code\n// and Puffs code, the reader1 and writer1's private_impl fields (including\n// limit and mark) are NULL. Otherwise, some internal assumptions break down.\n// For example, limits could be represented as pointers, even though\n// conceptually they are counts, but that pointer-to-count correspondence\n// becomes invalid if a buffer is re-used (e.g. on resuming a coroutine).\n//\n// Admittedly, some of the Puffs test code calls these methods, but that test\n// code is still Puffs code, not user code. Other Puffs test code modifies\n// private_impl fields directly.\n\nstatic inline puffs_base__re" + + "ader1 puffs_base__reader1__limit(\n puffs_base__reader1* o,\n uint64_t* ptr_to_len) {\n puffs_base__reader1 ret = *o;\n ret.private_impl.limit.ptr_to_len = ptr_to_len;\n ret.private_impl.limit.next = &o->private_impl.limit;\n return ret;\n}\n\nstatic inline puffs_base__empty_struct puffs_base__reader1__mark(\n puffs_base__reader1* o,\n uint8_t* mark) {\n o->private_impl.mark = mark;\n return ((puffs_base__empty_struct){});\n}\n\n// TODO: static inline puffs_base__writer1 puffs_base__writer1__limit()\n\nstatic inline puffs_base__empty_struct puffs_base__writer1__mark(\n puffs_base__writer1* o,\n uint8_t* mark) {\n o->private_impl.mark = mark;\n return ((puffs_base__empty_struct){});\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" + "" type template_args_short_read struct { @@ -44,7 +43,7 @@ type template_args_short_read struct { } func template_short_read(b *buffer, args template_args_short_read) error { - b.printf("short_read_%s:\nif (a_%s.buf && a_%s.buf->closed &&\n!a_%s.private_impl.use_limit) {\nstatus = %sERROR_UNEXPECTED_EOF;\ngoto exit;\n}\nstatus = %sSUSPENSION_SHORT_READ;\ngoto suspend;\n", + b.printf("short_read_%s:\nif (a_%s.buf && a_%s.buf->closed &&\n!a_%s.private_impl.limit.ptr_to_len) {\nstatus = %sERROR_UNEXPECTED_EOF;\ngoto exit;\n}\nstatus = %sSUSPENSION_SHORT_READ;\ngoto suspend;\n", args.name, args.name, args.name, diff --git a/cmd/puffs-c/internal/cgen/expr.go b/cmd/puffs-c/internal/cgen/expr.go index f7f61e5f..791b9065 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -172,7 +172,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare return nil } if isInSrc(g.tm, n, t.KeyLimit, 1) { - return fmt.Errorf("TODO: in.src.limit") + return fmt.Errorf(`TODO: cgen an "in.src.limit" expression`) } if isInSrc(g.tm, n, t.KeyMark, 0) { b.printf("puffs_base__reader1__mark(&%ssrc, %srptr_src)", aPrefix, bPrefix) @@ -186,7 +186,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare return nil } if isInDst(g.tm, n, t.KeyLimit, 1) { - return fmt.Errorf("TODO: in.dst.limit") + return fmt.Errorf(`TODO: cgen an "in.dst.limit" expression`) } if isInDst(g.tm, n, t.KeyMark, 0) { // TODO: is a private_impl.mark the right representation? What if @@ -307,13 +307,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare return nil } if isThatMethod(g.tm, n, t.KeyLimit, 1) { - // TODO: don't hard-code v_r or b_rptr_src. - b.printf("puffs_base__reader1__limit(&v_r, b_rptr_src,") - if err := g.writeExpr(b, n.Args()[0].Arg().Value(), rp, parenthesesOptional, depth); err != nil { - return err - } - b.writes(")") - return nil + return fmt.Errorf(`TODO: cgen a "foo.limit" expression`) } if isThatMethod(g.tm, n, t.KeyMark, 0) { // TODO: don't hard-code v_r or b_rptr_src. diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index 237750f0..630d44f0 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -550,8 +550,10 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error { temp := g.currFunk.tempW g.currFunk.tempW++ - // TODO: don't hard-code a_dst or v_r. - b.printf("%sstatus %s%d = %slzw_decoder__decode(&self->private_impl.f_lzw, %sdst, %sr);\n", + // TODO: don't hard-code a_dst or v_r or l_rlimit or v_block_size. + b.writes("uint64_t l_rlimit0 = v_block_size;\n") + b.printf("%sstatus %s%d = %slzw_decoder__decode(&self->private_impl.f_lzw, %sdst,"+ + "puffs_base__reader1__limit(&%sr, &l_rlimit0));\n", g.pkgPrefix, tPrefix, temp, g.pkgPrefix, aPrefix, vPrefix) if err := g.writeLoadExprDerivedVars(b, n); err != nil { diff --git a/cmd/puffs-c/internal/cgen/templates.h b/cmd/puffs-c/internal/cgen/templates.h index b756e291..e1f832f5 100644 --- a/cmd/puffs-c/internal/cgen/templates.h +++ b/cmd/puffs-c/internal/cgen/templates.h @@ -24,7 +24,7 @@ template short_read(string qPKGPREFIXq, string qnameq) { short_read_qnameq: if (a_qnameq.buf && a_qnameq.buf->closed && - !a_qnameq.private_impl.use_limit) { + !a_qnameq.private_impl.limit.ptr_to_len) { status = qPKGPREFIXqERROR_UNEXPECTED_EOF; goto exit; } diff --git a/cmd/puffs-c/internal/cgen/var.go b/cmd/puffs-c/internal/cgen/var.go index 99dbc3f2..4b6a8a8f 100644 --- a/cmd/puffs-c/internal/cgen/var.go +++ b/cmd/puffs-c/internal/cgen/var.go @@ -100,12 +100,18 @@ func (g *gen) writeLoadDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, header b.printf("%srptr_%s = %s%s.buf->ptr + %s%s.buf->ri;", bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr) + if header { b.printf("uint64_t len = %s%s.buf->wi - %s%s.buf->ri;", aPrefix, nameStr, aPrefix, nameStr) + + b.printf("puffs_base__limit1* lim;\n") + b.printf("for (lim = &%s%s.private_impl.limit; lim; lim = lim->next) {\n", aPrefix, nameStr) + b.printf("if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {\n") + b.printf("len = *lim->ptr_to_len;\n") + b.printf("}\n") + b.printf("}\n") + b.printf("%srend_%s = %srptr_%s + len;", bPrefix, nameStr, bPrefix, nameStr) - b.printf("if (%s%s.private_impl.limit && (%srend_%s > %s%s.private_impl.limit)) {"+ - " %srend_%s = %s%s.private_impl.limit; }", - aPrefix, nameStr, bPrefix, nameStr, aPrefix, nameStr, bPrefix, nameStr, aPrefix, nameStr) } b.printf("}\n") @@ -119,14 +125,20 @@ func (g *gen) writeLoadDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, header b.printf("%swptr_%s = %s%s.buf->ptr + %s%s.buf->wi;", bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr) + if header { b.printf("%swend_%s = %swptr_%s;", bPrefix, nameStr, bPrefix, nameStr) b.printf("if (!%s%s.buf->closed) {", aPrefix, nameStr) b.printf("uint64_t len = %s%s.buf->len - %s%s.buf->wi;", aPrefix, nameStr, aPrefix, nameStr) + + b.printf("puffs_base__limit1* lim;\n") + b.printf("for (lim = &%s%s.private_impl.limit; lim; lim = lim->next) {\n", aPrefix, nameStr) + b.printf("if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {\n") + b.printf("len = *lim->ptr_to_len;\n") + b.printf("}\n") + b.printf("}\n") + b.printf("%swend_%s += len;", bPrefix, nameStr) - b.printf("if (%s%s.private_impl.limit && (%swend_%s > %s%s.private_impl.limit)) {"+ - " %swend_%s = %s%s.private_impl.limit; }", - aPrefix, nameStr, bPrefix, nameStr, aPrefix, nameStr, bPrefix, nameStr, aPrefix, nameStr) b.printf("}\n") } @@ -156,6 +168,14 @@ func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, footer b.printf("size_t n = %srptr_%s - (%s%s.buf->ptr + %s%s.buf->ri);", bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr) b.printf("%s%s.buf->ri += n;", aPrefix, nameStr) + + b.printf("puffs_base__limit1* lim;\n") + b.printf("for (lim = &%s%s.private_impl.limit; lim; lim = lim->next) {\n", aPrefix, nameStr) + b.printf("if (lim->ptr_to_len) {\n") + b.printf("*lim->ptr_to_len -= n;\n") + b.printf("}\n") + b.printf("}\n") + if footer { b.printf("PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%srend_%s);", bPrefix, nameStr) } @@ -169,6 +189,13 @@ func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, footer bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr) b.printf("%s%s.buf->wi += n;", aPrefix, nameStr) + b.printf("puffs_base__limit1* lim;\n") + b.printf("for (lim = &%s%s.private_impl.limit; lim; lim = lim->next) {\n", aPrefix, nameStr) + b.printf("if (lim->ptr_to_len) {\n") + b.printf("*lim->ptr_to_len -= n;\n") + b.printf("}\n") + b.printf("}\n") + if footer { b.printf("PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%swend_%s);", bPrefix, nameStr) } diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 64981952..a27b9479 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -79,6 +79,17 @@ typedef struct { bool closed; // No further writes are expected. } puffs_base__buf1; +// 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; + #ifdef PUFFS_USE_NO_OP_PERFORMANCE_HACKS typedef struct { void* always_null0; @@ -94,12 +105,8 @@ typedef struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. struct { - uint8_t* limit; + puffs_base__limit1 limit; uint8_t* mark; - // use_limit is redundant, in that it always equals (limit != NULL), but - // having a separate bool can have a significant performance effect with - // gcc 4.8.4 (e.g. 1.1x on some benchmarks). - bool use_limit; #ifdef PUFFS_USE_NO_OP_PERFORMANCE_HACKS struct { puffs_base__paired_nulls* noph0; @@ -117,12 +124,8 @@ typedef struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. struct { - uint8_t* limit; + puffs_base__limit1 limit; uint8_t* mark; - // use_limit is redundant, in that it always equals (limit != NULL), but - // having a separate bool can have a significant performance effect with - // gcc 4.8.4 (e.g. 1.1x on some benchmarks). - bool use_limit; } private_impl; } puffs_base__writer1; @@ -632,31 +635,21 @@ static inline uint32_t puffs_base__writer1__copy_from_slice32( // public (in base-header.h). We assume that, at the boundary between user code // and Puffs code, the reader1 and writer1's private_impl fields (including // limit and mark) are NULL. Otherwise, some internal assumptions break down. -// For example, the limit field is represented as a pointer, even though -// conceptually it is a count, but that pointer-to-count correspondence becomes -// invalid if a buffer is re-used (e.g. on resuming a coroutine). +// For example, limits could be represented as pointers, even though +// conceptually they are counts, but that pointer-to-count correspondence +// becomes invalid if a buffer is re-used (e.g. on resuming a coroutine). // // Admittedly, some of the Puffs test code calls these methods, but that test // code is still Puffs code, not user code. Other Puffs test code modifies // private_impl fields directly. -static inline puffs_base__empty_struct puffs_base__reader1__limit( +static inline puffs_base__reader1 puffs_base__reader1__limit( puffs_base__reader1* o, - uint8_t* ptr, - uint64_t limit) { - if (!o->buf) { - o->private_impl.limit = NULL; - } else { - uint64_t len = o->buf->wi - o->buf->ri; - if (limit > len) { - limit = len; - } - if (!o->private_impl.limit || (limit < (o->private_impl.limit - ptr))) { - o->private_impl.limit = ptr + limit; - } - } - o->private_impl.use_limit = true; - return ((puffs_base__empty_struct){}); + uint64_t* ptr_to_len) { + puffs_base__reader1 ret = *o; + ret.private_impl.limit.ptr_to_len = ptr_to_len; + ret.private_impl.limit.next = &o->private_impl.limit; + return ret; } static inline puffs_base__empty_struct puffs_base__reader1__mark( @@ -666,24 +659,7 @@ static inline puffs_base__empty_struct puffs_base__reader1__mark( return ((puffs_base__empty_struct){}); } -static inline puffs_base__empty_struct puffs_base__writer1__limit( - puffs_base__writer1* o, - uint8_t* ptr, - uint64_t limit) { - if (!o->buf) { - o->private_impl.limit = NULL; - } else { - uint64_t len = o->buf->len - o->buf->wi; - if (limit > len) { - limit = len; - } - if (!o->private_impl.limit || (limit < (o->private_impl.limit - ptr))) { - o->private_impl.limit = ptr + limit; - } - } - o->private_impl.use_limit = true; - return ((puffs_base__empty_struct){}); -} +// TODO: static inline puffs_base__writer1 puffs_base__writer1__limit() static inline puffs_base__empty_struct puffs_base__writer1__mark( puffs_base__writer1* o, @@ -919,10 +895,13 @@ puffs_flate__status puffs_flate__flate_decoder__decode( b_wend_dst = b_wptr_dst; if (!a_dst.buf->closed) { uint64_t len = a_dst.buf->len - a_dst.buf->wi; - b_wend_dst += len; - if (a_dst.private_impl.limit && (b_wend_dst > a_dst.private_impl.limit)) { - b_wend_dst = a_dst.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_dst.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_wend_dst += len; } } @@ -942,6 +921,12 @@ puffs_flate__status puffs_flate__flate_decoder__decode( if (a_dst.buf) { size_t n = b_wptr_dst - (a_dst.buf->ptr + a_dst.buf->wi); a_dst.buf->wi += n; + puffs_base__limit1* lim; + for (lim = &a_dst.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } } puffs_flate__status t_0 = puffs_flate__flate_decoder__decode_blocks(self, a_dst, a_src); @@ -1011,6 +996,12 @@ exit: if (a_dst.buf) { size_t n = b_wptr_dst - (a_dst.buf->ptr + a_dst.buf->wi); a_dst.buf->wi += n; + puffs_base__limit1* lim; + for (lim = &a_dst.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_wend_dst); } @@ -1032,10 +1023,13 @@ puffs_flate__status puffs_flate__flate_decoder__decode_blocks( if (a_src.buf) { b_rptr_src = a_src.buf->ptr + a_src.buf->ri; uint64_t len = a_src.buf->wi - a_src.buf->ri; - b_rend_src = b_rptr_src + len; - if (a_src.private_impl.limit && (b_rend_src > a_src.private_impl.limit)) { - b_rend_src = a_src.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_rend_src = b_rptr_src + len; } uint32_t coro_susp_point = @@ -1069,6 +1063,12 @@ puffs_flate__status puffs_flate__flate_decoder__decode_blocks( if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } } status = puffs_flate__flate_decoder__decode_uncompressed(self, a_dst, a_src); @@ -1090,6 +1090,12 @@ puffs_flate__status puffs_flate__flate_decoder__decode_blocks( if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } } status = puffs_flate__flate_decoder__init_dynamic_huffman(self, a_src); if (a_src.buf) { @@ -1106,6 +1112,12 @@ puffs_flate__status puffs_flate__flate_decoder__decode_blocks( if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } } status = puffs_flate__flate_decoder__decode_huffman(self, a_dst, a_src); if (a_src.buf) { @@ -1132,13 +1144,19 @@ exit: if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src); } return status; short_read_src: - if (a_src.buf && a_src.buf->closed && !a_src.private_impl.use_limit) { + if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) { status = PUFFS_FLATE__ERROR_UNEXPECTED_EOF; goto exit; } @@ -1162,10 +1180,13 @@ puffs_flate__status puffs_flate__flate_decoder__decode_uncompressed( b_wend_dst = b_wptr_dst; if (!a_dst.buf->closed) { uint64_t len = a_dst.buf->len - a_dst.buf->wi; - b_wend_dst += len; - if (a_dst.private_impl.limit && (b_wend_dst > a_dst.private_impl.limit)) { - b_wend_dst = a_dst.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_dst.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_wend_dst += len; } } uint8_t* b_rptr_src = NULL; @@ -1173,10 +1194,13 @@ puffs_flate__status puffs_flate__flate_decoder__decode_uncompressed( if (a_src.buf) { b_rptr_src = a_src.buf->ptr + a_src.buf->ri; uint64_t len = a_src.buf->wi - a_src.buf->ri; - b_rend_src = b_rptr_src + len; - if (a_src.private_impl.limit && (b_rend_src > a_src.private_impl.limit)) { - b_rend_src = a_src.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_rend_src = b_rptr_src + len; } uint32_t coro_susp_point = @@ -1261,18 +1285,30 @@ exit: if (a_dst.buf) { size_t n = b_wptr_dst - (a_dst.buf->ptr + a_dst.buf->wi); a_dst.buf->wi += n; + puffs_base__limit1* lim; + for (lim = &a_dst.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_wend_dst); } if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src); } return status; short_read_src: - if (a_src.buf && a_src.buf->closed && !a_src.private_impl.use_limit) { + if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) { status = PUFFS_FLATE__ERROR_UNEXPECTED_EOF; goto exit; } @@ -1307,10 +1343,13 @@ puffs_flate__status puffs_flate__flate_decoder__decode_huffman( b_wend_dst = b_wptr_dst; if (!a_dst.buf->closed) { uint64_t len = a_dst.buf->len - a_dst.buf->wi; - b_wend_dst += len; - if (a_dst.private_impl.limit && (b_wend_dst > a_dst.private_impl.limit)) { - b_wend_dst = a_dst.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_dst.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_wend_dst += len; } } uint8_t* b_rptr_src = NULL; @@ -1318,10 +1357,13 @@ puffs_flate__status puffs_flate__flate_decoder__decode_huffman( if (a_src.buf) { b_rptr_src = a_src.buf->ptr + a_src.buf->ri; uint64_t len = a_src.buf->wi - a_src.buf->ri; - b_rend_src = b_rptr_src + len; - if (a_src.private_impl.limit && (b_rend_src > a_src.private_impl.limit)) { - b_rend_src = a_src.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_rend_src = b_rptr_src + len; } uint32_t coro_susp_point = @@ -1654,18 +1696,30 @@ exit: if (a_dst.buf) { size_t n = b_wptr_dst - (a_dst.buf->ptr + a_dst.buf->wi); a_dst.buf->wi += n; + puffs_base__limit1* lim; + for (lim = &a_dst.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_wend_dst); } if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src); } return status; short_read_src: - if (a_src.buf && a_src.buf->closed && !a_src.private_impl.use_limit) { + if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) { status = PUFFS_FLATE__ERROR_UNEXPECTED_EOF; goto exit; } @@ -1757,10 +1811,13 @@ puffs_flate__status puffs_flate__flate_decoder__init_dynamic_huffman( if (a_src.buf) { b_rptr_src = a_src.buf->ptr + a_src.buf->ri; uint64_t len = a_src.buf->wi - a_src.buf->ri; - b_rend_src = b_rptr_src + len; - if (a_src.private_impl.limit && (b_rend_src > a_src.private_impl.limit)) { - b_rend_src = a_src.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_rend_src = b_rptr_src + len; } uint32_t coro_susp_point = @@ -1965,13 +2022,19 @@ exit: if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src); } return status; short_read_src: - if (a_src.buf && a_src.buf->closed && !a_src.private_impl.use_limit) { + if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) { status = PUFFS_FLATE__ERROR_UNEXPECTED_EOF; goto exit; } @@ -2285,10 +2348,13 @@ puffs_flate__status puffs_flate__zlib_decoder__decode( b_wend_dst = b_wptr_dst; if (!a_dst.buf->closed) { uint64_t len = a_dst.buf->len - a_dst.buf->wi; - b_wend_dst += len; - if (a_dst.private_impl.limit && (b_wend_dst > a_dst.private_impl.limit)) { - b_wend_dst = a_dst.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_dst.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_wend_dst += len; } } uint8_t* b_rptr_src = NULL; @@ -2296,10 +2362,13 @@ puffs_flate__status puffs_flate__zlib_decoder__decode( if (a_src.buf) { b_rptr_src = a_src.buf->ptr + a_src.buf->ri; uint64_t len = a_src.buf->wi - a_src.buf->ri; - b_rend_src = b_rptr_src + len; - if (a_src.private_impl.limit && (b_rend_src > a_src.private_impl.limit)) { - b_rend_src = a_src.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_rend_src = b_rptr_src + len; } uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point; @@ -2360,10 +2429,22 @@ puffs_flate__status puffs_flate__zlib_decoder__decode( if (a_dst.buf) { size_t n = b_wptr_dst - (a_dst.buf->ptr + a_dst.buf->wi); a_dst.buf->wi += n; + puffs_base__limit1* lim; + for (lim = &a_dst.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } } if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } } puffs_flate__status t_2 = puffs_flate__flate_decoder__decode( &self->private_impl.f_flate, a_dst, a_src); @@ -2436,11 +2517,23 @@ exit: if (a_dst.buf) { size_t n = b_wptr_dst - (a_dst.buf->ptr + a_dst.buf->wi); a_dst.buf->wi += n; + puffs_base__limit1* lim; + for (lim = &a_dst.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_wend_dst); } if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src); } @@ -2448,7 +2541,7 @@ exit: return status; short_read_src: - if (a_src.buf && a_src.buf->closed && !a_src.private_impl.use_limit) { + if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) { status = PUFFS_FLATE__ERROR_UNEXPECTED_EOF; goto exit; } diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index d6653a71..5322bcee 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -79,6 +79,17 @@ typedef struct { bool closed; // No further writes are expected. } puffs_base__buf1; +// 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; + #ifdef PUFFS_USE_NO_OP_PERFORMANCE_HACKS typedef struct { void* always_null0; @@ -94,12 +105,8 @@ typedef struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. struct { - uint8_t* limit; + puffs_base__limit1 limit; uint8_t* mark; - // use_limit is redundant, in that it always equals (limit != NULL), but - // having a separate bool can have a significant performance effect with - // gcc 4.8.4 (e.g. 1.1x on some benchmarks). - bool use_limit; #ifdef PUFFS_USE_NO_OP_PERFORMANCE_HACKS struct { puffs_base__paired_nulls* noph0; @@ -117,12 +124,8 @@ typedef struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. struct { - uint8_t* limit; + puffs_base__limit1 limit; uint8_t* mark; - // use_limit is redundant, in that it always equals (limit != NULL), but - // having a separate bool can have a significant performance effect with - // gcc 4.8.4 (e.g. 1.1x on some benchmarks). - bool use_limit; } private_impl; } puffs_base__writer1; @@ -579,31 +582,21 @@ static inline uint32_t puffs_base__writer1__copy_from_slice32( // public (in base-header.h). We assume that, at the boundary between user code // and Puffs code, the reader1 and writer1's private_impl fields (including // limit and mark) are NULL. Otherwise, some internal assumptions break down. -// For example, the limit field is represented as a pointer, even though -// conceptually it is a count, but that pointer-to-count correspondence becomes -// invalid if a buffer is re-used (e.g. on resuming a coroutine). +// For example, limits could be represented as pointers, even though +// conceptually they are counts, but that pointer-to-count correspondence +// becomes invalid if a buffer is re-used (e.g. on resuming a coroutine). // // Admittedly, some of the Puffs test code calls these methods, but that test // code is still Puffs code, not user code. Other Puffs test code modifies // private_impl fields directly. -static inline puffs_base__empty_struct puffs_base__reader1__limit( +static inline puffs_base__reader1 puffs_base__reader1__limit( puffs_base__reader1* o, - uint8_t* ptr, - uint64_t limit) { - if (!o->buf) { - o->private_impl.limit = NULL; - } else { - uint64_t len = o->buf->wi - o->buf->ri; - if (limit > len) { - limit = len; - } - if (!o->private_impl.limit || (limit < (o->private_impl.limit - ptr))) { - o->private_impl.limit = ptr + limit; - } - } - o->private_impl.use_limit = true; - return ((puffs_base__empty_struct){}); + uint64_t* ptr_to_len) { + puffs_base__reader1 ret = *o; + ret.private_impl.limit.ptr_to_len = ptr_to_len; + ret.private_impl.limit.next = &o->private_impl.limit; + return ret; } static inline puffs_base__empty_struct puffs_base__reader1__mark( @@ -613,24 +606,7 @@ static inline puffs_base__empty_struct puffs_base__reader1__mark( return ((puffs_base__empty_struct){}); } -static inline puffs_base__empty_struct puffs_base__writer1__limit( - puffs_base__writer1* o, - uint8_t* ptr, - uint64_t limit) { - if (!o->buf) { - o->private_impl.limit = NULL; - } else { - uint64_t len = o->buf->len - o->buf->wi; - if (limit > len) { - limit = len; - } - if (!o->private_impl.limit || (limit < (o->private_impl.limit - ptr))) { - o->private_impl.limit = ptr + limit; - } - } - o->private_impl.use_limit = true; - return ((puffs_base__empty_struct){}); -} +// TODO: static inline puffs_base__writer1 puffs_base__writer1__limit() static inline puffs_base__empty_struct puffs_base__writer1__mark( puffs_base__writer1* o, @@ -767,10 +743,13 @@ puffs_gif__status puffs_gif__decoder__decode(puffs_gif__decoder* self, if (a_src.buf) { b_rptr_src = a_src.buf->ptr + a_src.buf->ri; uint64_t len = a_src.buf->wi - a_src.buf->ri; - b_rend_src = b_rptr_src + len; - if (a_src.private_impl.limit && (b_rend_src > a_src.private_impl.limit)) { - b_rend_src = a_src.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_rend_src = b_rptr_src + len; } uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point; @@ -784,6 +763,12 @@ puffs_gif__status puffs_gif__decoder__decode(puffs_gif__decoder* self, if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } } status = puffs_gif__decoder__decode_header(self, a_src); if (a_src.buf) { @@ -796,6 +781,12 @@ puffs_gif__status puffs_gif__decoder__decode(puffs_gif__decoder* self, if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } } status = puffs_gif__decoder__decode_lsd(self, a_src); if (a_src.buf) { @@ -816,6 +807,12 @@ puffs_gif__status puffs_gif__decoder__decode(puffs_gif__decoder* self, if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } } status = puffs_gif__decoder__decode_extension(self, a_src); if (a_src.buf) { @@ -829,6 +826,12 @@ puffs_gif__status puffs_gif__decoder__decode(puffs_gif__decoder* self, if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } } status = puffs_gif__decoder__decode_id(self, a_dst, a_src); if (a_src.buf) { @@ -861,6 +864,12 @@ exit: if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src); } @@ -868,7 +877,7 @@ exit: return status; short_read_src: - if (a_src.buf && a_src.buf->closed && !a_src.private_impl.use_limit) { + if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) { status = PUFFS_GIF__ERROR_UNEXPECTED_EOF; goto exit; } @@ -888,10 +897,13 @@ puffs_gif__status puffs_gif__decoder__decode_header(puffs_gif__decoder* self, if (a_src.buf) { b_rptr_src = a_src.buf->ptr + a_src.buf->ri; uint64_t len = a_src.buf->wi - a_src.buf->ri; - b_rend_src = b_rptr_src + len; - if (a_src.private_impl.limit && (b_rend_src > a_src.private_impl.limit)) { - b_rend_src = a_src.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_rend_src = b_rptr_src + len; } uint32_t coro_susp_point = @@ -936,13 +948,19 @@ exit: if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src); } return status; short_read_src: - if (a_src.buf && a_src.buf->closed && !a_src.private_impl.use_limit) { + if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) { status = PUFFS_GIF__ERROR_UNEXPECTED_EOF; goto exit; } @@ -963,10 +981,13 @@ puffs_gif__status puffs_gif__decoder__decode_lsd(puffs_gif__decoder* self, if (a_src.buf) { b_rptr_src = a_src.buf->ptr + a_src.buf->ri; uint64_t len = a_src.buf->wi - a_src.buf->ri; - b_rend_src = b_rptr_src + len; - if (a_src.private_impl.limit && (b_rend_src > a_src.private_impl.limit)) { - b_rend_src = a_src.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_rend_src = b_rptr_src + len; } uint32_t coro_susp_point = self->private_impl.c_decode_lsd[0].coro_susp_point; @@ -1037,13 +1058,19 @@ exit: if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src); } return status; short_read_src: - if (a_src.buf && a_src.buf->closed && !a_src.private_impl.use_limit) { + if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) { status = PUFFS_GIF__ERROR_UNEXPECTED_EOF; goto exit; } @@ -1064,10 +1091,13 @@ puffs_gif__status puffs_gif__decoder__decode_extension( if (a_src.buf) { b_rptr_src = a_src.buf->ptr + a_src.buf->ri; uint64_t len = a_src.buf->wi - a_src.buf->ri; - b_rend_src = b_rptr_src + len; - if (a_src.private_impl.limit && (b_rend_src > a_src.private_impl.limit)) { - b_rend_src = a_src.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_rend_src = b_rptr_src + len; } uint32_t coro_susp_point = @@ -1134,13 +1164,19 @@ exit: if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src); } return status; short_read_src: - if (a_src.buf && a_src.buf->closed && !a_src.private_impl.use_limit) { + if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) { status = PUFFS_GIF__ERROR_UNEXPECTED_EOF; goto exit; } @@ -1166,10 +1202,13 @@ puffs_gif__status puffs_gif__decoder__decode_id(puffs_gif__decoder* self, if (a_src.buf) { b_rptr_src = a_src.buf->ptr + a_src.buf->ri; uint64_t len = a_src.buf->wi - a_src.buf->ri; - b_rend_src = b_rptr_src + len; - if (a_src.private_impl.limit && (b_rend_src > a_src.private_impl.limit)) { - b_rend_src = a_src.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_rend_src = b_rptr_src + len; } uint32_t coro_susp_point = self->private_impl.c_decode_id[0].coro_susp_point; @@ -1228,14 +1267,21 @@ puffs_gif__status puffs_gif__decoder__decode_id(puffs_gif__decoder* self, while (true) { v_r = a_src; puffs_base__reader1__mark(&v_r, b_rptr_src); - puffs_base__reader1__limit(&v_r, b_rptr_src, v_block_size); PUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } } + uint64_t l_rlimit0 = v_block_size; puffs_gif__status t_3 = puffs_gif__lzw_decoder__decode( - &self->private_impl.f_lzw, a_dst, v_r); + &self->private_impl.f_lzw, a_dst, + puffs_base__reader1__limit(&v_r, &l_rlimit0)); if (a_src.buf) { b_rptr_src = a_src.buf->ptr + a_src.buf->ri; } @@ -1292,13 +1338,19 @@ exit: if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src); } return status; short_read_src: - if (a_src.buf && a_src.buf->closed && !a_src.private_impl.use_limit) { + if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) { status = PUFFS_GIF__ERROR_UNEXPECTED_EOF; goto exit; } @@ -1359,10 +1411,13 @@ puffs_gif__status puffs_gif__lzw_decoder__decode(puffs_gif__lzw_decoder* self, b_wend_dst = b_wptr_dst; if (!a_dst.buf->closed) { uint64_t len = a_dst.buf->len - a_dst.buf->wi; - b_wend_dst += len; - if (a_dst.private_impl.limit && (b_wend_dst > a_dst.private_impl.limit)) { - b_wend_dst = a_dst.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_dst.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_wend_dst += len; } } uint8_t* b_rptr_src = NULL; @@ -1370,10 +1425,13 @@ puffs_gif__status puffs_gif__lzw_decoder__decode(puffs_gif__lzw_decoder* self, if (a_src.buf) { b_rptr_src = a_src.buf->ptr + a_src.buf->ri; uint64_t len = a_src.buf->wi - a_src.buf->ri; - b_rend_src = b_rptr_src + len; - if (a_src.private_impl.limit && (b_rend_src > a_src.private_impl.limit)) { - b_rend_src = a_src.private_impl.limit; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } } + b_rend_src = b_rptr_src + len; } uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point; @@ -1513,11 +1571,23 @@ exit: if (a_dst.buf) { size_t n = b_wptr_dst - (a_dst.buf->ptr + a_dst.buf->wi); a_dst.buf->wi += n; + puffs_base__limit1* lim; + for (lim = &a_dst.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_wend_dst); } if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; + puffs_base__limit1* lim; + for (lim = &a_src.private_impl.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src); } @@ -1525,7 +1595,7 @@ exit: return status; short_read_src: - if (a_src.buf && a_src.buf->closed && !a_src.private_impl.use_limit) { + if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) { status = PUFFS_GIF__ERROR_UNEXPECTED_EOF; goto exit; } diff --git a/gen/h/std/flate.h b/gen/h/std/flate.h index 5a35a47d..3ef55b79 100644 --- a/gen/h/std/flate.h +++ b/gen/h/std/flate.h @@ -79,6 +79,17 @@ typedef struct { bool closed; // No further writes are expected. } puffs_base__buf1; +// 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; + #ifdef PUFFS_USE_NO_OP_PERFORMANCE_HACKS typedef struct { void* always_null0; @@ -94,12 +105,8 @@ typedef struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. struct { - uint8_t* limit; + puffs_base__limit1 limit; uint8_t* mark; - // use_limit is redundant, in that it always equals (limit != NULL), but - // having a separate bool can have a significant performance effect with - // gcc 4.8.4 (e.g. 1.1x on some benchmarks). - bool use_limit; #ifdef PUFFS_USE_NO_OP_PERFORMANCE_HACKS struct { puffs_base__paired_nulls* noph0; @@ -117,12 +124,8 @@ typedef struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. struct { - uint8_t* limit; + puffs_base__limit1 limit; uint8_t* mark; - // use_limit is redundant, in that it always equals (limit != NULL), but - // having a separate bool can have a significant performance effect with - // gcc 4.8.4 (e.g. 1.1x on some benchmarks). - bool use_limit; } private_impl; } puffs_base__writer1; diff --git a/gen/h/std/gif.h b/gen/h/std/gif.h index 18e9e839..4c321215 100644 --- a/gen/h/std/gif.h +++ b/gen/h/std/gif.h @@ -79,6 +79,17 @@ typedef struct { bool closed; // No further writes are expected. } puffs_base__buf1; +// 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; + #ifdef PUFFS_USE_NO_OP_PERFORMANCE_HACKS typedef struct { void* always_null0; @@ -94,12 +105,8 @@ typedef struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. struct { - uint8_t* limit; + puffs_base__limit1 limit; uint8_t* mark; - // use_limit is redundant, in that it always equals (limit != NULL), but - // having a separate bool can have a significant performance effect with - // gcc 4.8.4 (e.g. 1.1x on some benchmarks). - bool use_limit; #ifdef PUFFS_USE_NO_OP_PERFORMANCE_HACKS struct { puffs_base__paired_nulls* noph0; @@ -117,12 +124,8 @@ typedef struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. struct { - uint8_t* limit; + puffs_base__limit1 limit; uint8_t* mark; - // use_limit is redundant, in that it always equals (limit != NULL), but - // having a separate bool can have a significant performance effect with - // gcc 4.8.4 (e.g. 1.1x on some benchmarks). - bool use_limit; } private_impl; } puffs_base__writer1; diff --git a/std/gif/decode_gif.puffs b/std/gif/decode_gif.puffs index 058441b0..25d2250f 100644 --- a/std/gif/decode_gif.puffs +++ b/std/gif/decode_gif.puffs @@ -163,12 +163,15 @@ pri func decoder.decode_id?(dst writer1, src reader1)() { } while true { var r reader1 = in.src + // TODO: should "mark" be "set_mark"? Unlike "limit", "mark" does + // not return a different reader1. r.mark() - r.limit(l:block_size) // TODO: remove the dummy param. It's needed for now so that // writeSaveExprDerivedVars updates e.g. the b_rptr_src derived // variables. - var z status = try this.lzw.decode?(dst:in.dst, src:r, dummy:in.src) + // + // TODO: enforce that limit can only be called in a "foo?" call? + var z status = try this.lzw.decode?(dst:in.dst, src:r.limit(l:block_size), dummy:in.src) if z.is_ok() { break } diff --git a/test/c/std/flate.c b/test/c/std/flate.c index fcadde80..b8c03732 100644 --- a/test/c/std/flate.c +++ b/test/c/std/flate.c @@ -189,14 +189,18 @@ const char* puffs_flate_decode(puffs_base__buf1* dst, uint64_t rlimit) { puffs_flate__flate_decoder dec; puffs_flate__flate_decoder__initialize(&dec, PUFFS_VERSION, 0); + uint64_t wlim = 0; + uint64_t rlim = 0; while (true) { puffs_base__writer1 dst_writer = {.buf = dst}; if (wlimit) { - puffs_base__writer1__limit(&dst_writer, dst->ptr + dst->wi, wlimit); + wlim = wlimit; + dst_writer.private_impl.limit.ptr_to_len = &wlim; } puffs_base__reader1 src_reader = {.buf = src}; if (rlimit) { - puffs_base__reader1__limit(&src_reader, src->ptr + src->ri, rlimit); + rlim = rlimit; + src_reader.private_impl.limit.ptr_to_len = &rlim; } puffs_flate__status s = @@ -220,14 +224,18 @@ const char* puffs_zlib_decode(puffs_base__buf1* dst, puffs_flate__zlib_decoder dec; puffs_flate__zlib_decoder__initialize(&dec, PUFFS_VERSION, 0); + uint64_t wlim = 0; + uint64_t rlim = 0; while (true) { puffs_base__writer1 dst_writer = {.buf = dst}; if (wlimit) { - puffs_base__writer1__limit(&dst_writer, dst->ptr + dst->wi, wlimit); + wlim = wlimit; + dst_writer.private_impl.limit.ptr_to_len = &wlim; } puffs_base__reader1 src_reader = {.buf = src}; if (rlimit) { - puffs_base__reader1__limit(&src_reader, src->ptr + src->ri, rlimit); + rlim = rlimit; + src_reader.private_impl.limit.ptr_to_len = &rlim; } puffs_flate__status s = @@ -373,7 +381,7 @@ bool do_test_puffs_flate_history(int i, dec->private_impl.f_history_index = starting_history_index; - puffs_base__writer1__limit(&dst_writer, got->ptr + got->wi, limit); + dst_writer.private_impl.limit.ptr_to_len = &limit; puffs_flate__status got_s = puffs_flate__flate_decoder__decode(dec, dst_writer, src_reader); diff --git a/test/c/std/gif.c b/test/c/std/gif.c index 6f886385..4aa88884 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -230,15 +230,19 @@ bool do_test_puffs_gif_lzw_decode(const char* src_filename, puffs_gif__lzw_decoder__initialize(&dec, PUFFS_VERSION, 0); puffs_gif__lzw_decoder__set_literal_width(&dec, literal_width); int num_iters = 0; + uint64_t wlim = 0; + uint64_t rlim = 0; while (true) { num_iters++; puffs_base__writer1 got_writer = {.buf = &got}; if (wlimit) { - puffs_base__writer1__limit(&got_writer, got.ptr + got.wi, wlimit); + wlim = wlimit; + got_writer.private_impl.limit.ptr_to_len = &wlim; } puffs_base__reader1 src_reader = {.buf = &src}; if (rlimit) { - puffs_base__reader1__limit(&src_reader, src.ptr + src.ri, rlimit); + rlim = rlimit; + src_reader.private_impl.limit.ptr_to_len = &rlim; } size_t old_wi = got.wi; size_t old_ri = src.ri; @@ -409,15 +413,19 @@ bool do_test_puffs_gif_decode(const char* filename, puffs_gif__decoder dec; puffs_gif__decoder__initialize(&dec, PUFFS_VERSION, 0); int num_iters = 0; + uint64_t wlim = 0; + uint64_t rlim = 0; while (true) { num_iters++; puffs_base__writer1 got_writer = {.buf = &got}; if (wlimit) { - puffs_base__writer1__limit(&got_writer, got.ptr + got.wi, wlimit); + wlim = wlimit; + got_writer.private_impl.limit.ptr_to_len = &wlim; } puffs_base__reader1 src_reader = {.buf = &src}; if (rlimit) { - puffs_base__reader1__limit(&src_reader, src.ptr + src.ri, rlimit); + rlim = rlimit; + src_reader.private_impl.limit.ptr_to_len = &rlim; } size_t old_wi = got.wi; size_t old_ri = src.ri;