From eaec2bf0a1ef54d1b20a79bd8772a162b155529a Mon Sep 17 00:00:00 2001 From: Luigi Scarso <luigi.scarso@gmail.com> Date: Sat, 15 Mar 2025 22:24:19 +0100 Subject: [PATCH] Added missed files. --- .../harfbuzz/harfbuzz-src/src/hb-decycler.hh | 164 ++++++++++++++++++ .../harfbuzz-src/src/test-decycler.cc | 116 +++++++++++++ .../libpng-src/contrib/conftest/fixed.dfa | 15 ++ .../contrib/conftest/float-fixed.dfa | 14 ++ .../contrib/conftest/nocompile-limits.dfa | 21 +++ .../libpng-src/contrib/conftest/nolimits.dfa | 19 ++ 6 files changed, 349 insertions(+) create mode 100644 source/libs/harfbuzz/harfbuzz-src/src/hb-decycler.hh create mode 100644 source/libs/harfbuzz/harfbuzz-src/src/test-decycler.cc create mode 100644 source/libs/libpng/libpng-src/contrib/conftest/fixed.dfa create mode 100644 source/libs/libpng/libpng-src/contrib/conftest/float-fixed.dfa create mode 100644 source/libs/libpng/libpng-src/contrib/conftest/nocompile-limits.dfa create mode 100644 source/libs/libpng/libpng-src/contrib/conftest/nolimits.dfa diff --git a/source/libs/harfbuzz/harfbuzz-src/src/hb-decycler.hh b/source/libs/harfbuzz/harfbuzz-src/src/hb-decycler.hh new file mode 100644 index 000000000..95b961027 --- /dev/null +++ b/source/libs/harfbuzz/harfbuzz-src/src/hb-decycler.hh @@ -0,0 +1,164 @@ +/* + * Copyright © 2025 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#ifndef HB_DECYCLER_HH +#define HB_DECYCLER_HH + +#include "hb.hh" + +/* + * hb_decycler_t is an efficient cycle detector for graph traversal. + * It's a simple tortoise-and-hare algorithm with a twist: it's + * designed to detect cycles while traversing a graph in a DFS manner, + * instead of just a linked list. + * + * For Floyd's tortoise and hare algorithm, see: + * https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare + * + * hb_decycler_t is O(n) in the number of nodes in the DFS traversal + * if there are no cycles. Unlike Floyd's algorithm, hb_decycler_t + * can be used in a DFS traversal, where the graph is not a simple + * linked list, but a tree with possible cycles. Like Floyd's algorithm, + * it is constant-memory (~three pointers). + * + * The decycler works by creating an implicit linked-list on the stack, + * of the path from the root to the current node, and apply Floyd's + * algorithm on that list as it goes. + * + * The decycler is malloc-free, and as such, much faster to use than a + * hb_set_t or hb_map_t equivalent. + * + * The decycler detects cycles in the graph *eventually*, not *immediately*. + * That is, it may not detect a cycle until the cycle is fully traversed, + * even multiple times. See Floyd's algorithm analysis for details. + * + * The implementation saves a pointer storage on the stack by combining + * this->u.decycler and this->u.next into a union. This is possible because + * at any point we only need one of those values. The invariant is that + * after construction, and before destruction, of a node, the u.decycler + * field is always valid. The u.next field is only valid when the node is + * in the traversal path, parent to another node. + * + * There are three method's: + * + * - hb_decycler_node_t() constructor: Creates a new node in the traversal. + * The constructor takes a reference to the decycler object and inserts + * itself as the latest node in the traversal path, by advancing the hare + * pointer, and for every other descent, advancing the tortoise pointer. + * + * - ~hb_decycler_node_t() destructor: Restores the decycler object to its + * previous state by removing the node from the traversal path. + * + * - bool visit(uintptr_t value): Called on every node in the graph. Returns + * true if the node is not part of a cycle, and false if it is. The value + * parameter is used to detect cycles. It's the caller's responsibility + * to ensure that the value is unique for each node in the graph. + * The cycle detection is as simple as comparing the value to the value + * held by the tortoise pointer, which is the Floyd's algorithm. + * + * For usage examples see test-decycler.cc. + */ + +struct hb_decycler_node_t; + +struct hb_decycler_t +{ + friend struct hb_decycler_node_t; + + private: + bool tortoise_awake = false; + hb_decycler_node_t *tortoise = nullptr; + hb_decycler_node_t *hare = nullptr; +}; + +struct hb_decycler_node_t +{ + hb_decycler_node_t (hb_decycler_t &decycler) + { + u.decycler = &decycler; + + decycler.tortoise_awake = !decycler.tortoise_awake; + + if (!decycler.tortoise) + { + // First node. + assert (decycler.tortoise_awake); + assert (!decycler.hare); + decycler.tortoise = decycler.hare = this; + return; + } + + if (decycler.tortoise_awake) + decycler.tortoise = decycler.tortoise->u.next; // Time to move. + + this->prev = decycler.hare; + decycler.hare->u.next = this; + decycler.hare = this; + } + + ~hb_decycler_node_t () + { + hb_decycler_t &decycler = *u.decycler; + + // Inverse of the constructor. + + assert (decycler.hare == this); + decycler.hare = prev; + if (prev) + prev->u.decycler = &decycler; + + assert (decycler.tortoise); + if (decycler.tortoise_awake) + decycler.tortoise = decycler.tortoise->prev; + + decycler.tortoise_awake = !decycler.tortoise_awake; + } + + bool visit (uintptr_t value_) + { + value = value_; + + hb_decycler_t &decycler = *u.decycler; + + if (decycler.tortoise == this) + return true; // First node; not a cycle. + + if (decycler.tortoise->value == value) + return false; // Cycle detected. + + return true; + } + + private: + union { + hb_decycler_t *decycler; + hb_decycler_node_t *next; + } u = {nullptr}; + hb_decycler_node_t *prev = nullptr; + uintptr_t value = 0; +}; + +#endif /* HB_DECYCLER_HH */ diff --git a/source/libs/harfbuzz/harfbuzz-src/src/test-decycler.cc b/source/libs/harfbuzz/harfbuzz-src/src/test-decycler.cc new file mode 100644 index 000000000..dbe0f24f2 --- /dev/null +++ b/source/libs/harfbuzz/harfbuzz-src/src/test-decycler.cc @@ -0,0 +1,116 @@ +/* + * Copyright © 2025 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb.hh" +#include "hb-decycler.hh" + +static void +tree_recurse_binary (unsigned value, + unsigned max_value, + hb_decycler_t &decycler) +{ + if (value >= max_value) + return; + + hb_decycler_node_t node (decycler); + + bool ret = node.visit (value); + assert (ret); + + tree_recurse_binary (value * 2 + 1, max_value, decycler); + tree_recurse_binary (value * 2 + 2, max_value, decycler); +} + +static void +tree_recurse_tertiary (unsigned value, + unsigned max_value, + hb_decycler_t &decycler) +{ + /* This function implements an alternative way to use the + * decycler. It checks for each node before visiting it. + * It demonstrates reusing a node for multiple visits. */ + + if (value >= max_value) + return; + + hb_decycler_node_t node (decycler); + + value *= 3; + + for (unsigned i = 1; i <= 3; i++) + { + bool ret = node.visit (value + i); + assert (ret); + + tree_recurse_tertiary (value + i, max_value, decycler); + } +} + +static void +test_tree () +{ + hb_decycler_t decycler; + tree_recurse_binary (0, 64, decycler); + tree_recurse_tertiary (0, 1000, decycler); +} + +static void +cycle_recurse (signed value, + signed cycle_length, + hb_decycler_t &decycler) +{ + assert (cycle_length > 0); + + hb_decycler_node_t node (decycler); + + if (!node.visit (value)) + return; + + if (value >= cycle_length) + value = value % cycle_length; + + cycle_recurse (value + 1, cycle_length, decycler); +} + +static void +test_cycle () +{ + hb_decycler_t decycler; + cycle_recurse (0, 1, decycler); + cycle_recurse (0, 2, decycler); + cycle_recurse (2, 3, decycler); + cycle_recurse (-20, 8, decycler); +} + +int +main (int argc, char **argv) +{ + test_tree (); + test_cycle (); + + return 0; +} + diff --git a/source/libs/libpng/libpng-src/contrib/conftest/fixed.dfa b/source/libs/libpng/libpng-src/contrib/conftest/fixed.dfa new file mode 100644 index 000000000..cb45f0136 --- /dev/null +++ b/source/libs/libpng/libpng-src/contrib/conftest/fixed.dfa @@ -0,0 +1,15 @@ +# fixed.dfa +# Build time configuration of libpng +# +# Author: John Bowler +# Copyright: (c) John Bowler, 2025 +# Usage rights: +# To the extent possible under law, the author has waived all copyright and +# related or neighboring rights to this work. This work is published from: +# United States. +# +# Test the standard libpng configuration without floating point (the internal +# fixed point implementations are used instead). +# +option FLOATING_ARITHMETIC off +option FLOATING_POINT off diff --git a/source/libs/libpng/libpng-src/contrib/conftest/float-fixed.dfa b/source/libs/libpng/libpng-src/contrib/conftest/float-fixed.dfa new file mode 100644 index 000000000..c13da3198 --- /dev/null +++ b/source/libs/libpng/libpng-src/contrib/conftest/float-fixed.dfa @@ -0,0 +1,14 @@ +# fixed-float.dfa +# Build time configuration of libpng +# +# Author: John Bowler +# Copyright: (c) John Bowler, 2025 +# Usage rights: +# To the extent possible under law, the author has waived all copyright and +# related or neighboring rights to this work. This work is published from: +# United States. +# +# Test the standard libpng configuration with the fixed point internal +# implementation in place of the default floating point +# +option FLOATING_ARITHMETIC off diff --git a/source/libs/libpng/libpng-src/contrib/conftest/nocompile-limits.dfa b/source/libs/libpng/libpng-src/contrib/conftest/nocompile-limits.dfa new file mode 100644 index 000000000..24e1e2e08 --- /dev/null +++ b/source/libs/libpng/libpng-src/contrib/conftest/nocompile-limits.dfa @@ -0,0 +1,21 @@ +# nolimits.dfa +# Build time configuration of libpng +# +# Author: John Bowler +# Copyright: (c) John Bowler, 2025 +# +# Usage rights: +# To the extent possible under law, the author has waived all copyright and +# related or neighboring rights to this work. This work is published from: +# United States. +# +# Build libpng without any limits and without run-time settable limits. Turning +# USER_LIMITS off reduces libpng code size by allowing compile-time elimination +# of some checking code. +# +option USER_LIMITS off + +@# define PNG_USER_WIDTH_MAX PNG_UINT_31_MAX +@# define PNG_USER_HEIGHT_MAX PNG_UINT_31_MAX +@# define PNG_USER_CHUNK_CACHE_MAX 0 +@# define PNG_USER_CHUNK_MALLOC_MAX 0 diff --git a/source/libs/libpng/libpng-src/contrib/conftest/nolimits.dfa b/source/libs/libpng/libpng-src/contrib/conftest/nolimits.dfa new file mode 100644 index 000000000..5b37fcf72 --- /dev/null +++ b/source/libs/libpng/libpng-src/contrib/conftest/nolimits.dfa @@ -0,0 +1,19 @@ +# nolimits.dfa +# Build time configuration of libpng +# +# Author: John Bowler +# Copyright: (c) John Bowler, 2025 +# +# Usage rights: +# To the extent possible under law, the author has waived all copyright and +# related or neighboring rights to this work. This work is published from: +# United States. +# +# Build libpng without any limits. With these settigs run-time limits are still +# possible. +# +@# define PNG_USER_WIDTH_MAX PNG_UINT_31_MAX +@# define PNG_USER_HEIGHT_MAX PNG_UINT_31_MAX +@# define PNG_USER_CHUNK_CACHE_MAX 0 +@# define PNG_USER_CHUNK_MALLOC_MAX 0 + -- GitLab