mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Have copy_from return the number of bytes copied.
This commit is contained in:
1 parent
a9bdeee7c1
commit
f1643be221
9 files changed
+38
-28
No files matched your search
@@ -119,12 +119,13 @@ static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(
|
||||
//
|
||||
// Passing a puffs_base_slice_u8 with all fields NULL or zero (a valid, empty
|
||||
// slice) is valid and results in a no-op.
|
||||
static inline void puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,
|
||||
puffs_base_slice_u8 src) {
|
||||
static inline uint64_t puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,
|
||||
puffs_base_slice_u8 src) {
|
||||
size_t n = dst.len < src.len ? dst.len : src.len;
|
||||
if (n > 0) {
|
||||
memmove(dst.ptr, src.ptr, n);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
#endif // PUFFS_BASE_IMPL_H
|
||||
@@ -15,8 +15,8 @@ const baseImpl = "" +
|
||||
"#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_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#define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case n:\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define PUFFS_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n// Declare the printf prototype. The generated code shouldn't need this at all,\n// but it's useful for manual printf debugging.\nextern int printf(const char* __restrict __format, ...);\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#de" +
|
||||
"fine PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n// ---------------- Static Inline Functions\n//\n// The helpers below are functions, instead of macros, because their arguments\n// can be an expression that we shouldn't evaluate more than once.\n//\n// They are in base-impl.h and hence copy/pasted into every generated C file,\n// instead of being in some \"base.c\" file, since a design goal is that users of\n// the generated C code can often just #include a single .c file, such as\n// \"gif.c\", without having to additionally include or otherwise build and link\n// a \"base.c\" file.\n//\n// They are static, so that linking multiple puffs .o files won't complain about\n// duplicate function definitions.\n//\n// They are explicitly marked inline, even if modern compilers don't use the\n// inline attribute to guide optimizations such as inlining, to avoid the\n// -Wunused-function warning, and we like to compile with -Wall -Werror.\n\nstatic inline uint16_t puffs_base_load_u16be(uint8_t* p) {\n return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1" +
|
||||
"]) << 0);\n}\n\nstatic inline uint16_t puffs_base_load_u16le(uint8_t* p) {\n return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);\n}\n\nstatic inline uint32_t puffs_base_load_u32be(uint8_t* p) {\n return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) |\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0);\n}\n\nstatic inline uint32_t puffs_base_load_u32le(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);\n}\n\nstatic inline puffs_base_slice_u8 puffs_base_make_slice_u8_from_ints(\n uint8_t* start,\n uint64_t i,\n uint64_t j,\n uint64_t k) {\n if ((i <= j) && (j <= k) && (k <= SIZE_MAX)) {\n return ((puffs_base_slice_u8){\n .ptr = start + i,\n .len = j - i,\n });\n }\n return ((puffs_base_slice_u8){});\n}\n\nstatic inline puffs_base_slice_u8 puffs_base_make_slice_u8_from_ptrs(\n uint8_t* start,\n uint8_t* mark0,\n uint8_t* mark1,\n uint8_t* end) {\n if ((start <= mark0) && (mark0 <= mark1) && " +
|
||||
"(mark1 <= end)) {\n return ((puffs_base_slice_u8){\n .ptr = mark0,\n .len = mark1 - mark0,\n });\n }\n return ((puffs_base_slice_u8){});\n}\n\n// puffs_base_slice_u8_prefix returns up to the first n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.len = n;\n }\n return s;\n}\n\n// puffs_base_slice_u8_suffix returns up to the last n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.ptr += (uint64_t)(s.len) - n;\n s.len = n;\n }\n return s;\n}\n\n// puffs_base_slice_u8_copy_from calls memmove(dst.ptr, src.ptr, n) where n is\n// the minimum of dst.len and src.len.\n//\n// Passing a puffs_base_slice_u8 with all fields NULL or zero (a valid, empty\n// slice) is valid and results in a no-op.\nstatic inline void puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,\n " +
|
||||
" puffs_base_slice_u8 src) {\n size_t n = dst.len < src.len ? dst.len : src.len;\n if (n > 0) {\n memmove(dst.ptr, src.ptr, n);\n }\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" +
|
||||
"(mark1 <= end)) {\n return ((puffs_base_slice_u8){\n .ptr = mark0,\n .len = mark1 - mark0,\n });\n }\n return ((puffs_base_slice_u8){});\n}\n\n// puffs_base_slice_u8_prefix returns up to the first n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.len = n;\n }\n return s;\n}\n\n// puffs_base_slice_u8_suffix returns up to the last n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.ptr += (uint64_t)(s.len) - n;\n s.len = n;\n }\n return s;\n}\n\n// puffs_base_slice_u8_copy_from calls memmove(dst.ptr, src.ptr, n) where n is\n// the minimum of dst.len and src.len.\n//\n// Passing a puffs_base_slice_u8 with all fields NULL or zero (a valid, empty\n// slice) is valid and results in a no-op.\nstatic inline uint64_t puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,\n " +
|
||||
" puffs_base_slice_u8 src) {\n size_t n = dst.len < src.len ? dst.len : src.len;\n if (n > 0) {\n memmove(dst.ptr, src.ptr, n);\n }\n return n;\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" +
|
||||
""
|
||||
|
||||
type template_args_short_read struct {
|
||||
|
||||
@@ -169,6 +169,20 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
b.printf("%sdst.buf ? %swptr_dst : NULL)", aPrefix, bPrefix)
|
||||
return nil
|
||||
}
|
||||
if isThatMethod(g.tm, n, t.KeyCopyFrom, 1) {
|
||||
b.writes("puffs_base_slice_u8_copy_from(")
|
||||
receiver := n.LHS().Expr().LHS().Expr()
|
||||
if err := g.writeExpr(b, receiver, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writeb(',')
|
||||
a := n.Args()[0].Arg().Value()
|
||||
if err := g.writeExpr(b, a, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes(")\n")
|
||||
return nil
|
||||
}
|
||||
if isThatMethod(g.tm, n, t.KeyLength, 0) {
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb('(')
|
||||
|
||||
@@ -58,22 +58,11 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
|
||||
b.writes(");\n")
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.copy_from(etc)".
|
||||
if isThatMethod(g.tm, n, t.KeyCopyFrom, 1) {
|
||||
b.writes("puffs_base_slice_u8_copy_from(")
|
||||
receiver := n.LHS().Expr().LHS().Expr()
|
||||
if err := g.writeExpr(b, receiver, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writeb(',')
|
||||
a := n.Args()[0].Arg().Value()
|
||||
if err := g.writeExpr(b, a, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes(");\n")
|
||||
return nil
|
||||
if err := g.writeExpr(b, n, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("TODO: generate code for foo() when foo is not a ? call-suspendible")
|
||||
b.writes(";\n")
|
||||
return nil
|
||||
|
||||
case a.KIf:
|
||||
// TODO: for writeSuspendibles, make sure that we get order of
|
||||
|
||||
+8
-3
@@ -178,6 +178,7 @@ typedef struct {
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
puffs_flate_status v_z;
|
||||
uint64_t v_n;
|
||||
} c_decode[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
@@ -410,12 +411,13 @@ static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(
|
||||
//
|
||||
// Passing a puffs_base_slice_u8 with all fields NULL or zero (a valid, empty
|
||||
// slice) is valid and results in a no-op.
|
||||
static inline void puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,
|
||||
puffs_base_slice_u8 src) {
|
||||
static inline uint64_t puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,
|
||||
puffs_base_slice_u8 src) {
|
||||
size_t n = dst.len < src.len ? dst.len : src.len;
|
||||
if (n > 0) {
|
||||
memmove(dst.ptr, src.ptr, n);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
#endif // PUFFS_BASE_IMPL_H
|
||||
@@ -632,6 +634,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
|
||||
puffs_flate_status v_z;
|
||||
puffs_base_buf1_mark v_m1;
|
||||
puffs_base_slice_u8 v_written;
|
||||
uint64_t v_n;
|
||||
|
||||
uint8_t* b_wptr_dst = NULL;
|
||||
uint8_t* b_wend_dst = NULL;
|
||||
@@ -653,6 +656,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
|
||||
v_z = self->private_impl.c_decode[0].v_z;
|
||||
v_m1 = ((puffs_base_buf1_mark){});
|
||||
v_written = ((puffs_base_slice_u8){});
|
||||
v_n = self->private_impl.c_decode[0].v_n;
|
||||
}
|
||||
switch (coro_susp_point) {
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
||||
@@ -697,7 +701,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
|
||||
v_written);
|
||||
self->private_impl.f_history_index = 32768;
|
||||
} else {
|
||||
puffs_base_slice_u8_copy_from(
|
||||
v_n = puffs_base_slice_u8_copy_from(
|
||||
puffs_base_make_slice_u8_from_ints(
|
||||
self->private_impl.f_history,
|
||||
self->private_impl.f_history_index & 32767, 32768, 32768),
|
||||
@@ -714,6 +718,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
|
||||
suspend:
|
||||
self->private_impl.c_decode[0].coro_susp_point = coro_susp_point;
|
||||
self->private_impl.c_decode[0].v_z = v_z;
|
||||
self->private_impl.c_decode[0].v_n = v_n;
|
||||
|
||||
exit:
|
||||
if (a_dst.buf) {
|
||||
|
||||
+3
-2
@@ -382,12 +382,13 @@ static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(
|
||||
//
|
||||
// Passing a puffs_base_slice_u8 with all fields NULL or zero (a valid, empty
|
||||
// slice) is valid and results in a no-op.
|
||||
static inline void puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,
|
||||
puffs_base_slice_u8 src) {
|
||||
static inline uint64_t puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,
|
||||
puffs_base_slice_u8 src) {
|
||||
size_t n = dst.len < src.len ? dst.len : src.len;
|
||||
if (n > 0) {
|
||||
memmove(dst.ptr, src.ptr, n);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
#endif // PUFFS_BASE_IMPL_H
|
||||
|
||||
@@ -178,6 +178,7 @@ typedef struct {
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
puffs_flate_status v_z;
|
||||
uint64_t v_n;
|
||||
} c_decode[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
|
||||
+2
-3
@@ -436,8 +436,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.set_literal_width(etc)".
|
||||
if isThatMethod(q.tm, n, q.tm.ByName("set_literal_width").Key(), 1) ||
|
||||
isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 2) ||
|
||||
isThatMethod(q.tm, n, t.KeyCopyFrom, 1) {
|
||||
isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 2) {
|
||||
foo := n.LHS().Expr().LHS().Expr()
|
||||
if err := q.tcheckExpr(foo, depth); err != nil {
|
||||
return err
|
||||
@@ -453,7 +452,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.length(etc)".
|
||||
if isThatMethod(q.tm, n, t.KeyLength, 0) {
|
||||
if isThatMethod(q.tm, n, t.KeyCopyFrom, 1) || isThatMethod(q.tm, n, t.KeyLength, 0) {
|
||||
foo := n.LHS().Expr().LHS().Expr()
|
||||
if err := q.tcheckExpr(foo, depth); err != nil {
|
||||
return err
|
||||
|
||||
@@ -118,7 +118,7 @@ pub func decoder.decode?(dst writer1, src reader1)() {
|
||||
this.history_index = 32 * 1024
|
||||
} else {
|
||||
// TODO: fill part of the history.
|
||||
this.history[this.history_index & 0x7FFF:].copy_from(s:written)
|
||||
var n u64 = this.history[this.history_index & 0x7FFF:].copy_from(s:written)
|
||||
}
|
||||
}
|
||||
return z
|
||||
|
||||
Reference in new issue
Block a user