commit b6ccffe8099dfdfdaf29870924dd29e0d1cb27f5 Author: Greg Burd Date: Tue Apr 2 20:41:55 2024 -0400 import as C++ diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..09db5d1 --- /dev/null +++ b/.clang-format @@ -0,0 +1,197 @@ +# Basic .clang-format +--- +BasedOnStyle: WebKit +AlignAfterOpenBracket: DontAlign +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left +AlignOperands: false +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: InlineOnly +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterReturnType: TopLevelDefinitions +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: true +BinPackParameters: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: WebKit +BreakBeforeTernaryOperators: false +# TODO: BreakStringLiterals can cause very strange formatting so turn it off? +BreakStringLiterals: false +# Prefer: +# some_var = function(arg1, +# arg2) +# over: +# some_var = +# function(arg1, arg2) +PenaltyBreakAssignment: 100 +# Prefer: +# some_long_function(arg1, arg2 +# arg3) +# over: +# some_long_function( +# arg1, arg2, arg3) +PenaltyBreakBeforeFirstCallParameter: 100 +CompactNamespaces: true +DerivePointerAlignment: false +DisableFormat: false +ForEachMacros: + - ARB_ARRFOREACH + - ARB_ARRFOREACH_REVWCOND + - ARB_ARRFOREACH_REVERSE + - ARB_FOREACH + - ARB_FOREACH_FROM + - ARB_FOREACH_SAFE + - ARB_FOREACH_REVERSE + - ARB_FOREACH_REVERSE_FROM + - ARB_FOREACH_REVERSE_SAFE + - BIT_FOREACH_ISCLR + - BIT_FOREACH_ISSET + - CPU_FOREACH + - CPU_FOREACH_ISCLR + - CPU_FOREACH_ISSET + - FOREACH_THREAD_IN_PROC + - FOREACH_PROC_IN_SYSTEM + - FOREACH_PRISON_CHILD + - FOREACH_PRISON_DESCENDANT + - FOREACH_PRISON_DESCENDANT_LOCKED + - FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL + - MNT_VNODE_FOREACH_ALL + - MNT_VNODE_FOREACH_ACTIVE + - RB_FOREACH + - RB_FOREACH_FROM + - RB_FOREACH_SAFE + - RB_FOREACH_REVERSE + - RB_FOREACH_REVERSE_FROM + - RB_FOREACH_REVERSE_SAFE + - SLIST_FOREACH + - SLIST_FOREACH_FROM + - SLIST_FOREACH_FROM_SAFE + - SLIST_FOREACH_SAFE + - SLIST_FOREACH_PREVPTR + - SPLAY_FOREACH + - LIST_FOREACH + - LIST_FOREACH_FROM + - LIST_FOREACH_FROM_SAFE + - LIST_FOREACH_SAFE + - STAILQ_FOREACH + - STAILQ_FOREACH_FROM + - STAILQ_FOREACH_FROM_SAFE + - STAILQ_FOREACH_SAFE + - TAILQ_FOREACH + - TAILQ_FOREACH_FROM + - TAILQ_FOREACH_FROM_SAFE + - TAILQ_FOREACH_REVERSE + - TAILQ_FOREACH_REVERSE_FROM + - TAILQ_FOREACH_REVERSE_FROM_SAFE + - TAILQ_FOREACH_REVERSE_SAFE + - TAILQ_FOREACH_SAFE + - VM_MAP_ENTRY_FOREACH + - VM_PAGE_DUMP_FOREACH +IndentCaseLabels: false +IndentPPDirectives: None +Language: Cpp +NamespaceIndentation: None +PointerAlignment: Right +ContinuationIndentWidth: 4 +IndentWidth: 4 +TabWidth: 4 +ColumnLimit: 160 +UseTab: Never +SpaceAfterCStyleCast: false +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^\"opt_.*\.h\"' + Priority: 1 + SortPriority: 10 + - Regex: '^' + Priority: 2 + SortPriority: 20 + - Regex: '^' + Priority: 2 + SortPriority: 21 + - Regex: '^' + Priority: 2 + SortPriority: 22 + - Regex: '^' + Priority: 2 + SortPriority: 23 + - Regex: '^' + Priority: 3 + SortPriority: 30 + - Regex: '^ + bugprone-*, + clang-analyzer-*, + google-*, + misc-*, + modernize-*, + performance-*, + portability-*, + -bugprone-branch-clone, + -bugprone-easily-swappable-parameters, + -bugprone-macro-parentheses, + -bugprone-narrowing-conversions, + -bugprone-not-null-terminated-result, + -bugprone-reserved-identifier, + -bugprone-sizeof-expression, + -clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling, + -clang-analyzer-security.insecureAPI.strcpy, + -google-readability-todo, + -misc-unused-parameters, + -misc-no-recursion, + -performance-no-int-to-ptr, + -bugprone-assignment-in-if-condition, + +... diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..f59b4d9 --- /dev/null +++ b/.envrc @@ -0,0 +1,5 @@ +if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then + source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.4/direnvrc" "sha256-DzlYZ33mWF/Gs8DDeyjr8mnVmQGx7ASYqA5WlxwvBG4=" +fi +watch_file devShell.nix shell.nix flake.nix +use flake || use nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fc91bf8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,120 @@ +libsparesemap.a +libsparesemap.so +**/*.o +tests/test +examples/main +.cache +hints.txt +tmp/ +git.diff +.direnv +.vscode/.ropeproject +.vscode/ipch +.codelite/ +.cmaketools.json +*.tags +*.dll +build/ +cmake-build* +.cmake_dirty +Makefile +Testing/ +compile_commands.json +.tern* +*.iml +*.dat +*.fsm +*.db + +# Created by https://www.gitignore.io/api/jetbrains +# Edit at https://www.gitignore.io/?templates=jetbrains + +### JetBrains ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### JetBrains Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +.idea/**/sonarlint/ + +# SonarQube Plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator/ + +# End of https://www.gitignore.io/api/jetbrains diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9a2185c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Christoph Rupp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..4788f12 --- /dev/null +++ b/README.md @@ -0,0 +1,90 @@ + +`sparsemap` is a sparse, compressed bitmap. In best case, it can store 2048 +bits in just 8 bytes. In worst case, it stores the 2048 bits uncompressed and +requires additional 8 bytes of overhead. + +The "best" case happens when large consecutive sequences of the bits are +either set ("1") or not set ("0"). If your numbers are consecutive 64bit +integers then sparsemap can compress up to 16kb in just 8 bytes. + +## How does it work? + +On the lowest level, bits are stored in BitVectors (a uint32_t or uint64_t). + +Each BitVector has an additional descriptor (2 bits). All descriptors are +stored in a single Word which is prepended to the BitVectors. (The descriptor +Word and the BitVectors have the same size.) The descriptor of a BitVector +specifies whether the BitVector consists only of set bits ("1"), unset +bits ("0") or has a mixed payload. In the first and second case the +BitVector is not stored. + +An example shows a sequence of 4 x 16 bits (here, each BitVector and the +Descriptor word has 16 bits): + + Descriptor: + 00 00 00 00 11 00 11 10 + ^^ ^^ ^^ ^^-- BitVector #0 - #3 are "0000000000000000" + ^^-- BitVector #4 is "1111111111111111" + ^^-- BitVector #5 is "0000000000000000" + ^^-- BitVector #7 is "1111111111111111" + ^^-- BitVector #7 is "0110010101111001" + +Since the first 7 BitVectors are either all "1" or "0" they are not stored. +The actual memory sequence looks like this: + + 0000000011001110 0110010101111001 + +Instead of storing 8 Words (16 bytes), we only store 2 Words (2 bytes): one +for the Descriptor, one for last BitVector #7. + +Since such a construct (it's called a MiniMap) has a limited capacity, another +structure is created on top of it, a `Sparsemap`. The Sparsemap stores a list +of MiniMaps, and for each MiniMap it stores the absolute address. I.e. if +the user sets bit 0 and bit 10000, and the MiniMap capacity is 2048, the +Sparsemap creates two MiniMaps. The first starts at offset 0, the second starts +at offset 8192. + +# Usage instructions + +The file `main.cc` has example code. Here is a small excerpt: + + // we need a buffer to store the SparseMap + unsigned char buffer[1024]; + + sparsemap::SparseMap sm; + sm.create(buffer, sizeof(buffer)); + + // after initialization, the used size is just 4 bytes (sizeof(uint32_t)) + assert(sm.get_size() == sizeof(uint32_t)); + + // set the first bit + sm.set(0, true); + + // check that the first bit was set + assert(sm.is_set(0) == true); + + // unset the first bit + assert(sm.is_set(1) == false); + + // check that the first bit is now no longer set + sm.set(0, false); + +## Final words + +This bitmap implementation has very efficient compression when using on long +sequences of set (or unset) bits. I.e. with a word size of 64bit, and a +payload of consecutive numbers without gaps, the payload of 2048 x +sizeof(uint64_t) = 16kb can be stored in just 8 bytes! + +However, if the sequence is not consecutive and has gaps, it's possible that +the compression is completely inefficient, and the size basically is identical +to an uncompressed bitvector (even higher because a few bytes are required for +metadata). In such cases, other compression schemes are more efficient (i.e. +http://lemire.me/blog/archives/2008/08/20/the-mythical-bitmap-index/). + +This library was originally created for hamsterdb [http://hamsterdb.com] in +order to compress the key indices. For several technical reasons this turned +out to be impossible, though. (If you're curious then feel free to drop +me a mail.) I'm releasing it as open source, hoping that others can make good +use of it. + diff --git a/examples/main.c b/examples/main.c new file mode 100644 index 0000000..b2003b9 --- /dev/null +++ b/examples/main.c @@ -0,0 +1,205 @@ +#include +#include +#include + +using namespace sparsemap; + +// +// this code serves as a sample but also as a unittest. +// +int main() { + int size = 4; + setbuf(stdout, 0); // disable printf() buffering + printf("Please wait a moment."); +#if 1 + uint8_t buffer[1024]; + uint8_t buffer2[1024]; + + SparseMap sm; + sm.create(buffer, sizeof(buffer)); + assert(sm.get_size() == size); + sm.set(0, true); + assert(sm.get_size() == size + 4 + 8 + 8); + assert(sm.is_set(0) == true); + assert(sm.get_size() == size + 4 + 8 + 8); + assert(sm.is_set(1) == false); + sm.set(0, false); + assert(sm.get_size() == size); + + sm.clear(); + sm.set(64, true); + assert(sm.is_set(64) == true); + assert(sm.get_size() == size + 4 + 8 + 8); + + sm.clear(); + printf("."); + + // set [0..100000] + for (int i = 0; i < 100000; i++) { + assert(sm.is_set(i) == false); + sm.set(i, true); + if (i > 5) { + for (int j = i - 5; j <= i; j++) + assert(sm.is_set(j) == true); + } + + assert(sm.is_set(i) == true); + } + + printf("."); + + for (int i = 0; i < 100000; i++) + assert(sm.is_set(i) == true); + + // unset [0..10000] + for (int i = 0; i < 10000; i++) { + assert(sm.is_set(i) == true); + sm.set(i, false); + assert(sm.is_set(i) == false); + } + + for (int i = 0; i < 10000; i++) + assert(sm.is_set(i) == false); + + sm.clear(); + printf("."); + + // set [10000..0] + for (int i = 10000; i >= 0; i--) { + assert(sm.is_set(i) == false); + sm.set(i, true); + assert(sm.is_set(i) == true); + } + + for (int i = 10000; i >= 0; i--) + assert(sm.is_set(i) == true); + printf("."); + + // open and compare + SparseMap sm2; + sm2.open(buffer, sizeof(buffer)); + for (int i = 0; i < 10000; i++) + assert(sm2.is_set(i) == sm.is_set(i)); + + // unset [10000..0] + for (int i = 10000; i >= 0; i--) { + assert(sm.is_set(i) == true); + sm.set(i, false); + assert(sm.is_set(i) == false); + } + + for (int i = 10000; i >= 0; i--) + assert(sm.is_set(i) == false); + + printf("."); + sm.clear(); + + sm.set(0, true); + sm.set(2048 * 2 + 1, true); + assert(sm.is_set(0) == true); + assert(sm.is_set(2048 * 2 + 0) == false); + assert(sm.is_set(2048 * 2 + 1) == true); + assert(sm.is_set(2048 * 2 + 2) == false); + sm.set(2048, true); + assert(sm.is_set(0) == true); + assert(sm.is_set(2047) == false); + assert(sm.is_set(2048) == true); + assert(sm.is_set(2049) == false); + assert(sm.is_set(2048 * 2 + 2) == false); + assert(sm.is_set(2048 * 2 + 0) == false); + assert(sm.is_set(2048 * 2 + 1) == true); + assert(sm.is_set(2048 * 2 + 2) == false); + + sm.clear(); + printf("."); + + for (int i = 0; i < 100000; i++) + sm.set(i, true); + for (int i = 0; i < 100000; i++) + assert(sm.select(i) == (unsigned)i); + + sm.clear(); + printf("."); + + for (int i = 1; i < 513; i++) + sm.set(i, true); + for (int i = 1; i < 513; i++) + assert(sm.select(i - 1) == (unsigned)i); + + sm.clear(); + printf("."); + + for (int i = 0; i < 8; i++) + sm.set(i * 10, true); + for (int i = 0; i < 8; i++) + assert(sm.select(i) == (unsigned)i * 10); + + // split and move, aligned to MiniMap capacity + sm2.create(buffer2, sizeof(buffer2)); + sm.clear(); + for (int i = 0; i < 2048 * 2; i++) + sm.set(i, true); + sm.split(2048, &sm2); + for (int i = 0; i < 2048; i++) { + assert(sm.is_set(i) == true); + assert(sm2.is_set(i) == false); + } + for (int i = 2048; i < 2048 * 2; i++) { + assert(sm.is_set(i) == false); + assert(sm2.is_set(i) == true); + } + printf("."); + + // split and move, aligned to BitVector capacity + sm2.create(buffer2, sizeof(buffer2)); + sm.clear(); + for (int i = 0; i < 2048 * 3; i++) + sm.set(i, true); + sm.split(64, &sm2); + for (int i = 0; i < 64; i++) { + assert(sm.is_set(i) == true); + assert(sm2.is_set(i) == false); + } + for (int i = 64; i < 2048 * 3; i++) { + assert(sm.is_set(i) == false); + assert(sm2.is_set(i) == true); + } + + printf("ok\n"); +#else + // + // This code was used to create the lookup table for + // sparsemap::MiniMap<>::calc_vector_size() + // + printf(" "); + for (unsigned int ch = 0; ch <= 0xff; ch++) { + if (ch > 0 && ch % 16 == 0) + printf("\n "); + + /* + // check if value is invalid (contains 2#01) + if ((ch & (0x3 << 0)) >> 0 == 1 + || (ch & (0x3 << 2)) >> 2 == 1 + || (ch & (0x3 << 4)) >> 4 == 1 + || (ch & (0x3 << 6)) >> 6 == 1) { + //printf("%d: -1\n", (int)ch); + printf(" -1,"); + continue; + } + */ + + // count all occurrences of 2#10 + int size = 0; + if ((ch & (0x3 << 0)) >> 0 == 2) + size++; + if ((ch & (0x3 << 2)) >> 2 == 2) + size++; + if ((ch & (0x3 << 4)) >> 4 == 2) + size++; + if ((ch & (0x3 << 6)) >> 6 == 2) + size++; + //printf("%u: %d\n", (unsigned int)ch, size); + printf(" %d,", size); + } +#endif +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..2d8d35f --- /dev/null +++ b/flake.nix @@ -0,0 +1,51 @@ +{ + description = "A Concurrent Skip List library for key/value pairs."; + + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + inputs.flake-utils.url = "github:numtide/flake-utils"; + + outputs = + { self + , nixpkgs + , flake-utils + , ... + }: + flake-utils.lib.eachDefaultSystem (system: + let +# pkgs = nixpkgs.legacyPackages.${system}; + pkgs = import nixpkgs { + inherit system; + config = { allowUnfree = true; }; + }; + in + { + devShells.default = pkgs.mkShell { + packages = with pkgs; [ + autoconf + bashInteractive + clang-tools + ed + gdb + graphviz-nox + meson + python311Packages.rbtools + ]; + }; + buildInputs = with pkgs; [ + glibc + ]; + nativeBuildInputs = with pkgs.buildPackages; [ + act + binutils + coreutils + gcc + gettext + libtool + m4 + make + perl + pkg-config + ripgrep + ]; + }); +} diff --git a/include/popcount.h b/include/popcount.h new file mode 100644 index 0000000..08a1926 --- /dev/null +++ b/include/popcount.h @@ -0,0 +1,72 @@ +/* + * + * from https://github.com/efficient/rankselect/popcount.h + * + * licensed under Apache 2 + */ + +/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ +#ifndef _FASTRANK_POPCOUNT_H_ +#define _FASTRANK_POPCOUNT_H_ + +#include +#include + +#define L8 0x0101010101010101ULL // Every lowest 8th bit set: 00000001... +#define G2 0xAAAAAAAAAAAAAAAAULL // Every highest 2nd bit: 101010... +#define G4 0x3333333333333333ULL // 00110011 ... used to group the sum of 4 bits. +#define G8 0x0F0F0F0F0F0F0F0FULL +#define H8 0x8080808080808080ULL +#define L9 0x0040201008040201ULL +#define H9 (L9 << 8) +#define L16 0x0001000100010001ULL +#define H16 0x8000800080008000ULL + +#define ONES_STEP_4 ( 0x1111111111111111ULL ) +#define ONES_STEP_8 ( 0x0101010101010101ULL ) +#define ONES_STEP_9 ( 1ULL << 0 | 1ULL << 9 | 1ULL << 18 | 1ULL << 27 | 1ULL << 36 | 1ULL << 45 | 1ULL << 54 ) +#define ONES_STEP_16 ( 1ULL << 0 | 1ULL << 16 | 1ULL << 32 | 1ULL << 48 ) +#define MSBS_STEP_4 ( 0x8ULL * ONES_STEP_4 ) +#define MSBS_STEP_8 ( 0x80ULL * ONES_STEP_8 ) +#define MSBS_STEP_9 ( 0x100ULL * ONES_STEP_9 ) +#define MSBS_STEP_16 ( 0x8000ULL * ONES_STEP_16 ) +#define INCR_STEP_8 ( 0x80ULL << 56 | 0x40ULL << 48 | 0x20ULL << 40 | 0x10ULL << 32 | 0x8ULL << 24 | 0x4ULL << 16 | 0x2ULL << 8 | 0x1 ) + +#define ONES_STEP_32 ( 0x0000000100000001ULL ) +#define MSBS_STEP_32 ( 0x8000000080000000ULL ) + +#define COMPARE_STEP_8(x,y) ( ( ( ( ( (x) | MSBS_STEP_8 ) - ( (y) & ~MSBS_STEP_8 ) ) ^ (x) ^ ~(y) ) & MSBS_STEP_8 ) >> 7 ) +#define LEQ_STEP_8(x,y) ( ( ( ( ( (y) | MSBS_STEP_8 ) - ( (x) & ~MSBS_STEP_8 ) ) ^ (x) ^ (y) ) & MSBS_STEP_8 ) >> 7 ) + +#define UCOMPARE_STEP_9(x,y) ( ( ( ( ( ( (x) | MSBS_STEP_9 ) - ( (y) & ~MSBS_STEP_9 ) ) | ( x ^ y ) ) ^ ( x | ~y ) ) & MSBS_STEP_9 ) >> 8 ) +#define UCOMPARE_STEP_16(x,y) ( ( ( ( ( ( (x) | MSBS_STEP_16 ) - ( (y) & ~MSBS_STEP_16 ) ) | ( x ^ y ) ) ^ ( x | ~y ) ) & MSBS_STEP_16 ) >> 15 ) +#define ULEQ_STEP_9(x,y) ( ( ( ( ( ( (y) | MSBS_STEP_9 ) - ( (x) & ~MSBS_STEP_9 ) ) | ( x ^ y ) ) ^ ( x & ~y ) ) & MSBS_STEP_9 ) >> 8 ) +#define ULEQ_STEP_16(x,y) ( ( ( ( ( ( (y) | MSBS_STEP_16 ) - ( (x) & ~MSBS_STEP_16 ) ) | ( x ^ y ) ) ^ ( x & ~y ) ) & MSBS_STEP_16 ) >> 15 ) +#define ZCOMPARE_STEP_8(x) ( ( ( x | ( ( x | MSBS_STEP_8 ) - ONES_STEP_8 ) ) & MSBS_STEP_8 ) >> 7 ) + +// Population count of a 64 bit integer in SWAR (SIMD within a register) style +// From Sebastiano Vigna, "Broadword Implementation of Rank/Select Queries" +// http://sux.dsi.unimi.it/paper.pdf p4 +// This variant uses multiplication for the last summation instead of +// continuing the shift/mask/addition chain. +inline int suxpopcount(uint64_t x) { + // Step 1: 00 - 00 = 0; 01 - 00 = 01; 10 - 01 = 01; 11 - 01 = 10; + x = x - ((x & G2) >> 1); + // step 2: add 2 groups of 2. + x = (x & G4) + ((x >> 2) & G4); + // 2 groups of 4. + x = (x + (x >> 4)) & G8; + // Using a multiply to collect the 8 groups of 8 together. + x = x * L8 >> 56; + return x; +} + +// Default to using the GCC builtin popcount. On architectures +// with -march popcnt, this compiles to a single popcnt instruction. +#ifndef popcount +# define popcount __builtin_popcountll +#else +# define popcount suxpopcount +#endif + +#endif /* _FASTRANK_POPCOUNT_H_ */ diff --git a/include/sparsemap.h b/include/sparsemap.h new file mode 100644 index 0000000..e69de29 diff --git a/main b/main new file mode 100755 index 0000000..83e3a1b Binary files /dev/null and b/main differ diff --git a/src/sparsemap.c b/src/sparsemap.c new file mode 100644 index 0000000..c171024 --- /dev/null +++ b/src/sparsemap.c @@ -0,0 +1,1021 @@ +// +// SparseMap +// +// This is an implementation for a sparse, compressed bitmap. It is resizable +// and mutable, with ok performance for random access modifications +// and lookups. +// +// The implementation is separated into tiers. +// +// Tier 0 (lowest): bits are stored in a BitVector (usually a uint64_t). +// +// Tier 1 (middle): multiple BitVectors are managed in a MiniMap. The MiniMap +// only stores those BitVectors that have a mixed payload of bits (i.e. +// some bits are 1, some are 0). As soon as ALL bits in a BitVector are +// identical, this BitVector is no longer stored. (This is the compression +// aspect.) +// The MiniMap therefore stores additional flags (2 bit) for each BitVector +// in an additional word (same size as the BitVector itself). +// +// 00 11 22 33 +// ^-- descriptor for BitVector 1 +// ^-- descriptor for BitVector 2 +// ^-- descriptor for BitVector 3 +// ^-- descriptor for BitVector 4 +// +// Those flags (*) can have one of the following values: +// +// 00 The BitVector is all zero -> BitVector is not stored +// 11 The BitVector is all one -> BitVector is not stored +// 10 The BitVector contains a bitmap -> BitVector is stored +// 01 The BitVector is not used (**) +// +// The serialized size of a MiniMap in memory therefore is at least +// one BitVector for the flags, and (optionally) additional BitVectors +// if they are required. +// +// (*) The code comments often use the Erlang format for binary +// representation, i.e. 2#10 for (binary) 01. +// +// (**) This flag is set to reduce the capacity of a MiniMap. This is +// a hamsterdb-specific extension. +// +// Tier 2 (highest): the SparseMap manages multiple MiniMaps. Each MiniMap +// has its own offset (relative to the offset of the SparseMap). In +// addition, the SparseMap manages the memory of the MiniMap, and +// is able to grow or shrink that memory as required. +// + +#ifndef SPARSEMAP_H +#define SPARSEMAP_H + +#include +#include +#include +#include +#include +#include + +#include "popcount.h" + + +namespace sparsemap { + +// +// This helper structure is returned by MiniMap::set() +// +template +struct MultiReturn +{ + // the return code - kOk, kNeedsToGrow, kNeedsToShrink + int code; + + // the position of the BitVector which is inserted/deleted + int position; + + // the value of the fill word (for growing) + BitVector fill; + + // Constructor + MultiReturn(int _code, int _position, BitVector _fill) + : code(_code), position(_position), fill(_fill) { + } +}; + + +// +// The MiniMap is usually not used directly; it is used by the SparseMap +// and can store up to 2048 bits. +// +template +class MiniMap { + public: + enum { + // number of bits that can be stored in a BitVector + kBitsPerVector = sizeof(BitVector) * 8, + + // number of flags that can be stored in a single index byte + kFlagsPerIndexByte = 4, + + // number of flags that can be stored in the index + kFlagsPerIndex = sizeof(BitVector) * kFlagsPerIndexByte, + + // maximum capacity of a MiniMap (in bits) + kMaxCapacity = kBitsPerVector * kFlagsPerIndex, + + // BitVector payload is all zeroes (2#00) + kPayloadZeroes = 0, + + // BitVector payload is all ones (2#11) + kPayloadOnes = 3, + + // BitVector payload is mixed (2#10) + kPayloadMixed = 2, + + // BitVector is not used (2#01) + kPayloadNone = 1, + + // a mask for checking flags (2 bits) + kFlagMask = 3, + + // return code for set(): ok, no further action required + kOk, + + // return code for set(): needs to grow this MiniMap + kNeedsToGrow, + + // return code for set(): needs to shrink this MiniMap + kNeedsToShrink + }; + + public: + // Constructor + MiniMap(uint8_t *data) + : m_data((BitVector *)data) { + } + + // Sets the capacity + void set_capacity(size_t capacity) { + if (capacity >= kMaxCapacity) + return; + + assert(capacity % kBitsPerVector == 0); + + size_t reduced = 0; + register uint8_t *p = (uint8_t *)m_data; + for (size_t i = sizeof(BitVector) - 1; i >= 0; i--) { + for (int j = kFlagsPerIndexByte - 1; j >= 0; j--) { + p[i] &= ~((BitVector)0x03 << (j * 2)); + p[i] |= ((BitVector)0x01 << (j * 2)); + reduced += kBitsPerVector; + if (capacity + reduced == kMaxCapacity) { + assert(get_capacity() == capacity); + return; + } + } + } + + assert(get_capacity() == capacity); + } + + // Returns the maximum capacity of this MiniMap + size_t get_capacity() { + size_t capacity = kMaxCapacity; + + register uint8_t *p = (uint8_t *)m_data; + for (size_t i = 0; i < sizeof(BitVector); i++, p++) { + if (!*p) + continue; + for (int j = 0; j < kFlagsPerIndexByte; j++) { + int flags = ((*p) & ((BitVector)kFlagMask << (j * 2))) >> (j * 2); + if (flags == kPayloadNone) + capacity -= kBitsPerVector; + } + } + return (capacity); + } + + // Returns true if this MiniMap is empty + bool is_empty() const { + // The MiniMap is empty if all flags (in m_data[0]) are zero. + if (m_data[0] == 0) + return (true); + + // It's also empty if all flags are Zero or None + register uint8_t *p = (uint8_t *)m_data; + for (size_t i = 0; i < sizeof(BitVector); i++, p++) { + if (*p) { + for (int j = 0; j < kFlagsPerIndexByte; j++) { + int flags = ((*p) & ((BitVector)kFlagMask << (j * 2))) >> (j * 2); + if (flags != kPayloadNone && flags != kPayloadZeroes) + return (false); + } + } + } + return (true); + } + + // Returns the size of the data buffer, in bytes + size_t get_size() const { + // At least one BitVector is required for the flags (m_data[0]) + size_t size = sizeof(BitVector); + // Use a lookup table for each byte of the flags + register uint8_t *p = (uint8_t *)m_data; + for (size_t i = 0; i < sizeof(BitVector); i++, p++) + size += sizeof(BitVector) * calc_vector_size(*p); + + return (size); + } + + // Returns the value of a bit at index |idx| + bool is_set(size_t idx) const { + // in which BitVector is |idx| stored? + int bv = idx / kBitsPerVector; + assert(bv < kFlagsPerIndex); + + // now retrieve the flags of that BitVector + int flags = ((*m_data) & ((BitVector)kFlagMask << (bv * 2))) >> (bv * 2); + switch (flags) { + case kPayloadZeroes: + case kPayloadNone: + return (false); + case kPayloadOnes: + return (true); + default: + assert(flags == kPayloadMixed); + // fall through + } + + // get the BitVector at |bv| + BitVector w = m_data[1 + get_position(bv)]; + // and finally check the bit in that BitVector + return ((w & ((BitVector)1 << (idx % kBitsPerVector))) > 0); + } + + // Sets the value of a bit at index |idx|. This function returns + // a MultiReturn structure. If MultiReturn::code is |kNeedsToGrow| + // or |kNeedsToShrink| then the caller has to perform the relevant + // actions and call set() again, this time with |retried| = true! + MultiReturn set(size_t idx, bool value, bool retried = false) { + // in which BitVector is |idx| stored? + int bv = idx / kBitsPerVector; + assert(bv < kFlagsPerIndex); + + // now retrieve the flags of that BitVector + int flags = ((*m_data) & ((BitVector)kFlagMask << (bv * 2))) >> (bv * 2); + assert(flags != kPayloadNone); + if (flags == kPayloadZeroes) { + // easy - set bit to 0 in a BitVector of zeroes + if (value == false) + return (MultiReturn(kOk, 0, 0)); + // the SparseMap must grow this MiniMap by one additional BitVector, + // then try again + if (!retried) + return (MultiReturn(kNeedsToGrow, + 1 + get_position(bv), 0)); + // new flags are 2#10 (currently, flags are set to 2#00 + // 2#00 | 2#10 = 2#10) + m_data[0] |= ((BitVector)0x2 << (bv * 2)); + // fall through + } + else if (flags == kPayloadOnes) { + // easy - set bit to 1 in a BitVector of ones + if (value == true) + return (MultiReturn(kOk, 0, 0)); + // the SparseMap must grow this MiniMap by one additional BitVector, + // then try again + if (!retried) + return (MultiReturn(kNeedsToGrow, + 1 + get_position(bv), (BitVector)-1)); + // new flags are 2#10 (currently, flags are set to 2#11; + // 2#11 ^ 2#01 = 2#10) + m_data[0] ^= ((BitVector)0x1 << (bv * 2)); + // fall through + } + + // now flip the bit + size_t position = 1 + get_position(bv); + BitVector w = m_data[position]; + if (value) + w |= (BitVector)1 << (idx % kBitsPerVector); + else + w &= ~((BitVector)1 << (idx % kBitsPerVector)); + + // if this BitVector is now all zeroes or ones then we can remove it + if (w == 0) { + m_data[0] &= ~((BitVector)kPayloadOnes << (bv * 2)); + return (MultiReturn(kNeedsToShrink, position, 0)); + } + if (w == (BitVector)-1) { + m_data[0] |= (BitVector)kPayloadOnes << (bv * 2); + return (MultiReturn(kNeedsToShrink, position, 0)); + } + + m_data[position] = w; + return (MultiReturn(kOk, 0, 0)); + } + + // Decompresses the whole bitmap; calls visitor's operator() for all bits + // Returns the number of (set) bits that were passed to the scanner + template + size_t scan(IndexedType start, Scanner &scanner, size_t skip) { + size_t ret = 0; + register uint8_t *p = (uint8_t *)m_data; + IndexedType buffer[kBitsPerVector]; + for (size_t i = 0; i < sizeof(BitVector); i++, p++) { + if (*p == 0) { + // skip the zeroes + continue; + } + + for (int j = 0; j < kFlagsPerIndexByte; j++) { + int flags = ((*p) & ((BitVector)kFlagMask << (j * 2))) >> (j * 2); + if (flags == kPayloadNone || flags == kPayloadZeroes) { + // ignore the zeroes + } + else if (flags == kPayloadOnes) { + if (skip) { + if (skip >= kBitsPerVector) { + skip -= kBitsPerVector; + ret += kBitsPerVector; + continue; + } + size_t n = 0; + for (size_t b = skip; b < kBitsPerVector; b++) + buffer[n++] = start + b; + scanner(&buffer[0], n); + ret += n; + skip = 0; + } + else { + for (size_t b = 0; b < kBitsPerVector; b++) + buffer[b] = start + b; + scanner(&buffer[0], kBitsPerVector); + ret += kBitsPerVector; + } + } + else if (flags == kPayloadMixed) { + BitVector w = m_data[1 + get_position(i * kFlagsPerIndexByte + j)]; + int n = 0; + if (skip) { + for (int b = 0; b < kBitsPerVector; b++) { + if (w & ((BitVector)1 << b)) { + if (skip) { + skip--; + continue; + } + buffer[n++] = start + b; + ret++; + } + } + } + else { + for (int b = 0; b < kBitsPerVector; b++) { + if (w & ((BitVector)1 << b)) + buffer[n++] = start + b; + } + ret += n; + } + assert(n > 0); + scanner(&buffer[0], n); + } + } + } + return (ret); + } + + // Returns the index of the 'nth' set bit; sets |*pnew_n| to 0 if the + // n'th bit was found in this MiniMap, or to the new, reduced value of |n| + size_t select(size_t n, ssize_t *pnew_n) { + size_t ret = 0; + + register uint8_t *p = (uint8_t *)m_data; + for (size_t i = 0; i < sizeof(BitVector); i++, p++) { + if (*p == 0) { + ret += kFlagsPerIndexByte * kBitsPerVector; + continue; + } + + for (int j = 0; j < kFlagsPerIndexByte; j++) { + int flags = ((*p) & ((BitVector)kFlagMask << (j * 2))) >> (j * 2); + if (flags == kPayloadNone) + continue; + if (flags == kPayloadZeroes) { + ret += kBitsPerVector; + continue; + } + if (flags == kPayloadOnes) { + if (n > kBitsPerVector) { + n -= kBitsPerVector; + ret += kBitsPerVector; + continue; + } + + *pnew_n = -1; + return (ret + n); + } + if (flags == kPayloadMixed) { + BitVector w = m_data[1 + get_position(i * kFlagsPerIndexByte + j)]; + for (int k = 0; k < kBitsPerVector; k++) { + if (w & ((BitVector)1 << k)) { + if (n == 0) { + *pnew_n = -1; + return (ret); + } + n--; + } + ret++; + } + } + } + } + + *pnew_n = n; + return (ret); + } + + // Counts the set bits in the range [0, idx] + size_t rank(size_t idx) { + size_t ret = 0; + + register uint8_t *p = (uint8_t *)m_data; + for (size_t i = 0; i < sizeof(BitVector); i++, p++) { + for (int j = 0; j < kFlagsPerIndexByte; j++) { + int flags = ((*p) & ((BitVector)kFlagMask << (j * 2))) >> (j * 2); + if (flags == kPayloadNone) + continue; + if (flags == kPayloadZeroes) { + if (idx > kBitsPerVector) + idx -= kBitsPerVector; + else + return (ret); + } + else if (flags == kPayloadOnes) { + if (idx > kBitsPerVector) { + idx -= kBitsPerVector; + ret += kBitsPerVector; + } + else + return (ret + idx); + } + else if (flags == kPayloadMixed) { + if (idx > kBitsPerVector) { + idx -= kBitsPerVector; + ret += popcount((uint64_t)m_data[1 + + get_position(i * kFlagsPerIndexByte + j)]); + } + else { + BitVector w = m_data[1 + get_position(i * kFlagsPerIndexByte + j)]; + for (size_t k = 0; k < idx; k++) { + if (w & ((BitVector)1 << k)) + ret++; + } + return (ret); + } + } + } + } + return (ret); + } + + private: + // Returns the position of a BitVector in m_data + size_t get_position(int bv) const { + // handle 4 indices (1 byte) at a time + size_t num_bytes = bv / (kFlagsPerIndexByte * kBitsPerVector); + + size_t position = 0; + register uint8_t *p = (uint8_t *)m_data; + for (size_t i = 0; i < num_bytes; i++, p++) + position += calc_vector_size(*p); + + bv -= num_bytes * kFlagsPerIndexByte; + for (int i = 0; i < bv; i++) { + int flags = ((*m_data) & ((BitVector)kFlagMask << (i * 2))) >> (i * 2); + if (flags == kPayloadMixed) + position++; + } + + return (position); + } + + // Calculates the number of BitVectors required by a single byte + // with flags (in m_data[0]) + size_t calc_vector_size(uint8_t b) const { + static int lookup[] = { + 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0, + 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0, + 1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 3, 2, 1, 1, 2, 1, + 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0, + 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0, + 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0, + 1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 3, 2, 1, 1, 2, 1, + 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0, + 1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 3, 2, 1, 1, 2, 1, + 1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 3, 2, 1, 1, 2, 1, + 2, 2, 3, 2, 2, 2, 3, 2, 3, 3, 4, 3, 2, 2, 3, 2, + 1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 3, 2, 1, 1, 2, 1, + 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0, + 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0, + 1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 3, 2, 1, 1, 2, 1, + 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0 + }; + return ((size_t)lookup[b]); + } + + // Pointer to the stored data; m_data[0] always contains the index + BitVector *m_data; +}; + + +// +// The SparseMap is the public interface of this library. +// +// |IndexedType| is the user's numerical data type which is mapped to +// a single bit in the bitmap. Usually this is uint32_t or uint64_t. +// |BitVector| is the storage type for a bit vector used by the MiniMap. +// Usually this is a uint64_t. +// +template +class SparseMap { + enum { + // metadata overhead: + // 4 bytes for minimap count + kSizeofOverhead = sizeof(uint32_t) + }; + + public: + // Constructor + SparseMap() + : m_data(0), m_data_size(0), m_data_used(0) { + } + + // Creates a new SparseMap at the specified buffer + void create(uint8_t *data, size_t data_size, + size_t capacity = std::numeric_limits::max()) { + m_data = data; + m_data_size = data_size; + clear(); + } + + // Opens an existing SparseMap at the specified buffer + void open(uint8_t *data, size_t data_size) { + m_data = data; + m_data_size = data_size; + } + + // Resizes the data range + void set_data_size(size_t data_size) { + m_data_size = data_size; + } + + // Returns the size of the underlying byte array + size_t get_range_size() const { + return (m_data_size); + } + + // Returns the value of a bit at index |idx| + bool is_set(size_t idx) { + assert(get_size() >= kSizeofOverhead); + + // Get the MiniMap which manages this index + ssize_t offset = get_minimap_offset(idx); + + // No MiniMaps available -> the bit is not set + if (offset == -1) + return (false); + + // Otherwise load the MiniMap + uint8_t *p = get_minimap_data(offset); + IndexedType start = *(IndexedType *)p; + + MiniMap minimap(p + sizeof(IndexedType)); + + // Check if the bit is out of bounds of the MiniMap; if yes then + // the bit is not set + if (idx < start || idx - start >= minimap.get_capacity()) + return (false); + + // Otherwise ask the MiniMap whether the bit is set + return (minimap.is_set(idx - start)); + } + + // Sets the bit at index |idx| to true or false, depending on |value| + void set(size_t idx, bool value) { + assert(get_size() >= kSizeofOverhead); + + // Get the MiniMap which manages this index + ssize_t offset = get_minimap_offset(idx); + bool dont_grow = false; + + // If there is no MiniMap and the bit is set to zero then return + // immediately; otherwise create an initial MiniMap + if (offset == -1) { + if (value == false) + return; + + uint8_t buf[sizeof(IndexedType) + sizeof(BitVector) * 2] = {0}; + append_data(&buf[0], sizeof(buf)); + + uint8_t *p = get_minimap_data(0); + *(IndexedType *)p = get_aligned_offset(idx); + + set_minimap_count(1); + + // we already inserted an additional BitVector; later on there + // is no need to grow the vector even further + dont_grow = true; + offset = 0; + } + + // Load the MiniMap + uint8_t *p = get_minimap_data(offset); + IndexedType start = *(IndexedType *)p; + + // The new index is smaller than the first MiniMap: create a new + // MiniMap and insert it at the front + if (idx < start) { + if (value == false) // nothing to do + return; + + uint8_t buf[sizeof(IndexedType) + sizeof(BitVector) * 2] = {0}; + insert_data(offset, &buf[0], sizeof(buf)); + + size_t aligned_idx = get_fully_aligned_offset(idx); + if (start - aligned_idx < MiniMap::kMaxCapacity) { + MiniMap minimap(p + sizeof(IndexedType)); + minimap.set_capacity(start - aligned_idx); + } + *(IndexedType *)p = start = aligned_idx; + + // we just added another minimap! + set_minimap_count(get_minimap_count() + 1); + + // we already inserted an additional BitVector; later on there + // is no need to grow the vector even further + dont_grow = true; + } + + // A MiniMap exists, but the new index exceeds its capacities: create + // a new MiniMap and insert it after the current one + else { + MiniMap minimap(p + sizeof(IndexedType)); + if (idx - start >= minimap.get_capacity()) { + if (value == false) // nothing to do + return; + + size_t size = minimap.get_size(); + offset += sizeof(IndexedType) + size; + p += sizeof(IndexedType) + size; + + uint8_t buf[sizeof(IndexedType) + sizeof(BitVector) * 2] = {0}; + insert_data(offset, &buf[0], sizeof(buf)); + + start += minimap.get_capacity(); + if ((size_t)start + MiniMap::kMaxCapacity < idx) + start = get_fully_aligned_offset(idx); + *(IndexedType *)p = start; + + // we just added another minimap! + set_minimap_count(get_minimap_count() + 1); + + // we already inserted an additional BitVector; later on there + // is no need to grow the vector even further + dont_grow = true; + } + } + + MiniMap minimap(p + sizeof(IndexedType)); + + // Now update the MiniMap + MultiReturn mret = minimap.set(idx - start, value); + switch (mret.code) { + case MiniMap::kOk: + break; + case MiniMap::kNeedsToGrow: + if (!dont_grow) { + offset += sizeof(IndexedType) + mret.position * sizeof(BitVector); + insert_data(offset, (uint8_t *)&mret.fill, sizeof(BitVector)); + } + mret = minimap.set(idx - start, value, true); + assert(mret.code == MiniMap::kOk); + break; + case MiniMap::kNeedsToShrink: + // if the MiniMap is empty then remove it + if (minimap.is_empty()) { + assert(mret.position == 1); + remove_data(offset, sizeof(IndexedType) + sizeof(BitVector) * 2); + set_minimap_count(get_minimap_count() - 1); + } + else { + offset += sizeof(IndexedType) + mret.position * sizeof(BitVector); + remove_data(offset, sizeof(BitVector)); + } + break; + default: + assert(!"shouldn't be here"); + break; + } + assert(get_size() >= kSizeofOverhead); + } + + // Clears the whole buffer + void clear() { + m_data_used = kSizeofOverhead; + set_minimap_count(0); + } + + // Returns the offset of the very first bit + IndexedType get_start_offset() { + if (get_minimap_count() == 0) + return (0); + return (*(IndexedType *)get_minimap_data(0)); + } + + // Returns the used size in the data buffer + size_t get_size() { + if (m_data_used) { + assert(m_data_used == get_size_impl()); + return (m_data_used); + } + return (m_data_used = get_size_impl()); + } + + // Decompresses the whole bitmap; calls visitor's operator() for all bits + template + void scan(Scanner &scanner, size_t skip) { + uint8_t *p = get_minimap_data(0); + + size_t count = get_minimap_count(); + for (size_t i = 0; i < count; i++) { + IndexedType start = *(IndexedType *)p; + p += sizeof(IndexedType); + MiniMap minimap(p); + size_t skipped = minimap.scan(start, scanner, skip); + if (skip) { + assert(skip >= skipped); + skip -= skipped; + } + p += minimap.get_size(); + } + } + + // Appends all MiniMaps from |sstart| to |other|, then reduces the + // MiniMap-count appropriately + // + // |sstart| must be BitVector-aligned! + void split(size_t sstart, SparseMap *other) { + assert(sstart % MiniMap::kBitsPerVector == 0); + + // |dst| points to the destination buffer + uint8_t *dst = other->get_minimap_end(); + + // |src| points to the source-MiniMap + uint8_t *src = get_minimap_data(0); + + // |sstart| is relative to the beginning of this SparseMap; better + // make it absolute + sstart += *(IndexedType *)src; + + bool in_middle = false; + uint8_t *prev = src; + size_t i, count = get_minimap_count(); + for (i = 0; i < count; i++) { + IndexedType start = *(IndexedType *)src; + MiniMap minimap(src + sizeof(IndexedType)); + if (start == sstart) + break; + if (start + minimap.get_capacity() > sstart) { + in_middle = true; + break; + } + if (start > sstart) { + src = prev; + i--; + break; + } + + prev = src; + src += sizeof(IndexedType) + minimap.get_size(); + } + if (i == count) { + assert(get_size() > kSizeofOverhead); + assert(other->get_size() > kSizeofOverhead); + return; + } + + // Now copy all the remaining MiniMaps + int moved = 0; + + // If |sstart| is in the middle of a MiniMap then this MiniMap has + // to be split + if (in_middle) { + uint8_t buf[sizeof(IndexedType) + sizeof(BitVector) * 2] = {0}; + memcpy(dst, &buf[0], sizeof(buf)); + + *(IndexedType *)dst = sstart; + dst += sizeof(IndexedType); + + // the |other| SparseMap now has one additional MiniMap + other->set_minimap_count(other->get_minimap_count() + 1); + if (other->m_data_used != 0) + other->m_data_used += sizeof(IndexedType) + sizeof(BitVector); + + src += sizeof(IndexedType); + MiniMap sminimap(src); + size_t capacity = sminimap.get_capacity(); + + MiniMap dminimap(dst); + dminimap.set_capacity(capacity - (sstart % capacity)); + + // now copy the bits + size_t d = sstart; + for (size_t j = sstart % capacity; j < capacity; j++, d++) { + if (sminimap.is_set(j)) + other->set(d, true); + } + + src += sminimap.get_size(); + size_t dsize = dminimap.get_size(); + dst += dsize; + i++; + + // reduce the capacity of the source-MiniMap + sminimap.set_capacity(sstart % capacity); + } + + // Now continue with all remaining minimaps + for (; i < count; i++) { + IndexedType start = *(IndexedType *)src; + src += sizeof(IndexedType); + MiniMap minimap(src); + size_t s = minimap.get_size(); + + *(IndexedType *)dst = start; + dst += sizeof(IndexedType); + memcpy(dst, src, s); + src += s; + dst += s; + + moved++; + } + + // force new calculation + other->m_data_used = 0; + m_data_used = 0; + + // Update the MiniMap counters + set_minimap_count(get_minimap_count() - moved); + other->set_minimap_count(other->get_minimap_count() + moved); + + assert(get_size() >= kSizeofOverhead); + assert(other->get_size() > kSizeofOverhead); + } + + // Returns the index of the 'nth' set bit; uses a 0-based index, + // i.e. n == 0 for the first bit which is set, n == 1 for the second bit etc + size_t select(size_t n) { + assert(get_size() >= kSizeofOverhead); + size_t result = 0; + size_t count = get_minimap_count(); + + uint8_t *p = get_minimap_data(0); + + for (size_t i = 0; i < count; i++) { + result = *(IndexedType *)p; + p += sizeof(IndexedType); + MiniMap minimap(p); + + ssize_t new_n = (ssize_t)n; + size_t index = minimap.select(n, &new_n); + if (new_n == -1) + return (result + index); + n = (size_t)new_n; + + p += minimap.get_size(); + } + assert(!"shouldn't be here"); + return (0); + } + + // Counts the set bits in the range [offset, idx] + size_t rank(size_t offset, size_t idx) { + assert(get_size() >= kSizeofOverhead); + size_t result = 0; + size_t count = get_minimap_count(); + + uint8_t *p = get_minimap_data(offset); + + for (size_t i = 0; i < count; i++) { + IndexedType start = *(IndexedType *)p; + if (start > idx) + return (result); + p += sizeof(IndexedType); + MiniMap minimap(p); + + result += minimap.rank(idx - start); + p += minimap.get_size(); + } + return (result); + } + + // Counts the set bits in the range [0, idx] + size_t rank(size_t idx) { + rank(0, idx); + } + + // Returns the number of MiniMaps + size_t get_minimap_count() const { + return (*(uint32_t *)&m_data[0]); + } + + private: + // Returns the used size in the data buffer + size_t get_size_impl() { + uint8_t *start = get_minimap_data(0); + uint8_t *p = start; + + size_t count = get_minimap_count(); + for (size_t i = 0; i < count; i++) { + p += sizeof(IndexedType); + MiniMap minimap(p); + p += minimap.get_size(); + } + return (kSizeofOverhead + p - start); + } + + // Returns the byte offset of a MiniMap in m_data + ssize_t get_minimap_offset(size_t idx) { + size_t count = get_minimap_count(); + if (count == 0) + return (-1); + + uint8_t *start = get_minimap_data(0); + uint8_t *p = start; + + for (size_t i = 0; i < count - 1; i++) { + IndexedType start = *(IndexedType *)p; + assert(start == get_aligned_offset(start)); + MiniMap minimap(p + sizeof(IndexedType)); + if (start >= idx || idx < start + minimap.get_capacity()) + break; + p += sizeof(IndexedType) + minimap.get_size(); + } + + return ((ssize_t)(p - start)); + } + + // Returns the data at the specified |offset| + uint8_t *get_minimap_data(size_t offset) { + return (&m_data[kSizeofOverhead + offset]); + } + + // Returns a pointer after the end of the used data + // TODO can also use m_data_used? + uint8_t *get_minimap_end() { + uint8_t *p = get_minimap_data(0); + + size_t count = get_minimap_count(); + for (size_t i = 0; i < count; i++) { + p += sizeof(IndexedType); + MiniMap minimap(p); + p += minimap.get_size(); + } + return (p); + } + + // Returns the aligned offset (aligned to BitVector capacity) + IndexedType get_aligned_offset(size_t idx) const { + const size_t capacity = MiniMap::kBitsPerVector; + return ((idx / capacity) * capacity); + } + + // Returns the aligned offset (aligned to MiniMap capacity) + IndexedType get_fully_aligned_offset(size_t idx) const { + const size_t capacity = MiniMap::kMaxCapacity; + return ((idx / capacity) * capacity); + } + + // Sets the number of MiniMaps + void set_minimap_count(size_t new_count) { + *(uint32_t *)&m_data[0] = (uint32_t)new_count; + } + + // Appends more data + void append_data(uint8_t *buffer, size_t buffer_size) { + memcpy(&m_data[m_data_used], buffer, buffer_size); + m_data_used += buffer_size; + } + + // Inserts data somewhere in the middle of m_data + void insert_data(size_t offset, uint8_t *buffer, size_t buffer_size) { + if (m_data_used + buffer_size > m_data_size) + throw std::overflow_error("buffer overflow"); + + uint8_t *p = get_minimap_data(offset); + memmove(p + buffer_size, p, m_data_used - offset); + memcpy(p, buffer, buffer_size); + m_data_used += buffer_size; + } + + // Removes data from m_data + void remove_data(size_t offset, size_t gap_size) { + assert(m_data_used >= offset + gap_size); + uint8_t *p = get_minimap_data(offset); + memmove(p, p + gap_size, m_data_used - offset - gap_size); + m_data_used -= gap_size; + } + + // The serialized bitmap data + uint8_t *m_data; + + // The total size of m_data + size_t m_data_size; + + // The used size of m_data + size_t m_data_used; +}; + +} // namespace sparsemap + +#endif // SPARSEMAP_H diff --git a/tests/munit.c b/tests/munit.c new file mode 100644 index 0000000..f42f8e9 --- /dev/null +++ b/tests/munit.c @@ -0,0 +1,2255 @@ +/* Copyright (c) 2013-2018 Evan Nemerson + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/*** Configuration ***/ + +/* This is just where the output from the test goes. It's really just + * meant to let you choose stdout or stderr, but if anyone really want + * to direct it to a file let me know, it would be fairly easy to + * support. */ +#if !defined(MUNIT_OUTPUT_FILE) +#define MUNIT_OUTPUT_FILE stdout +#endif + +/* This is a bit more useful; it tells µnit how to format the seconds in + * timed tests. If your tests run for longer you might want to reduce + * it, and if your computer is really fast and your tests are tiny you + * can increase it. */ +#if !defined(MUNIT_TEST_TIME_FORMAT) +#define MUNIT_TEST_TIME_FORMAT "0.8f" +#endif + +/* If you have long test names you might want to consider bumping + * this. The result information takes 43 characters. */ +#if !defined(MUNIT_TEST_NAME_LEN) +#define MUNIT_TEST_NAME_LEN 37 +#endif + +/* If you don't like the timing information, you can disable it by + * defining MUNIT_DISABLE_TIMING. */ +#if !defined(MUNIT_DISABLE_TIMING) +#define MUNIT_ENABLE_TIMING +#endif + +/*** End configuration ***/ + +#if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE < 200809L) +#undef _POSIX_C_SOURCE +#endif +#if !defined(_POSIX_C_SOURCE) +#define _POSIX_C_SOURCE 200809L +#endif + +/* Solaris freaks out if you try to use a POSIX or SUS standard without + * the "right" C standard. */ +#if defined(_XOPEN_SOURCE) +#undef _XOPEN_SOURCE +#endif + +#if defined(__STDC_VERSION__) +#if __STDC_VERSION__ >= 201112L +#define _XOPEN_SOURCE 700 +#elif __STDC_VERSION__ >= 199901L +#define _XOPEN_SOURCE 600 +#endif +#endif + +/* Because, according to Microsoft, POSIX is deprecated. You've got + * to appreciate the chutzpah. */ +#if defined(_MSC_VER) && !defined(_CRT_NONSTDC_NO_DEPRECATE) +#define _CRT_NONSTDC_NO_DEPRECATE +#endif + +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) +#include +#elif defined(_WIN32) +/* https://msdn.microsoft.com/en-us/library/tf4dy80a.aspx */ +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +#if !defined(MUNIT_NO_NL_LANGINFO) && !defined(_WIN32) +#define MUNIT_NL_LANGINFO +#include +#include +#include +#endif + +#if !defined(_WIN32) +#include +#include + +#include +#else +#include +#include +#include +#if !defined(STDERR_FILENO) +#define STDERR_FILENO _fileno(stderr) +#endif +#endif + +#include "munit.h" + +#define MUNIT_STRINGIFY(x) #x +#define MUNIT_XSTRINGIFY(x) MUNIT_STRINGIFY(x) + +#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_CC) || \ + defined(__IBMCPP__) +#define MUNIT_THREAD_LOCAL __thread +#elif (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201102L)) || \ + defined(_Thread_local) +#define MUNIT_THREAD_LOCAL _Thread_local +#elif defined(_WIN32) +#define MUNIT_THREAD_LOCAL __declspec(thread) +#endif + +/* MSVC 12.0 will emit a warning at /W4 for code like 'do { ... } + * while (0)', or 'do { ... } while (1)'. I'm pretty sure nobody + * at Microsoft compiles with /W4. */ +#if defined(_MSC_VER) && (_MSC_VER <= 1800) +#pragma warning(disable : 4127) +#endif + +#if defined(_WIN32) || defined(__EMSCRIPTEN__) +#define MUNIT_NO_FORK +#endif + +#if defined(__EMSCRIPTEN__) +#define MUNIT_NO_BUFFER +#endif + +/*** Logging ***/ + +static MunitLogLevel munit_log_level_visible = MUNIT_LOG_INFO; +static MunitLogLevel munit_log_level_fatal = MUNIT_LOG_ERROR; + +#if defined(MUNIT_THREAD_LOCAL) +static MUNIT_THREAD_LOCAL munit_bool munit_error_jmp_buf_valid = 0; +static MUNIT_THREAD_LOCAL jmp_buf munit_error_jmp_buf; +#endif + +/* At certain warning levels, mingw will trigger warnings about + * suggesting the format attribute, which we've explicity *not* set + * because it will then choke on our attempts to use the MS-specific + * I64 modifier for size_t (which we have to use since MSVC doesn't + * support the C99 z modifier). */ + +#if defined(__MINGW32__) || defined(__MINGW64__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wsuggest-attribute=format" +#endif + +MUNIT_PRINTF(5, 0) +static void +munit_logf_exv(MunitLogLevel level, FILE *fp, const char *filename, int line, + const char *format, va_list ap) +{ + if (level < munit_log_level_visible) + return; + + switch (level) { + case MUNIT_LOG_DEBUG: + fputs("Debug", fp); + break; + case MUNIT_LOG_INFO: + fputs("Info", fp); + break; + case MUNIT_LOG_WARNING: + fputs("Warning", fp); + break; + case MUNIT_LOG_ERROR: + fputs("Error", fp); + break; + default: + munit_logf_ex(MUNIT_LOG_ERROR, filename, line, "Invalid log level (%d)", + level); + return; + } + + fputs(": ", fp); + if (filename != NULL) + fprintf(fp, "%s:%d: ", filename, line); + vfprintf(fp, format, ap); + fputc('\n', fp); +} + +MUNIT_PRINTF(3, 4) +static void +munit_logf_internal(MunitLogLevel level, FILE *fp, const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + munit_logf_exv(level, fp, NULL, 0, format, ap); + va_end(ap); +} + +static void +munit_log_internal(MunitLogLevel level, FILE *fp, const char *message) +{ + munit_logf_internal(level, fp, "%s", message); +} + +void +munit_logf_ex(MunitLogLevel level, const char *filename, int line, + const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + munit_logf_exv(level, stderr, filename, line, format, ap); + va_end(ap); + + if (level >= munit_log_level_fatal) { +#if defined(MUNIT_THREAD_LOCAL) + if (munit_error_jmp_buf_valid) + longjmp(munit_error_jmp_buf, 1); +#endif + abort(); + } +} + +void +munit_errorf_ex(const char *filename, int line, const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + munit_logf_exv(MUNIT_LOG_ERROR, stderr, filename, line, format, ap); + va_end(ap); + +#if defined(MUNIT_THREAD_LOCAL) + if (munit_error_jmp_buf_valid) + longjmp(munit_error_jmp_buf, 1); +#endif + abort(); +} + +#if defined(__MINGW32__) || defined(__MINGW64__) +#pragma GCC diagnostic pop +#endif + +#if !defined(MUNIT_STRERROR_LEN) +#define MUNIT_STRERROR_LEN 80 +#endif + +static void +munit_log_errno(MunitLogLevel level, FILE *fp, const char *msg) +{ +#if defined(MUNIT_NO_STRERROR_R) || \ + (defined(__MINGW32__) && !defined(MINGW_HAS_SECURE_API)) + munit_logf_internal(level, fp, "%s: %s (%d)", msg, strerror(errno), errno); +#else + char munit_error_str[MUNIT_STRERROR_LEN]; + munit_error_str[0] = '\0'; + +#if !defined(_WIN32) + strerror_r(errno, munit_error_str, MUNIT_STRERROR_LEN); +#else + strerror_s(munit_error_str, MUNIT_STRERROR_LEN, errno); +#endif + + munit_logf_internal(level, fp, "%s: %s (%d)", msg, munit_error_str, errno); +#endif +} + +/*** Memory allocation ***/ + +void * +munit_malloc_ex(const char *filename, int line, size_t size) +{ + void *ptr; + + if (size == 0) + return NULL; + + ptr = calloc(1, size); + if (MUNIT_UNLIKELY(ptr == NULL)) { + munit_logf_ex(MUNIT_LOG_ERROR, filename, line, + "Failed to allocate %" MUNIT_SIZE_MODIFIER "u bytes.", size); + } + + return ptr; +} + +/*** Timer code ***/ + +#if defined(MUNIT_ENABLE_TIMING) + +#define psnip_uint64_t munit_uint64_t +#define psnip_uint32_t munit_uint32_t + +/* Code copied from portable-snippets + * . If you need to + * change something, please do it there so we can keep the code in + * sync. */ + +/* Clocks (v1) + * Portable Snippets - https://gitub.com/nemequ/portable-snippets + * Created by Evan Nemerson + * + * To the extent possible under law, the authors have waived all + * copyright and related or neighboring rights to this code. For + * details, see the Creative Commons Zero 1.0 Universal license at + * https://creativecommons.org/publicdomain/zero/1.0/ + */ + +#if !defined(PSNIP_CLOCK_H) +#define PSNIP_CLOCK_H + +#if !defined(psnip_uint64_t) +#include "../exact-int/exact-int.h" +#endif + +#if !defined(PSNIP_CLOCK_STATIC_INLINE) +#if defined(__GNUC__) +#define PSNIP_CLOCK__COMPILER_ATTRIBUTES __attribute__((__unused__)) +#else +#define PSNIP_CLOCK__COMPILER_ATTRIBUTES +#endif + +#define PSNIP_CLOCK__FUNCTION PSNIP_CLOCK__COMPILER_ATTRIBUTES static +#endif + +enum PsnipClockType { + /* This clock provides the current time, in units since 1970-01-01 + * 00:00:00 UTC not including leap seconds. In other words, UNIX + * time. Keep in mind that this clock doesn't account for leap + * seconds, and can go backwards (think NTP adjustments). */ + PSNIP_CLOCK_TYPE_WALL = 1, + /* The CPU time is a clock which increases only when the current + * process is active (i.e., it doesn't increment while blocking on + * I/O). */ + PSNIP_CLOCK_TYPE_CPU = 2, + /* Monotonic time is always running (unlike CPU time), but it only + ever moves forward unless you reboot the system. Things like NTP + adjustments have no effect on this clock. */ + PSNIP_CLOCK_TYPE_MONOTONIC = 3 +}; + +struct PsnipClockTimespec { + psnip_uint64_t seconds; + psnip_uint64_t nanoseconds; +}; + +/* Methods we support: */ + +#define PSNIP_CLOCK_METHOD_CLOCK_GETTIME 1 +#define PSNIP_CLOCK_METHOD_TIME 2 +#define PSNIP_CLOCK_METHOD_GETTIMEOFDAY 3 +#define PSNIP_CLOCK_METHOD_QUERYPERFORMANCECOUNTER 4 +#define PSNIP_CLOCK_METHOD_MACH_ABSOLUTE_TIME 5 +#define PSNIP_CLOCK_METHOD_CLOCK 6 +#define PSNIP_CLOCK_METHOD_GETPROCESSTIMES 7 +#define PSNIP_CLOCK_METHOD_GETRUSAGE 8 +#define PSNIP_CLOCK_METHOD_GETSYSTEMTIMEPRECISEASFILETIME 9 +#define PSNIP_CLOCK_METHOD_GETTICKCOUNT64 10 + +#include + +#if defined(HEDLEY_UNREACHABLE) +#define PSNIP_CLOCK_UNREACHABLE() HEDLEY_UNREACHABLE() +#else +#define PSNIP_CLOCK_UNREACHABLE() assert(0) +#endif + +/* Choose an implementation */ + +/* #undef PSNIP_CLOCK_WALL_METHOD */ +/* #undef PSNIP_CLOCK_CPU_METHOD */ +/* #undef PSNIP_CLOCK_MONOTONIC_METHOD */ + +/* We want to be able to detect the libc implementation, so we include + ( isn't available everywhere). */ + +#if defined(__unix__) || defined(__unix) || defined(__linux__) +#include +#include +#endif + +#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) +/* These are known to work without librt. If you know of others + * please let us know so we can add them. */ +#if (defined(__GLIBC__) && \ + (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 17))) || \ + (defined(__FreeBSD__)) +#define PSNIP_CLOCK_HAVE_CLOCK_GETTIME +#elif !defined(PSNIP_CLOCK_NO_LIBRT) +#define PSNIP_CLOCK_HAVE_CLOCK_GETTIME +#endif +#endif + +#if defined(_WIN32) +#if !defined(PSNIP_CLOCK_CPU_METHOD) +#define PSNIP_CLOCK_CPU_METHOD PSNIP_CLOCK_METHOD_GETPROCESSTIMES +#endif +#if !defined(PSNIP_CLOCK_MONOTONIC_METHOD) +#define PSNIP_CLOCK_MONOTONIC_METHOD PSNIP_CLOCK_METHOD_QUERYPERFORMANCECOUNTER +#endif +#endif + +#if defined(__MACH__) && !defined(__gnu_hurd__) +#if !defined(PSNIP_CLOCK_MONOTONIC_METHOD) +#define PSNIP_CLOCK_MONOTONIC_METHOD PSNIP_CLOCK_METHOD_MACH_ABSOLUTE_TIME +#endif +#endif + +#if defined(PSNIP_CLOCK_HAVE_CLOCK_GETTIME) +#include +#if !defined(PSNIP_CLOCK_WALL_METHOD) +#if defined(CLOCK_REALTIME_PRECISE) +#define PSNIP_CLOCK_WALL_METHOD PSNIP_CLOCK_METHOD_CLOCK_GETTIME +#define PSNIP_CLOCK_CLOCK_GETTIME_WALL CLOCK_REALTIME_PRECISE +#elif !defined(__sun) +#define PSNIP_CLOCK_WALL_METHOD PSNIP_CLOCK_METHOD_CLOCK_GETTIME +#define PSNIP_CLOCK_CLOCK_GETTIME_WALL CLOCK_REALTIME +#endif +#endif +#if !defined(PSNIP_CLOCK_CPU_METHOD) +#if defined(_POSIX_CPUTIME) || defined(CLOCK_PROCESS_CPUTIME_ID) +#define PSNIP_CLOCK_CPU_METHOD PSNIP_CLOCK_METHOD_CLOCK_GETTIME +#define PSNIP_CLOCK_CLOCK_GETTIME_CPU CLOCK_PROCESS_CPUTIME_ID +#elif defined(CLOCK_VIRTUAL) +#define PSNIP_CLOCK_CPU_METHOD PSNIP_CLOCK_METHOD_CLOCK_GETTIME +#define PSNIP_CLOCK_CLOCK_GETTIME_CPU CLOCK_VIRTUAL +#endif +#endif +#if !defined(PSNIP_CLOCK_MONOTONIC_METHOD) +#if defined(CLOCK_MONOTONIC_RAW) +#define PSNIP_CLOCK_MONOTONIC_METHOD PSNIP_CLOCK_METHOD_CLOCK_GETTIME +#define PSNIP_CLOCK_CLOCK_GETTIME_MONOTONIC CLOCK_MONOTONIC +#elif defined(CLOCK_MONOTONIC_PRECISE) +#define PSNIP_CLOCK_MONOTONIC_METHOD PSNIP_CLOCK_METHOD_CLOCK_GETTIME +#define PSNIP_CLOCK_CLOCK_GETTIME_MONOTONIC CLOCK_MONOTONIC_PRECISE +#elif defined(_POSIX_MONOTONIC_CLOCK) || defined(CLOCK_MONOTONIC) +#define PSNIP_CLOCK_MONOTONIC_METHOD PSNIP_CLOCK_METHOD_CLOCK_GETTIME +#define PSNIP_CLOCK_CLOCK_GETTIME_MONOTONIC CLOCK_MONOTONIC +#endif +#endif +#endif + +#if defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L) +#if !defined(PSNIP_CLOCK_WALL_METHOD) +#define PSNIP_CLOCK_WALL_METHOD PSNIP_CLOCK_METHOD_GETTIMEOFDAY +#endif +#endif + +#if !defined(PSNIP_CLOCK_WALL_METHOD) +#define PSNIP_CLOCK_WALL_METHOD PSNIP_CLOCK_METHOD_TIME +#endif + +#if !defined(PSNIP_CLOCK_CPU_METHOD) +#define PSNIP_CLOCK_CPU_METHOD PSNIP_CLOCK_METHOD_CLOCK +#endif + +/* Primarily here for testing. */ +#if !defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + defined(PSNIP_CLOCK_REQUIRE_MONOTONIC) +#error No monotonic clock found. +#endif + +/* Implementations */ + +#if (defined(PSNIP_CLOCK_CPU_METHOD) && \ + (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME)) || \ + (defined(PSNIP_CLOCK_WALL_METHOD) && \ + (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME)) || \ + (defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME)) || \ + (defined(PSNIP_CLOCK_CPU_METHOD) && \ + (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_CLOCK)) || \ + (defined(PSNIP_CLOCK_WALL_METHOD) && \ + (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_CLOCK)) || \ + (defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_CLOCK)) || \ + (defined(PSNIP_CLOCK_CPU_METHOD) && \ + (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_TIME)) || \ + (defined(PSNIP_CLOCK_WALL_METHOD) && \ + (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_TIME)) || \ + (defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_TIME)) +#include +#endif + +#if (defined(PSNIP_CLOCK_CPU_METHOD) && \ + (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETTIMEOFDAY)) || \ + (defined(PSNIP_CLOCK_WALL_METHOD) && \ + (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_GETTIMEOFDAY)) || \ + (defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_GETTIMEOFDAY)) +#include +#endif + +#if (defined(PSNIP_CLOCK_CPU_METHOD) && \ + (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETPROCESSTIMES)) || \ + (defined(PSNIP_CLOCK_WALL_METHOD) && \ + (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_GETPROCESSTIMES)) || \ + (defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_GETPROCESSTIMES)) || \ + (defined(PSNIP_CLOCK_CPU_METHOD) && \ + (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETTICKCOUNT64)) || \ + (defined(PSNIP_CLOCK_WALL_METHOD) && \ + (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_GETTICKCOUNT64)) || \ + (defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_GETTICKCOUNT64)) +#include +#endif + +#if (defined(PSNIP_CLOCK_CPU_METHOD) && \ + (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETRUSAGE)) || \ + (defined(PSNIP_CLOCK_WALL_METHOD) && \ + (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_GETRUSAGE)) || \ + (defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_GETRUSAGE)) +#include +#include +#endif + +#if (defined(PSNIP_CLOCK_CPU_METHOD) && \ + (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_MACH_ABSOLUTE_TIME)) || \ + (defined(PSNIP_CLOCK_WALL_METHOD) && \ + (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_MACH_ABSOLUTE_TIME)) || \ + (defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_MACH_ABSOLUTE_TIME)) +#include +#include +#include +#endif + +/*** Implementations ***/ + +#define PSNIP_CLOCK_NSEC_PER_SEC ((psnip_uint32_t)(1000000000ULL)) + +#if (defined(PSNIP_CLOCK_CPU_METHOD) && \ + (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME)) || \ + (defined(PSNIP_CLOCK_WALL_METHOD) && \ + (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME)) || \ + (defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME)) +PSNIP_CLOCK__FUNCTION psnip_uint32_t +psnip_clock__clock_getres(clockid_t clk_id) +{ + struct timespec res; + int r; + + r = clock_getres(clk_id, &res); + if (r != 0) + return 0; + + return (psnip_uint32_t)(PSNIP_CLOCK_NSEC_PER_SEC / res.tv_nsec); +} + +PSNIP_CLOCK__FUNCTION int +psnip_clock__clock_gettime(clockid_t clk_id, struct PsnipClockTimespec *res) +{ + struct timespec ts; + + if (clock_gettime(clk_id, &ts) != 0) + return -10; + + res->seconds = (psnip_uint64_t)(ts.tv_sec); + res->nanoseconds = (psnip_uint64_t)(ts.tv_nsec); + + return 0; +} +#endif + +PSNIP_CLOCK__FUNCTION psnip_uint32_t +psnip_clock_wall_get_precision(void) +{ +#if !defined(PSNIP_CLOCK_WALL_METHOD) + return 0; +#elif defined(PSNIP_CLOCK_WALL_METHOD) && \ + PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME + return psnip_clock__clock_getres(PSNIP_CLOCK_CLOCK_GETTIME_WALL); +#elif defined(PSNIP_CLOCK_WALL_METHOD) && \ + PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_GETTIMEOFDAY + return 1000000; +#elif defined(PSNIP_CLOCK_WALL_METHOD) && \ + PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_TIME + return 1; +#else + return 0; +#endif +} + +PSNIP_CLOCK__FUNCTION int +psnip_clock_wall_get_time(struct PsnipClockTimespec *res) +{ + (void)res; + +#if !defined(PSNIP_CLOCK_WALL_METHOD) + return -2; +#elif defined(PSNIP_CLOCK_WALL_METHOD) && \ + PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME + return psnip_clock__clock_gettime(PSNIP_CLOCK_CLOCK_GETTIME_WALL, res); +#elif defined(PSNIP_CLOCK_WALL_METHOD) && \ + PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_TIME + res->seconds = time(NULL); + res->nanoseconds = 0; +#elif defined(PSNIP_CLOCK_WALL_METHOD) && \ + PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_GETTIMEOFDAY + struct timeval tv; + + if (gettimeofday(&tv, NULL) != 0) + return -6; + + res->seconds = tv.tv_sec; + res->nanoseconds = tv.tv_usec * 1000; +#else + return -2; +#endif + + return 0; +} + +PSNIP_CLOCK__FUNCTION psnip_uint32_t +psnip_clock_cpu_get_precision(void) +{ +#if !defined(PSNIP_CLOCK_CPU_METHOD) + return 0; +#elif defined(PSNIP_CLOCK_CPU_METHOD) && \ + PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME + return psnip_clock__clock_getres(PSNIP_CLOCK_CLOCK_GETTIME_CPU); +#elif defined(PSNIP_CLOCK_CPU_METHOD) && \ + PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_CLOCK + return CLOCKS_PER_SEC; +#elif defined(PSNIP_CLOCK_CPU_METHOD) && \ + PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETPROCESSTIMES + return PSNIP_CLOCK_NSEC_PER_SEC / 100; +#else + return 0; +#endif +} + +PSNIP_CLOCK__FUNCTION int +psnip_clock_cpu_get_time(struct PsnipClockTimespec *res) +{ +#if !defined(PSNIP_CLOCK_CPU_METHOD) + (void)res; + return -2; +#elif defined(PSNIP_CLOCK_CPU_METHOD) && \ + PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME + return psnip_clock__clock_gettime(PSNIP_CLOCK_CLOCK_GETTIME_CPU, res); +#elif defined(PSNIP_CLOCK_CPU_METHOD) && \ + PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_CLOCK + clock_t t = clock(); + if (t == ((clock_t)-1)) + return -5; + res->seconds = t / CLOCKS_PER_SEC; + res->nanoseconds = (t % CLOCKS_PER_SEC) * + (PSNIP_CLOCK_NSEC_PER_SEC / CLOCKS_PER_SEC); +#elif defined(PSNIP_CLOCK_CPU_METHOD) && \ + PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETPROCESSTIMES + FILETIME CreationTime, ExitTime, KernelTime, UserTime; + LARGE_INTEGER date, adjust; + + if (!GetProcessTimes(GetCurrentProcess(), &CreationTime, &ExitTime, + &KernelTime, &UserTime)) + return -7; + + /* http://www.frenk.com/2009/12/convert-filetime-to-unix-timestamp/ */ + date.HighPart = UserTime.dwHighDateTime; + date.LowPart = UserTime.dwLowDateTime; + adjust.QuadPart = 11644473600000 * 10000; + date.QuadPart -= adjust.QuadPart; + + res->seconds = date.QuadPart / 10000000; + res->nanoseconds = (date.QuadPart % 10000000) * + (PSNIP_CLOCK_NSEC_PER_SEC / 100); +#elif PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETRUSAGE + struct rusage usage; + if (getrusage(RUSAGE_SELF, &usage) != 0) + return -8; + + res->seconds = usage.ru_utime.tv_sec; + res->nanoseconds = tv.tv_usec * 1000; +#else + (void)res; + return -2; +#endif + + return 0; +} + +PSNIP_CLOCK__FUNCTION psnip_uint32_t +psnip_clock_monotonic_get_precision(void) +{ +#if !defined(PSNIP_CLOCK_MONOTONIC_METHOD) + return 0; +#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME + return psnip_clock__clock_getres(PSNIP_CLOCK_CLOCK_GETTIME_MONOTONIC); +#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_MACH_ABSOLUTE_TIME + static mach_timebase_info_data_t tbi = { + 0, + }; + if (tbi.denom == 0) + mach_timebase_info(&tbi); + return (psnip_uint32_t)(tbi.numer / tbi.denom); +#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_GETTICKCOUNT64 + return 1000; +#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_QUERYPERFORMANCECOUNTER + LARGE_INTEGER Frequency; + QueryPerformanceFrequency(&Frequency); + return (psnip_uint32_t)((Frequency.QuadPart > PSNIP_CLOCK_NSEC_PER_SEC) ? + PSNIP_CLOCK_NSEC_PER_SEC : + Frequency.QuadPart); +#else + return 0; +#endif +} + +PSNIP_CLOCK__FUNCTION int +psnip_clock_monotonic_get_time(struct PsnipClockTimespec *res) +{ +#if !defined(PSNIP_CLOCK_MONOTONIC_METHOD) + (void)res; + return -2; +#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME + return psnip_clock__clock_gettime(PSNIP_CLOCK_CLOCK_GETTIME_MONOTONIC, res); +#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_MACH_ABSOLUTE_TIME + psnip_uint64_t nsec = mach_absolute_time(); + static mach_timebase_info_data_t tbi = { + 0, + }; + if (tbi.denom == 0) + mach_timebase_info(&tbi); + nsec *= ((psnip_uint64_t)tbi.numer) / ((psnip_uint64_t)tbi.denom); + res->seconds = nsec / PSNIP_CLOCK_NSEC_PER_SEC; + res->nanoseconds = nsec % PSNIP_CLOCK_NSEC_PER_SEC; +#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_QUERYPERFORMANCECOUNTER + LARGE_INTEGER t, f; + if (QueryPerformanceCounter(&t) == 0) + return -12; + + QueryPerformanceFrequency(&f); + res->seconds = t.QuadPart / f.QuadPart; + res->nanoseconds = t.QuadPart % f.QuadPart; + if (f.QuadPart > PSNIP_CLOCK_NSEC_PER_SEC) + res->nanoseconds /= f.QuadPart / PSNIP_CLOCK_NSEC_PER_SEC; + else + res->nanoseconds *= PSNIP_CLOCK_NSEC_PER_SEC / f.QuadPart; +#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && \ + PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_GETTICKCOUNT64 + const ULONGLONG msec = GetTickCount64(); + res->seconds = msec / 1000; + res->nanoseconds = sec % 1000; +#else + return -2; +#endif + + return 0; +} + +/* Returns the number of ticks per second for the specified clock. + * For example, a clock with millisecond precision would return 1000, + * and a clock with 1 second (such as the time() function) would + * return 1. + * + * If the requested clock isn't available, it will return 0. + * Hopefully this will be rare, but if it happens to you please let us + * know so we can work on finding a way to support your system. + * + * Note that different clocks on the same system often have a + * different precisions. + */ +PSNIP_CLOCK__FUNCTION psnip_uint32_t +psnip_clock_get_precision(enum PsnipClockType clock_type) +{ + switch (clock_type) { + case PSNIP_CLOCK_TYPE_MONOTONIC: + return psnip_clock_monotonic_get_precision(); + case PSNIP_CLOCK_TYPE_CPU: + return psnip_clock_cpu_get_precision(); + case PSNIP_CLOCK_TYPE_WALL: + return psnip_clock_wall_get_precision(); + } + + PSNIP_CLOCK_UNREACHABLE(); + return 0; +} + +/* Set the provided timespec to the requested time. Returns 0 on + * success, or a negative value on failure. */ +PSNIP_CLOCK__FUNCTION int +psnip_clock_get_time(enum PsnipClockType clock_type, + struct PsnipClockTimespec *res) +{ + assert(res != NULL); + + switch (clock_type) { + case PSNIP_CLOCK_TYPE_MONOTONIC: + return psnip_clock_monotonic_get_time(res); + case PSNIP_CLOCK_TYPE_CPU: + return psnip_clock_cpu_get_time(res); + case PSNIP_CLOCK_TYPE_WALL: + return psnip_clock_wall_get_time(res); + } + + return -1; +} + +#endif /* !defined(PSNIP_CLOCK_H) */ + +static psnip_uint64_t +munit_clock_get_elapsed(struct PsnipClockTimespec *start, + struct PsnipClockTimespec *end) +{ + psnip_uint64_t r = (end->seconds - start->seconds) * PSNIP_CLOCK_NSEC_PER_SEC; + if (end->nanoseconds < start->nanoseconds) { + r -= (start->nanoseconds - end->nanoseconds); + } else { + r += (end->nanoseconds - start->nanoseconds); + } + return r; +} + +#else +#include +#endif /* defined(MUNIT_ENABLE_TIMING) */ + +/*** PRNG stuff ***/ + +/* This is (unless I screwed up, which is entirely possible) the + * version of PCG with 32-bit state. It was chosen because it has a + * small enough state that we should reliably be able to use CAS + * instead of requiring a lock for thread-safety. + * + * If I did screw up, I probably will not bother changing it unless + * there is a significant bias. It's really not important this be + * particularly strong, as long as it is fairly random it's much more + * important that it be reproducible, so bug reports have a better + * chance of being reproducible. */ + +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \ + !defined(__STDC_NO_ATOMICS__) && !defined(__EMSCRIPTEN__) && \ + (!defined(__GNUC_MINOR__) || (__GNUC__ > 4) || \ + (__GNUC__ == 4 && __GNUC_MINOR__ > 8)) +#define HAVE_STDATOMIC +#elif defined(__clang__) +#if __has_extension(c_atomic) +#define HAVE_CLANG_ATOMICS +#endif +#endif + +/* Workaround for http://llvm.org/bugs/show_bug.cgi?id=26911 */ +#if defined(__clang__) && defined(_WIN32) +#undef HAVE_STDATOMIC +#if defined(__c2__) +#undef HAVE_CLANG_ATOMICS +#endif +#endif + +#if defined(_OPENMP) +#define ATOMIC_UINT32_T uint32_t +#define ATOMIC_UINT32_INIT(x) (x) +#elif defined(HAVE_STDATOMIC) +#include +#define ATOMIC_UINT32_T _Atomic uint32_t +#define ATOMIC_UINT32_INIT(x) ATOMIC_VAR_INIT(x) +#elif defined(HAVE_CLANG_ATOMICS) +#define ATOMIC_UINT32_T _Atomic uint32_t +#define ATOMIC_UINT32_INIT(x) (x) +#elif defined(_WIN32) +#define ATOMIC_UINT32_T volatile LONG +#define ATOMIC_UINT32_INIT(x) (x) +#else +#define ATOMIC_UINT32_T volatile uint32_t +#define ATOMIC_UINT32_INIT(x) (x) +#endif + +static ATOMIC_UINT32_T munit_rand_state = ATOMIC_UINT32_INIT(42); + +#if defined(_OPENMP) +static inline void +munit_atomic_store(ATOMIC_UINT32_T *dest, ATOMIC_UINT32_T value) +{ +#pragma omp critical(munit_atomics) + *dest = value; +} + +static inline uint32_t +munit_atomic_load(ATOMIC_UINT32_T *src) +{ + int ret; +#pragma omp critical(munit_atomics) + ret = *src; + return ret; +} + +static inline uint32_t +munit_atomic_cas(ATOMIC_UINT32_T *dest, ATOMIC_UINT32_T *expected, + ATOMIC_UINT32_T desired) +{ + munit_bool ret; + +#pragma omp critical(munit_atomics) + { + if (*dest == *expected) { + *dest = desired; + ret = 1; + } else { + ret = 0; + } + } + + return ret; +} +#elif defined(HAVE_STDATOMIC) +#define munit_atomic_store(dest, value) atomic_store(dest, value) +#define munit_atomic_load(src) atomic_load(src) +#define munit_atomic_cas(dest, expected, value) \ + atomic_compare_exchange_weak(dest, expected, value) +#elif defined(HAVE_CLANG_ATOMICS) +#define munit_atomic_store(dest, value) \ + __c11_atomic_store(dest, value, __ATOMIC_SEQ_CST) +#define munit_atomic_load(src) __c11_atomic_load(src, __ATOMIC_SEQ_CST) +#define munit_atomic_cas(dest, expected, value) \ + __c11_atomic_compare_exchange_weak(dest, expected, value, __ATOMIC_SEQ_CST, \ + __ATOMIC_SEQ_CST) +#elif defined(__GNUC__) && (__GNUC__ > 4) || \ + (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) +#define munit_atomic_store(dest, value) \ + __atomic_store_n(dest, value, __ATOMIC_SEQ_CST) +#define munit_atomic_load(src) __atomic_load_n(src, __ATOMIC_SEQ_CST) +#define munit_atomic_cas(dest, expected, value) \ + __atomic_compare_exchange_n(dest, expected, value, 1, __ATOMIC_SEQ_CST, \ + __ATOMIC_SEQ_CST) +#elif defined(__GNUC__) && (__GNUC__ >= 4) +#define munit_atomic_store(dest, value) \ + do { \ + *(dest) = (value); \ + } while (0) +#define munit_atomic_load(src) (*(src)) +#define munit_atomic_cas(dest, expected, value) \ + __sync_bool_compare_and_swap(dest, *expected, value) +#elif defined(_WIN32) /* Untested */ +#define munit_atomic_store(dest, value) \ + do { \ + *(dest) = (value); \ + } while (0) +#define munit_atomic_load(src) (*(src)) +#define munit_atomic_cas(dest, expected, value) \ + InterlockedCompareExchange((dest), (value), *(expected)) +#else +#warning No atomic implementation, PRNG will not be thread-safe +#define munit_atomic_store(dest, value) \ + do { \ + *(dest) = (value); \ + } while (0) +#define munit_atomic_load(src) (*(src)) +static inline munit_bool +munit_atomic_cas(ATOMIC_UINT32_T *dest, ATOMIC_UINT32_T *expected, + ATOMIC_UINT32_T desired) +{ + if (*dest == *expected) { + *dest = desired; + return 1; + } else { + return 0; + } +} +#endif + +#define MUNIT_PRNG_MULTIPLIER (747796405U) +#define MUNIT_PRNG_INCREMENT (1729U) + +static munit_uint32_t +munit_rand_next_state(munit_uint32_t state) +{ + return state * MUNIT_PRNG_MULTIPLIER + MUNIT_PRNG_INCREMENT; +} + +static munit_uint32_t +munit_rand_from_state(munit_uint32_t state) +{ + munit_uint32_t res = ((state >> ((state >> 28) + 4)) ^ state) * (277803737U); + res ^= res >> 22; + return res; +} + +void +munit_rand_seed(munit_uint32_t seed) +{ + munit_uint32_t state = munit_rand_next_state(seed + MUNIT_PRNG_INCREMENT); + munit_atomic_store(&munit_rand_state, state); +} + +static munit_uint32_t +munit_rand_generate_seed(void) +{ + munit_uint32_t seed, state; +#if defined(MUNIT_ENABLE_TIMING) + struct PsnipClockTimespec wc = { + 0, + }; + + psnip_clock_get_time(PSNIP_CLOCK_TYPE_WALL, &wc); + seed = (munit_uint32_t)wc.nanoseconds; +#else + seed = (munit_uint32_t)time(NULL); +#endif + + state = munit_rand_next_state(seed + MUNIT_PRNG_INCREMENT); + return munit_rand_from_state(state); +} + +static munit_uint32_t +munit_rand_state_uint32(munit_uint32_t *state) +{ + const munit_uint32_t old = *state; + *state = munit_rand_next_state(old); + return munit_rand_from_state(old); +} + +munit_uint32_t +munit_rand_uint32(void) +{ + munit_uint32_t old, state; + + do { + old = munit_atomic_load(&munit_rand_state); + state = munit_rand_next_state(old); + } while (!munit_atomic_cas(&munit_rand_state, &old, state)); + + return munit_rand_from_state(old); +} + +static void +munit_rand_state_memory(munit_uint32_t *state, size_t size, + munit_uint8_t data[MUNIT_ARRAY_PARAM(size)]) +{ + size_t members_remaining = size / sizeof(munit_uint32_t); + size_t bytes_remaining = size % sizeof(munit_uint32_t); + munit_uint8_t *b = data; + munit_uint32_t rv; + while (members_remaining-- > 0) { + rv = munit_rand_state_uint32(state); + memcpy(b, &rv, sizeof(munit_uint32_t)); + b += sizeof(munit_uint32_t); + } + if (bytes_remaining != 0) { + rv = munit_rand_state_uint32(state); + memcpy(b, &rv, bytes_remaining); + } +} + +void +munit_rand_memory(size_t size, munit_uint8_t data[MUNIT_ARRAY_PARAM(size)]) +{ + munit_uint32_t old, state; + + do { + state = old = munit_atomic_load(&munit_rand_state); + munit_rand_state_memory(&state, size, data); + } while (!munit_atomic_cas(&munit_rand_state, &old, state)); +} + +static munit_uint32_t +munit_rand_state_at_most(munit_uint32_t *state, munit_uint32_t salt, + munit_uint32_t max) +{ + /* We want (UINT32_MAX + 1) % max, which in unsigned arithmetic is the same + * as (UINT32_MAX + 1 - max) % max = -max % max. We compute -max using not + * to avoid compiler warnings. + */ + const munit_uint32_t min = (~max + 1U) % max; + munit_uint32_t x; + + if (max == (~((munit_uint32_t)0U))) + return munit_rand_state_uint32(state) ^ salt; + + max++; + + do { + x = munit_rand_state_uint32(state) ^ salt; + } while (x < min); + + return x % max; +} + +static munit_uint32_t +munit_rand_at_most(munit_uint32_t salt, munit_uint32_t max) +{ + munit_uint32_t old, state; + munit_uint32_t retval; + + do { + state = old = munit_atomic_load(&munit_rand_state); + retval = munit_rand_state_at_most(&state, salt, max); + } while (!munit_atomic_cas(&munit_rand_state, &old, state)); + + return retval; +} + +int +munit_rand_int_range(int min, int max) +{ + munit_uint64_t range = (munit_uint64_t)max - (munit_uint64_t)min; + + if (min > max) + return munit_rand_int_range(max, min); + + if (range > (~((munit_uint32_t)0U))) + range = (~((munit_uint32_t)0U)); + + return min + munit_rand_at_most(0, (munit_uint32_t)range); +} + +double +munit_rand_double(void) +{ + munit_uint32_t old, state; + double retval = 0.0; + + do { + state = old = munit_atomic_load(&munit_rand_state); + + /* See http://mumble.net/~campbell/tmp/random_real.c for how to do + * this right. Patches welcome if you feel that this is too + * biased. */ + retval = munit_rand_state_uint32(&state) / ((~((munit_uint32_t)0U)) + 1.0); + } while (!munit_atomic_cas(&munit_rand_state, &old, state)); + + return retval; +} + +/*** Test suite handling ***/ + +typedef struct { + unsigned int successful; + unsigned int skipped; + unsigned int failed; + unsigned int errored; +#if defined(MUNIT_ENABLE_TIMING) + munit_uint64_t cpu_clock; + munit_uint64_t wall_clock; +#endif +} MunitReport; + +typedef struct { + const char *prefix; + const MunitSuite *suite; + const char **tests; + munit_uint32_t seed; + unsigned int iterations; + MunitParameter *parameters; + munit_bool single_parameter_mode; + void *user_data; + MunitReport report; + munit_bool colorize; + munit_bool fork; + munit_bool show_stderr; + munit_bool fatal_failures; +} MunitTestRunner; + +const char * +munit_parameters_get(const MunitParameter params[], const char *key) +{ + const MunitParameter *param; + + for (param = params; param != NULL && param->name != NULL; param++) + if (strcmp(param->name, key) == 0) + return param->value; + return NULL; +} + +#if defined(MUNIT_ENABLE_TIMING) +static void +munit_print_time(FILE *fp, munit_uint64_t nanoseconds) +{ + fprintf(fp, "%" MUNIT_TEST_TIME_FORMAT, + ((double)nanoseconds) / ((double)PSNIP_CLOCK_NSEC_PER_SEC)); +} +#endif + +/* Add a paramter to an array of parameters. */ +static MunitResult +munit_parameters_add(size_t *params_size, + MunitParameter *params[MUNIT_ARRAY_PARAM(*params_size)], char *name, + char *value) +{ + *params = realloc(*params, sizeof(MunitParameter) * (*params_size + 2)); + if (*params == NULL) + return MUNIT_ERROR; + + (*params)[*params_size].name = name; + (*params)[*params_size].value = value; + (*params_size)++; + (*params)[*params_size].name = NULL; + (*params)[*params_size].value = NULL; + + return MUNIT_OK; +} + +/* Concatenate two strings, but just return one of the components + * unaltered if the other is NULL or "". */ +static char * +munit_maybe_concat(size_t *len, char *prefix, char *suffix) +{ + char *res; + size_t res_l; + const size_t prefix_l = prefix != NULL ? strlen(prefix) : 0; + const size_t suffix_l = suffix != NULL ? strlen(suffix) : 0; + if (prefix_l == 0 && suffix_l == 0) { + res = NULL; + res_l = 0; + } else if (prefix_l == 0 && suffix_l != 0) { + res = suffix; + res_l = suffix_l; + } else if (prefix_l != 0 && suffix_l == 0) { + res = prefix; + res_l = prefix_l; + } else { + res_l = prefix_l + suffix_l; + res = malloc(res_l + 1); + memcpy(res, prefix, prefix_l); + memcpy(res + prefix_l, suffix, suffix_l); + res[res_l] = 0; + } + + if (len != NULL) + *len = res_l; + + return res; +} + +/* Possbily free a string returned by munit_maybe_concat. */ +static void +munit_maybe_free_concat(char *s, const char *prefix, const char *suffix) +{ + if (prefix != s && suffix != s) + free(s); +} + +/* Cheap string hash function, just used to salt the PRNG. */ +static munit_uint32_t +munit_str_hash(const char *name) +{ + const char *p; + munit_uint32_t h = 5381U; + + for (p = name; *p != '\0'; p++) + h = (h << 5) + h + *p; + + return h; +} + +static void +munit_splice(int from, int to) +{ + munit_uint8_t buf[1024]; +#if !defined(_WIN32) + ssize_t len; + ssize_t bytes_written; + ssize_t write_res; +#else + int len; + int bytes_written; + int write_res; +#endif + do { + len = read(from, buf, sizeof(buf)); + if (len > 0) { + bytes_written = 0; + do { + write_res = write(to, buf + bytes_written, len - bytes_written); + if (write_res < 0) + break; + bytes_written += write_res; + } while (bytes_written < len); + } else + break; + } while (1); +} + +/* This is the part that should be handled in the child process */ +static MunitResult +munit_test_runner_exec(MunitTestRunner *runner, const MunitTest *test, + const MunitParameter params[], MunitReport *report) +{ + unsigned int iterations = runner->iterations; + MunitResult result = MUNIT_FAIL; +#if defined(MUNIT_ENABLE_TIMING) + struct PsnipClockTimespec wall_clock_begin = { 0, }, wall_clock_end = { 0, }; + struct PsnipClockTimespec cpu_clock_begin = { 0, }, cpu_clock_end = { 0, }; +#endif + unsigned int i = 0; + + if ((test->options & MUNIT_TEST_OPTION_SINGLE_ITERATION) == + MUNIT_TEST_OPTION_SINGLE_ITERATION) + iterations = 1; + else if (iterations == 0) + iterations = runner->suite->iterations; + + munit_rand_seed(runner->seed); + + do { + void *data = (test->setup == NULL) ? runner->user_data : + test->setup(params, runner->user_data); + +#if defined(MUNIT_ENABLE_TIMING) + psnip_clock_get_time(PSNIP_CLOCK_TYPE_WALL, &wall_clock_begin); + psnip_clock_get_time(PSNIP_CLOCK_TYPE_CPU, &cpu_clock_begin); +#endif + + result = test->test(params, data); + +#if defined(MUNIT_ENABLE_TIMING) + psnip_clock_get_time(PSNIP_CLOCK_TYPE_WALL, &wall_clock_end); + psnip_clock_get_time(PSNIP_CLOCK_TYPE_CPU, &cpu_clock_end); +#endif + + if (test->tear_down != NULL) + test->tear_down(data); + + if (MUNIT_LIKELY(result == MUNIT_OK)) { + report->successful++; +#if defined(MUNIT_ENABLE_TIMING) + report->wall_clock += munit_clock_get_elapsed(&wall_clock_begin, + &wall_clock_end); + report->cpu_clock += munit_clock_get_elapsed(&cpu_clock_begin, + &cpu_clock_end); +#endif + } else { + switch ((int)result) { + case MUNIT_SKIP: + report->skipped++; + break; + case MUNIT_FAIL: + report->failed++; + break; + case MUNIT_ERROR: + report->errored++; + break; + default: + break; + } + break; + } + } while (++i < iterations); + + return result; +} + +#if defined(MUNIT_EMOTICON) +#define MUNIT_RESULT_STRING_OK ":)" +#define MUNIT_RESULT_STRING_SKIP ":|" +#define MUNIT_RESULT_STRING_FAIL ":(" +#define MUNIT_RESULT_STRING_ERROR ":o" +#define MUNIT_RESULT_STRING_TODO ":/" +#else +#define MUNIT_RESULT_STRING_OK "OK " +#define MUNIT_RESULT_STRING_SKIP "SKIP " +#define MUNIT_RESULT_STRING_FAIL "FAIL " +#define MUNIT_RESULT_STRING_ERROR "ERROR" +#define MUNIT_RESULT_STRING_TODO "TODO " +#endif + +static void +munit_test_runner_print_color(const MunitTestRunner *runner, const char *string, + char color) +{ + if (runner->colorize) + fprintf(MUNIT_OUTPUT_FILE, "\x1b[3%cm%s\x1b[39m", color, string); + else + fputs(string, MUNIT_OUTPUT_FILE); +} + +#if !defined(MUNIT_NO_BUFFER) +static int +munit_replace_stderr(FILE *stderr_buf) +{ + if (stderr_buf != NULL) { + const int orig_stderr = dup(STDERR_FILENO); + + int errfd = fileno(stderr_buf); + if (MUNIT_UNLIKELY(errfd == -1)) { + exit(EXIT_FAILURE); + } + + dup2(errfd, STDERR_FILENO); + + return orig_stderr; + } + + return -1; +} + +static void +munit_restore_stderr(int orig_stderr) +{ + if (orig_stderr != -1) { + dup2(orig_stderr, STDERR_FILENO); + close(orig_stderr); + } +} +#endif /* !defined(MUNIT_NO_BUFFER) */ + +/* Run a test with the specified parameters. */ +static void +munit_test_runner_run_test_with_params(MunitTestRunner *runner, + const MunitTest *test, const MunitParameter params[]) +{ + MunitResult result = MUNIT_OK; + MunitReport report = { + 0, + 0, + 0, + 0, +#if defined(MUNIT_ENABLE_TIMING) + 0, + 0 +#endif + }; + unsigned int output_l; + munit_bool first; + const MunitParameter *param; + FILE *stderr_buf; +#if !defined(MUNIT_NO_FORK) + int pipefd[2]; + pid_t fork_pid; + int orig_stderr; + ssize_t bytes_written = 0; + ssize_t write_res; + ssize_t bytes_read = 0; + ssize_t read_res; + int status = 0; + pid_t changed_pid; +#endif + + if (params != NULL) { + output_l = 2; + fputs(" ", MUNIT_OUTPUT_FILE); + first = 1; + for (param = params; param != NULL && param->name != NULL; param++) { + if (!first) { + fputs(", ", MUNIT_OUTPUT_FILE); + output_l += 2; + } else { + first = 0; + } + + output_l += fprintf(MUNIT_OUTPUT_FILE, "%s=%s", param->name, + param->value); + } + while (output_l++ < MUNIT_TEST_NAME_LEN) { + fputc(' ', MUNIT_OUTPUT_FILE); + } + } + + fflush(MUNIT_OUTPUT_FILE); + + stderr_buf = NULL; +#if !defined(_WIN32) || defined(__MINGW32__) + stderr_buf = tmpfile(); +#else + tmpfile_s(&stderr_buf); +#endif + if (stderr_buf == NULL) { + munit_log_errno(MUNIT_LOG_ERROR, stderr, + "unable to create buffer for stderr"); + result = MUNIT_ERROR; + goto print_result; + } + +#if !defined(MUNIT_NO_FORK) + if (runner->fork) { + pipefd[0] = -1; + pipefd[1] = -1; + if (pipe(pipefd) != 0) { + munit_log_errno(MUNIT_LOG_ERROR, stderr, "unable to create pipe"); + result = MUNIT_ERROR; + goto print_result; + } + + fork_pid = fork(); + if (fork_pid == 0) { + close(pipefd[0]); + + orig_stderr = munit_replace_stderr(stderr_buf); + munit_test_runner_exec(runner, test, params, &report); + + /* Note that we don't restore stderr. This is so we can buffer + * things written to stderr later on (such as by + * asan/tsan/ubsan, valgrind, etc.) */ + close(orig_stderr); + + do { + write_res = write(pipefd[1], + ((munit_uint8_t *)(&report)) + bytes_written, + sizeof(report) - bytes_written); + if (write_res < 0) { + if (stderr_buf != NULL) { + munit_log_errno(MUNIT_LOG_ERROR, stderr, "unable to write to pipe"); + } + exit(EXIT_FAILURE); + } + bytes_written += write_res; + } while ((size_t)bytes_written < sizeof(report)); + + if (stderr_buf != NULL) + fclose(stderr_buf); + close(pipefd[1]); + + exit(EXIT_SUCCESS); + } else if (fork_pid == -1) { + close(pipefd[0]); + close(pipefd[1]); + if (stderr_buf != NULL) { + munit_log_errno(MUNIT_LOG_ERROR, stderr, "unable to fork"); + } + report.errored++; + result = MUNIT_ERROR; + } else { + close(pipefd[1]); + do { + read_res = read(pipefd[0], ((munit_uint8_t *)(&report)) + bytes_read, + sizeof(report) - bytes_read); + if (read_res < 1) + break; + bytes_read += read_res; + } while (bytes_read < (ssize_t)sizeof(report)); + + changed_pid = waitpid(fork_pid, &status, 0); + + if (MUNIT_LIKELY(changed_pid == fork_pid) && + MUNIT_LIKELY(WIFEXITED(status))) { + if (bytes_read != sizeof(report)) { + munit_logf_internal(MUNIT_LOG_ERROR, stderr_buf, + "child exited unexpectedly with status %d", WEXITSTATUS(status)); + report.errored++; + } else if (WEXITSTATUS(status) != EXIT_SUCCESS) { + munit_logf_internal(MUNIT_LOG_ERROR, stderr_buf, + "child exited with status %d", WEXITSTATUS(status)); + report.errored++; + } + } else { + if (WIFSIGNALED(status)) { +#if defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 700) + munit_logf_internal(MUNIT_LOG_ERROR, stderr_buf, + "child killed by signal %d (%s)", WTERMSIG(status), + strsignal(WTERMSIG(status))); +#else + munit_logf_internal(MUNIT_LOG_ERROR, stderr_buf, + "child killed by signal %d", WTERMSIG(status)); +#endif + } else if (WIFSTOPPED(status)) { + munit_logf_internal(MUNIT_LOG_ERROR, stderr_buf, + "child stopped by signal %d", WSTOPSIG(status)); + } + report.errored++; + } + + close(pipefd[0]); + waitpid(fork_pid, NULL, 0); + } + } else +#endif + { +#if !defined(MUNIT_NO_BUFFER) + const volatile int orig_stderr = munit_replace_stderr(stderr_buf); +#endif + +#if defined(MUNIT_THREAD_LOCAL) + if (MUNIT_UNLIKELY(setjmp(munit_error_jmp_buf) != 0)) { + result = MUNIT_FAIL; + report.failed++; + } else { + munit_error_jmp_buf_valid = 1; + result = munit_test_runner_exec(runner, test, params, &report); + } +#else + result = munit_test_runner_exec(runner, test, params, &report); +#endif + +#if !defined(MUNIT_NO_BUFFER) + munit_restore_stderr(orig_stderr); +#endif + + /* Here just so that the label is used on Windows and we don't get + * a warning */ + goto print_result; + } + +print_result: + + fputs("[ ", MUNIT_OUTPUT_FILE); + if ((test->options & MUNIT_TEST_OPTION_TODO) == MUNIT_TEST_OPTION_TODO) { + if (report.failed != 0 || report.errored != 0 || report.skipped != 0) { + munit_test_runner_print_color(runner, MUNIT_RESULT_STRING_TODO, '3'); + result = MUNIT_OK; + } else { + munit_test_runner_print_color(runner, MUNIT_RESULT_STRING_ERROR, '1'); + if (MUNIT_LIKELY(stderr_buf != NULL)) + munit_log_internal(MUNIT_LOG_ERROR, stderr_buf, + "Test marked TODO, but was successful."); + runner->report.failed++; + result = MUNIT_ERROR; + } + } else if (report.failed > 0) { + munit_test_runner_print_color(runner, MUNIT_RESULT_STRING_FAIL, '1'); + runner->report.failed++; + result = MUNIT_FAIL; + } else if (report.errored > 0) { + munit_test_runner_print_color(runner, MUNIT_RESULT_STRING_ERROR, '1'); + runner->report.errored++; + result = MUNIT_ERROR; + } else if (report.skipped > 0) { + munit_test_runner_print_color(runner, MUNIT_RESULT_STRING_SKIP, '3'); + runner->report.skipped++; + result = MUNIT_SKIP; + } else if (report.successful > 1) { + munit_test_runner_print_color(runner, MUNIT_RESULT_STRING_OK, '2'); +#if defined(MUNIT_ENABLE_TIMING) + fputs(" ] [ ", MUNIT_OUTPUT_FILE); + munit_print_time(MUNIT_OUTPUT_FILE, report.wall_clock / report.successful); + fputs(" / ", MUNIT_OUTPUT_FILE); + munit_print_time(MUNIT_OUTPUT_FILE, report.cpu_clock / report.successful); + fprintf(MUNIT_OUTPUT_FILE, + " CPU ]\n %-" MUNIT_XSTRINGIFY(MUNIT_TEST_NAME_LEN) "s Total: [ ", ""); + munit_print_time(MUNIT_OUTPUT_FILE, report.wall_clock); + fputs(" / ", MUNIT_OUTPUT_FILE); + munit_print_time(MUNIT_OUTPUT_FILE, report.cpu_clock); + fputs(" CPU", MUNIT_OUTPUT_FILE); +#endif + runner->report.successful++; + result = MUNIT_OK; + } else if (report.successful > 0) { + munit_test_runner_print_color(runner, MUNIT_RESULT_STRING_OK, '2'); +#if defined(MUNIT_ENABLE_TIMING) + fputs(" ] [ ", MUNIT_OUTPUT_FILE); + munit_print_time(MUNIT_OUTPUT_FILE, report.wall_clock); + fputs(" / ", MUNIT_OUTPUT_FILE); + munit_print_time(MUNIT_OUTPUT_FILE, report.cpu_clock); + fputs(" CPU", MUNIT_OUTPUT_FILE); +#endif + runner->report.successful++; + result = MUNIT_OK; + } + fputs(" ]\n", MUNIT_OUTPUT_FILE); + + if (stderr_buf != NULL) { + if (result == MUNIT_FAIL || result == MUNIT_ERROR || runner->show_stderr) { + fflush(MUNIT_OUTPUT_FILE); + + rewind(stderr_buf); + munit_splice(fileno(stderr_buf), STDERR_FILENO); + + fflush(stderr); + } + + fclose(stderr_buf); + } +} + +static void +munit_test_runner_run_test_wild(MunitTestRunner *runner, const MunitTest *test, + const char *test_name, MunitParameter *params, MunitParameter *p) +{ + const MunitParameterEnum *pe; + char **values; + MunitParameter *next; + + for (pe = test->parameters; pe != NULL && pe->name != NULL; pe++) { + if (p->name == pe->name) + break; + } + + if (pe == NULL) + return; + + for (values = pe->values; *values != NULL; values++) { + next = p + 1; + p->value = *values; + if (next->name == NULL) { + munit_test_runner_run_test_with_params(runner, test, params); + } else { + munit_test_runner_run_test_wild(runner, test, test_name, params, next); + } + if (runner->fatal_failures && + (runner->report.failed != 0 || runner->report.errored != 0)) + break; + } +} + +/* Run a single test, with every combination of parameters + * requested. */ +static void +munit_test_runner_run_test(MunitTestRunner *runner, const MunitTest *test, + const char *prefix) +{ + char *test_name = munit_maybe_concat(NULL, (char *)prefix, + (char *)test->name); + /* The array of parameters to pass to + * munit_test_runner_run_test_with_params */ + MunitParameter *params = NULL; + size_t params_l = 0; + /* Wildcard parameters are parameters which have possible values + * specified in the test, but no specific value was passed to the + * CLI. That means we want to run the test once for every + * possible combination of parameter values or, if --single was + * passed to the CLI, a single time with a random set of + * parameters. */ + MunitParameter *wild_params = NULL; + size_t wild_params_l = 0; + const MunitParameterEnum *pe; + const MunitParameter *cli_p; + munit_bool filled; + unsigned int possible; + char **vals; + size_t first_wild; + const MunitParameter *wp; + int pidx; + + munit_rand_seed(runner->seed); + + fprintf(MUNIT_OUTPUT_FILE, "%-" MUNIT_XSTRINGIFY(MUNIT_TEST_NAME_LEN) "s", + test_name); + + if (test->parameters == NULL) { + /* No parameters. Simple, nice. */ + munit_test_runner_run_test_with_params(runner, test, NULL); + } else { + fputc('\n', MUNIT_OUTPUT_FILE); + + for (pe = test->parameters; pe != NULL && pe->name != NULL; pe++) { + /* Did we received a value for this parameter from the CLI? */ + filled = 0; + for (cli_p = runner->parameters; cli_p != NULL && cli_p->name != NULL; + cli_p++) { + if (strcmp(cli_p->name, pe->name) == 0) { + if (MUNIT_UNLIKELY(munit_parameters_add(¶ms_l, ¶ms, pe->name, + cli_p->value) != MUNIT_OK)) + goto cleanup; + filled = 1; + break; + } + } + if (filled) + continue; + + /* Nothing from CLI, is the enum NULL/empty? We're not a + * fuzzer… */ + if (pe->values == NULL || pe->values[0] == NULL) + continue; + + /* If --single was passed to the CLI, choose a value from the + * list of possibilities randomly. */ + if (runner->single_parameter_mode) { + possible = 0; + for (vals = pe->values; *vals != NULL; vals++) + possible++; + /* We want the tests to be reproducible, even if you're only + * running a single test, but we don't want every test with + * the same number of parameters to choose the same parameter + * number, so use the test name as a primitive salt. */ + pidx = munit_rand_at_most(munit_str_hash(test_name), possible - 1); + if (MUNIT_UNLIKELY(munit_parameters_add(¶ms_l, ¶ms, pe->name, + pe->values[pidx]) != MUNIT_OK)) + goto cleanup; + } else { + /* We want to try every permutation. Put in a placeholder + * entry, we'll iterate through them later. */ + if (MUNIT_UNLIKELY(munit_parameters_add(&wild_params_l, &wild_params, + pe->name, NULL) != MUNIT_OK)) + goto cleanup; + } + } + + if (wild_params_l != 0) { + first_wild = params_l; + for (wp = wild_params; wp != NULL && wp->name != NULL; wp++) { + for (pe = test->parameters; + pe != NULL && pe->name != NULL && pe->values != NULL; pe++) { + if (strcmp(wp->name, pe->name) == 0) { + if (MUNIT_UNLIKELY(munit_parameters_add(¶ms_l, ¶ms, + pe->name, pe->values[0]) != MUNIT_OK)) + goto cleanup; + } + } + } + + munit_test_runner_run_test_wild(runner, test, test_name, params, + params + first_wild); + } else { + munit_test_runner_run_test_with_params(runner, test, params); + } + + cleanup: + free(params); + free(wild_params); + } + + munit_maybe_free_concat(test_name, prefix, test->name); +} + +/* Recurse through the suite and run all the tests. If a list of + * tests to run was provied on the command line, run only those + * tests. */ +static void +munit_test_runner_run_suite(MunitTestRunner *runner, const MunitSuite *suite, + const char *prefix) +{ + size_t pre_l; + char *pre = munit_maybe_concat(&pre_l, (char *)prefix, (char *)suite->prefix); + const MunitTest *test; + const char **test_name; + const MunitSuite *child_suite; + + /* Run the tests. */ + for (test = suite->tests; test != NULL && test->test != NULL; test++) { + if (runner->tests != NULL) { /* Specific tests were requested on the CLI */ + for (test_name = runner->tests; test_name != NULL && *test_name != NULL; + test_name++) { + if ((pre_l == 0 || strncmp(pre, *test_name, pre_l) == 0) && + strncmp(test->name, *test_name + pre_l, strlen(*test_name + pre_l)) == + 0) { + munit_test_runner_run_test(runner, test, pre); + if (runner->fatal_failures && + (runner->report.failed != 0 || runner->report.errored != 0)) + goto cleanup; + } + } + } else { /* Run all tests */ + munit_test_runner_run_test(runner, test, pre); + } + } + + if (runner->fatal_failures && + (runner->report.failed != 0 || runner->report.errored != 0)) + goto cleanup; + + /* Run any child suites. */ + for (child_suite = suite->suites; + child_suite != NULL && child_suite->prefix != NULL; child_suite++) { + munit_test_runner_run_suite(runner, child_suite, pre); + } + +cleanup: + + munit_maybe_free_concat(pre, prefix, suite->prefix); +} + +static void +munit_test_runner_run(MunitTestRunner *runner) +{ + munit_test_runner_run_suite(runner, runner->suite, NULL); +} + +static void +munit_print_help(int argc, char *const argv[MUNIT_ARRAY_PARAM(argc + 1)], + void *user_data, const MunitArgument arguments[]) +{ + const MunitArgument *arg; + (void)argc; + + printf("USAGE: %s [OPTIONS...] [TEST...]\n\n", argv[0]); + puts( + " --seed SEED\n" + " Value used to seed the PRNG. Must be a 32-bit integer in decimal\n" + " notation with no separators (commas, decimals, spaces, etc.), or\n" + " hexidecimal prefixed by \"0x\".\n" + " --iterations N\n" + " Run each test N times. 0 means the default number.\n" + " --param name value\n" + " A parameter key/value pair which will be passed to any test with\n" + " takes a parameter of that name. If not provided, the test will be\n" + " run once for each possible parameter value.\n" + " --list Write a list of all available tests.\n" + " --list-params\n" + " Write a list of all available tests and their possible parameters.\n" + " --single Run each parameterized test in a single configuration instead of\n" + " every possible combination\n" + " --log-visible debug|info|warning|error\n" + " --log-fatal debug|info|warning|error\n" + " Set the level at which messages of different severities are visible,\n" + " or cause the test to terminate.\n" +#if !defined(MUNIT_NO_FORK) + " --no-fork Do not execute tests in a child process. If this option is supplied\n" + " and a test crashes (including by failing an assertion), no further\n" + " tests will be performed.\n" +#endif + " --fatal-failures\n" + " Stop executing tests as soon as a failure is found.\n" + " --show-stderr\n" + " Show data written to stderr by the tests, even if the test succeeds.\n" + " --color auto|always|never\n" + " Colorize (or don't) the output.\n" + /* 12345678901234567890123456789012345678901234567890123456789012345678901234567890 + */ + " --help Print this help message and exit.\n"); +#if defined(MUNIT_NL_LANGINFO) + setlocale(LC_ALL, ""); + fputs((strcasecmp("UTF-8", nl_langinfo(CODESET)) == 0) ? "µnit" : "munit", + stdout); +#else + puts("munit"); +#endif + printf(" %d.%d.%d\n" + "Full documentation at: https://nemequ.github.io/munit/\n", + (MUNIT_CURRENT_VERSION >> 16) & 0xff, (MUNIT_CURRENT_VERSION >> 8) & 0xff, + (MUNIT_CURRENT_VERSION >> 0) & 0xff); + for (arg = arguments; arg != NULL && arg->name != NULL; arg++) + arg->write_help(arg, user_data); +} + +static const MunitArgument * +munit_arguments_find(const MunitArgument arguments[], const char *name) +{ + const MunitArgument *arg; + + for (arg = arguments; arg != NULL && arg->name != NULL; arg++) + if (strcmp(arg->name, name) == 0) + return arg; + + return NULL; +} + +static void +munit_suite_list_tests(const MunitSuite *suite, munit_bool show_params, + const char *prefix) +{ + size_t pre_l; + char *pre = munit_maybe_concat(&pre_l, (char *)prefix, (char *)suite->prefix); + const MunitTest *test; + const MunitParameterEnum *params; + munit_bool first; + char **val; + const MunitSuite *child_suite; + + for (test = suite->tests; test != NULL && test->name != NULL; test++) { + if (pre != NULL) + fputs(pre, stdout); + puts(test->name); + + if (show_params) { + for (params = test->parameters; params != NULL && params->name != NULL; + params++) { + fprintf(stdout, " - %s: ", params->name); + if (params->values == NULL) { + puts("Any"); + } else { + first = 1; + for (val = params->values; *val != NULL; val++) { + if (!first) { + fputs(", ", stdout); + } else { + first = 0; + } + fputs(*val, stdout); + } + putc('\n', stdout); + } + } + } + } + + for (child_suite = suite->suites; + child_suite != NULL && child_suite->prefix != NULL; child_suite++) { + munit_suite_list_tests(child_suite, show_params, pre); + } + + munit_maybe_free_concat(pre, prefix, suite->prefix); +} + +static munit_bool +munit_stream_supports_ansi(FILE *stream) +{ +#if !defined(_WIN32) + return isatty(fileno(stream)); +#else + +#if !defined(__MINGW32__) + size_t ansicon_size = 0; +#endif + + if (isatty(fileno(stream))) { +#if !defined(__MINGW32__) + getenv_s(&ansicon_size, NULL, 0, "ANSICON"); + return ansicon_size != 0; +#else + return getenv("ANSICON") != NULL; +#endif + } + return 0; +#endif +} + +int +munit_suite_main_custom(const MunitSuite *suite, void *user_data, int argc, + char *const argv[MUNIT_ARRAY_PARAM(argc + 1)], + const MunitArgument arguments[]) +{ + int result = EXIT_FAILURE; + MunitTestRunner runner; + size_t parameters_size = 0; + size_t tests_size = 0; + int arg; + + char *envptr; + unsigned long ts; + char *endptr; + unsigned long long iterations; + MunitLogLevel level; + const MunitArgument *argument; + const char **runner_tests; + unsigned int tests_run; + unsigned int tests_total; + + runner.prefix = NULL; + runner.suite = NULL; + runner.tests = NULL; + runner.seed = 0; + runner.iterations = 0; + runner.parameters = NULL; + runner.single_parameter_mode = 0; + runner.user_data = NULL; + + runner.report.successful = 0; + runner.report.skipped = 0; + runner.report.failed = 0; + runner.report.errored = 0; +#if defined(MUNIT_ENABLE_TIMING) + runner.report.cpu_clock = 0; + runner.report.wall_clock = 0; +#endif + + runner.colorize = 0; +#if !defined(_WIN32) + runner.fork = 1; +#else + runner.fork = 0; +#endif + runner.show_stderr = 0; + runner.fatal_failures = 0; + runner.suite = suite; + runner.user_data = user_data; + runner.seed = munit_rand_generate_seed(); + runner.colorize = munit_stream_supports_ansi(MUNIT_OUTPUT_FILE); + + for (arg = 1; arg < argc; arg++) { + if (strncmp("--", argv[arg], 2) == 0) { + if (strcmp("seed", argv[arg] + 2) == 0) { + if (arg + 1 >= argc) { + munit_logf_internal(MUNIT_LOG_ERROR, stderr, + "%s requires an argument", argv[arg]); + goto cleanup; + } + + envptr = argv[arg + 1]; + ts = strtoul(argv[arg + 1], &envptr, 0); + if (*envptr != '\0' || ts > (~((munit_uint32_t)0U))) { + munit_logf_internal(MUNIT_LOG_ERROR, stderr, + "invalid value ('%s') passed to %s", argv[arg + 1], argv[arg]); + goto cleanup; + } + runner.seed = (munit_uint32_t)ts; + + arg++; + } else if (strcmp("iterations", argv[arg] + 2) == 0) { + if (arg + 1 >= argc) { + munit_logf_internal(MUNIT_LOG_ERROR, stderr, + "%s requires an argument", argv[arg]); + goto cleanup; + } + + endptr = argv[arg + 1]; + iterations = strtoul(argv[arg + 1], &endptr, 0); + if (*endptr != '\0' || iterations > UINT_MAX) { + munit_logf_internal(MUNIT_LOG_ERROR, stderr, + "invalid value ('%s') passed to %s", argv[arg + 1], argv[arg]); + goto cleanup; + } + + runner.iterations = (unsigned int)iterations; + + arg++; + } else if (strcmp("param", argv[arg] + 2) == 0) { + if (arg + 2 >= argc) { + munit_logf_internal(MUNIT_LOG_ERROR, stderr, + "%s requires two arguments", argv[arg]); + goto cleanup; + } + + runner.parameters = realloc(runner.parameters, + sizeof(MunitParameter) * (parameters_size + 2)); + if (runner.parameters == NULL) { + munit_log_internal(MUNIT_LOG_ERROR, stderr, + "failed to allocate memory"); + goto cleanup; + } + runner.parameters[parameters_size].name = (char *)argv[arg + 1]; + runner.parameters[parameters_size].value = (char *)argv[arg + 2]; + parameters_size++; + runner.parameters[parameters_size].name = NULL; + runner.parameters[parameters_size].value = NULL; + arg += 2; + } else if (strcmp("color", argv[arg] + 2) == 0) { + if (arg + 1 >= argc) { + munit_logf_internal(MUNIT_LOG_ERROR, stderr, + "%s requires an argument", argv[arg]); + goto cleanup; + } + + if (strcmp(argv[arg + 1], "always") == 0) + runner.colorize = 1; + else if (strcmp(argv[arg + 1], "never") == 0) + runner.colorize = 0; + else if (strcmp(argv[arg + 1], "auto") == 0) + runner.colorize = munit_stream_supports_ansi(MUNIT_OUTPUT_FILE); + else { + munit_logf_internal(MUNIT_LOG_ERROR, stderr, + "invalid value ('%s') passed to %s", argv[arg + 1], argv[arg]); + goto cleanup; + } + + arg++; + } else if (strcmp("help", argv[arg] + 2) == 0) { + munit_print_help(argc, argv, user_data, arguments); + result = EXIT_SUCCESS; + goto cleanup; + } else if (strcmp("single", argv[arg] + 2) == 0) { + runner.single_parameter_mode = 1; + } else if (strcmp("show-stderr", argv[arg] + 2) == 0) { + runner.show_stderr = 1; +#if !defined(_WIN32) + } else if (strcmp("no-fork", argv[arg] + 2) == 0) { + runner.fork = 0; +#endif + } else if (strcmp("fatal-failures", argv[arg] + 2) == 0) { + runner.fatal_failures = 1; + } else if (strcmp("log-visible", argv[arg] + 2) == 0 || + strcmp("log-fatal", argv[arg] + 2) == 0) { + if (arg + 1 >= argc) { + munit_logf_internal(MUNIT_LOG_ERROR, stderr, + "%s requires an argument", argv[arg]); + goto cleanup; + } + + if (strcmp(argv[arg + 1], "debug") == 0) + level = MUNIT_LOG_DEBUG; + else if (strcmp(argv[arg + 1], "info") == 0) + level = MUNIT_LOG_INFO; + else if (strcmp(argv[arg + 1], "warning") == 0) + level = MUNIT_LOG_WARNING; + else if (strcmp(argv[arg + 1], "error") == 0) + level = MUNIT_LOG_ERROR; + else { + munit_logf_internal(MUNIT_LOG_ERROR, stderr, + "invalid value ('%s') passed to %s", argv[arg + 1], argv[arg]); + goto cleanup; + } + + if (strcmp("log-visible", argv[arg] + 2) == 0) + munit_log_level_visible = level; + else + munit_log_level_fatal = level; + + arg++; + } else if (strcmp("list", argv[arg] + 2) == 0) { + munit_suite_list_tests(suite, 0, NULL); + result = EXIT_SUCCESS; + goto cleanup; + } else if (strcmp("list-params", argv[arg] + 2) == 0) { + munit_suite_list_tests(suite, 1, NULL); + result = EXIT_SUCCESS; + goto cleanup; + } else { + argument = munit_arguments_find(arguments, argv[arg] + 2); + if (argument == NULL) { + munit_logf_internal(MUNIT_LOG_ERROR, stderr, + "unknown argument ('%s')", argv[arg]); + goto cleanup; + } + + if (!argument->parse_argument(suite, user_data, &arg, argc, argv)) + goto cleanup; + } + } else { + runner_tests = realloc((void *)runner.tests, + sizeof(char *) * (tests_size + 2)); + if (runner_tests == NULL) { + munit_log_internal(MUNIT_LOG_ERROR, stderr, + "failed to allocate memory"); + goto cleanup; + } + runner.tests = runner_tests; + runner.tests[tests_size++] = argv[arg]; + runner.tests[tests_size] = NULL; + } + } + + fflush(stderr); + fprintf(MUNIT_OUTPUT_FILE, + "Running test suite with seed 0x%08" PRIx32 "...\n", runner.seed); + + munit_test_runner_run(&runner); + + tests_run = runner.report.successful + runner.report.failed + + runner.report.errored; + tests_total = tests_run + runner.report.skipped; + if (tests_run == 0) { + fprintf(stderr, "No tests run, %d (100%%) skipped.\n", + runner.report.skipped); + } else { + fprintf(MUNIT_OUTPUT_FILE, + "%d of %d (%0.0f%%) tests successful, %d (%0.0f%%) test skipped.\n", + runner.report.successful, tests_run, + (((double)runner.report.successful) / ((double)tests_run)) * 100.0, + runner.report.skipped, + (((double)runner.report.skipped) / ((double)tests_total)) * 100.0); + } + + if (runner.report.failed == 0 && runner.report.errored == 0) { + result = EXIT_SUCCESS; + } + +cleanup: + free(runner.parameters); + free((void *)runner.tests); + + return result; +} + +int +munit_suite_main(const MunitSuite *suite, void *user_data, int argc, + char *const argv[MUNIT_ARRAY_PARAM(argc + 1)]) +{ + return munit_suite_main_custom(suite, user_data, argc, argv, NULL); +}